【问题标题】:Fetch Image from mysql and resize using timthumb in php从 mysql 获取图像并在 php 中使用 timthumb 调整大小
【发布时间】:2014-01-13 07:32:54
【问题描述】:

我正在使用 timthumb.php 调整图像大小。 它适用于本地图像,但是如果我有可以从 MYSQL 获取图像的 URL,然后我想在显示之前动态调整该图像的大小怎么办? 我尝试了几种方法。 我将从 MYSQL 获取图像的 URL 是

http://somedomain/final/getImage.php?id=1234

我想做类似下面的事情,但它不起作用

<img src="php_helpers/timthumb.php?src=http://somedomain/final/getImage.php?id=1234&w=260" alt="" />

任何帮助将不胜感激。 谢谢

【问题讨论】:

  • 我不知道网上的图片resize url。要不要给你php图片resize类。
  • @Sankar - 这会很有帮助。请分享。

标签: php mysql image-processing timthumb


【解决方案1】:

你应该在 src 标签中使用图片路径而不是 php path.like

&lt;img src="timthumb.php?src=/images/filename.jpg&amp;h=150&amp;w=150&amp;zc=1" alt="some text" /&gt;

如果你想从数据库中获取图像路径,那么你应该写类似

<?php
 $sql = "SELECT image FROM image_tbl WHERE ID ='$image_id'";
 $result = mysql_query($sql);
 $image = mysql_result($result, 0);

 echo '<img src="' $image'"/>';

 ?> 

【讨论】:

  • 我没有在数据库中保存图像路径我已经在 MYSQL 中保存了整个图像
  • 好的,你的数据库中保存的(图像路径/图像 url)数据是什么。如果你使用 move_uploaded_file() 在任何文件夹中上传图像,那么你可以轻松地使用 src=" "
  • @pryajain- 我没有在数据库中保存路径/图像 url。
【解决方案2】:

您好,这是图像调整大小类。请在您的应用程序中使用它。如果需要任何更改取决于您的应用程序路径,请在此进行。

<?php
    /**
    * Resize image class will allow you to resize an image
    *
    * Can resize to exact size
    * Max width size while keep aspect ratio
    * Max height size while keep aspect ratio
    * Automatic while keep aspect ratio
    */
    class ResizeImage
    {
    private $ext;
    private $image;
    private $newImage;
    private $origWidth;
    private $origHeight;
    private $resizeWidth;
    private $resizeHeight;
    /**
    * Class constructor requires to send through the image filename
    *
    * @param string $filename – Filename of the image you want to resize
    */
    public function __construct( $filename )
    {
        if(file_exists($filename))
        {
        $this->setImage( $filename );
        } else {
        throw new Exception(‘Image ‘ . $filename . ‘ can not be found, try another image.’);
        }
    }
    /**
    * Set the image variable by using image create
    *
    * @param string $filename – The image filename
    */
    private function setImage( $filename )
        {
        $size = getimagesize($filename);
        $this->ext = $size['mime'];
        switch($this->ext)
        {
        // Image is a JPG
        case ‘image/jpg’:
        case ‘image/jpeg’:
        // create a jpeg extension
        $this->image = imagecreatefromjpeg($filename);
        break;
        // Image is a GIF
        case ‘image/gif’:
        $this->image = @imagecreatefromgif($filename);
        break;
        // Image is a PNG
        case ‘image/png’:
        $this->image = @imagecreatefrompng($filename);
        break;
        // Mime type not found
        default:
        throw new Exception(“File is not an image, please use another file type.”, 1);
        }
        $this->origWidth = imagesx($this->image);
        $this->origHeight = imagesy($this->image);
    }
    /**
    * Save the image as the image type the original image was
    *
    * @param  String[type] $savePath     – The path to store the new image
    * @param  string $imageQuality       – The qulaity level of image to create
    *
    * @return Saves the image
    */
    public function saveImage($savePath, $imageQuality=”100″, $download = false)
    {
        switch($this->ext)
        {
        case ‘image/jpg’:
        case ‘image/jpeg’:
        // Check PHP supports this file type
        if (imagetypes() & IMG_JPG) {
        imagejpeg($this->newImage, $savePath, $imageQuality);
        }
        break;
        case ‘image/gif’:
        // Check PHP supports this file type
        if (imagetypes() & IMG_GIF) {
        imagegif($this->newImage, $savePath);
        }
        break;
        case ‘image/png’:
        $invertScaleQuality = 9 – round(($imageQuality/100) * 9);
        // Check PHP supports this file type
        if (imagetypes() & IMG_PNG) {
        imagepng($this->newImage, $savePath, $invertScaleQuality);
        }
        break;
        }
        if($download)
        {
        header(‘Content-Description: File Transfer’);
        header(“Content-type: application/octet-stream”);
        header(“Content-disposition: attachment; filename= “.$savePath."");
        readfile($savePath);
        }
        imagedestroy($this->newImage);
    }
    /**
    * Resize the image to these set dimensions
    *
    * @param  int $width            - Max width of the image
    * @param  int $height           - Max height of the image
    * @param  string $resizeOption – Scale option for the image
    *
    * @return Save new image
    */
    public function resizeTo( $width, $height, $resizeOption = ‘default’ )
    {
        switch(strtolower($resizeOption))
        {
        case ‘exact’:
        $this->resizeWidth = $width;
        $this->resizeHeight = $height;
        break;
        case ‘maxwidth’:
        $this->resizeWidth  = $width;
        $this->resizeHeight = $this->resizeHeightByWidth($width);
        break;
        case ‘maxheight’:
        $this->resizeWidth  = $this->resizeWidthByHeight($height);
        $this->resizeHeight = $height;
        break;
        default:
        if($this->origWidth > $width || $this->origHeight > $height)
        {
        if ( $this->origWidth > $this->origHeight ) {
        $this->resizeHeight = $this->resizeHeightByWidth($width);
        $this->resizeWidth  = $width;
        } else if( $this->origWidth < $this->origHeight ) {
        $this->resizeWidth  = $this->resizeWidthByHeight($height);
        $this->resizeHeight = $height;
        }
        } else {
        $this->resizeWidth = $width;
        $this->resizeHeight = $height;
        }
        break;
        }
        $this->newImage = imagecreatetruecolor($this->resizeWidth, $this->resizeHeight);
        imagecopyresampled($this->newImage, $this->image, 0, 0, 0, 0, $this->resizeWidth, $this->resizeHeight, $this->origWidth, $this->origHeight);
    }
    /**
    * Get the resized height from the width keeping the aspect ratio
    *
    * @param  int $width – Max image width
    *
    * @return Height keeping aspect ratio
    */
    private function resizeHeightByWidth($width)
    {
    return floor(($this->origHeight/$this->origWidth)*$width);
    }
    /**
    * Get the resized width from the height keeping the aspect ratio
    *
    * @param  int $height – Max image height
    *
    * @return Width keeping aspect ratio
    */
    private function resizeWidthByHeight($height)
    {
    return floor(($this->origWidth/$this->origHeight)*$height);
    }
    }
    ?>

    <!– Below functions is to used for resizing and saving the image –>


    <?php
    $resize = new ResizeImage(‘images/1.jpg’);
    $resize->resizeTo(500, 500, ‘exact’);
    $resize->saveImage(‘images/5.jpg’);
    $resize->resizeTo(500, 500, ‘maxWidth’);
    $resize->saveImage(‘images/6.png’);
    $resize->resizeTo(500, 500, ‘maxHeight’);
    $resize->saveImage(‘images/7.png’);
    $resize->resizeTo(500, 500);
    $resize->saveImage(‘images/8.png’);
    //$resize->resizeTo(500, 500, ‘exact’);
    //$resize->saveImage(‘images/9.png’, “100″, true);
    echo “<img src=’images/1.jpg’>”;
    echo “<br>”;
    echo “<img src=’images/6.png’>”;
    echo “<br>”;
    echo “<img src=’images/7.png’>”;
    echo “<br>”;
    echo “<img src=’images/8.png’>”;
    echo “<br>”;
    echo “<img src=’images/9.jpg’>”;
    //echo “<img src=’images/5.jpg’ height=’600′ width=’1000′>”;
    ?>

如果您在使用此代码时遇到任何问题,请与我分享。我会帮你。

【讨论】:

  • 这个类怎么用?谁将图像传递给这个类?你能添加代码sn-p..
  • $resize = new ResizeImage('images/1.jpg'); $resize->resizeTo(500, 500, ‘exact’); $resize->saveImage('images/5.jpg');以上三行只有你需要把你的实际图像现在出现在哪里。它实际上是调整大小并将调整大小的图像保存到我们自己的位置。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-02-28
  • 1970-01-01
  • 2015-09-12
相关资源
最近更新 更多