在PHP中高效调整JPEG图像大小

时间:2020-03-05 18:39:58  来源:igfitidea点击:

在PHP中调整大图像大小的最有效方法是什么?

我目前正在使用GD函数imagecopyresampled来拍摄高分辨率图像,然后将其干净地重新调整大小以进行Web查看(大约700像素宽乘700像素高)。

这对于较小(小于2 MB)的照片非常有用,并且整个调整大小操作在服务器上花费的时间不到一秒钟。但是,该网站最终将为摄影师提供服务,他们可能会上传最大10 MB的图像(或者最大5000x4000像素的图像)。

对大图像执行这种大小调整操作往往会大大增加内存使用量(较大的图像可能会使脚本的内存使用量超过80 MB)。有什么方法可以使此大小调整操作更有效?我应该使用替代图像库(例如ImageMagick)吗?

现在,调整大小的代码看起来像这样

function makeThumbnail($sourcefile, $endfile, $thumbwidth, $thumbheight, $quality) {
    // Takes the sourcefile (path/to/image.jpg) and makes a thumbnail from it
    // and places it at endfile (path/to/thumb.jpg).

    // Load image and get image size.
    $img = imagecreatefromjpeg($sourcefile);
    $width = imagesx( $img );
    $height = imagesy( $img );

    if ($width > $height) {
        $newwidth = $thumbwidth;
        $divisor = $width / $thumbwidth;
        $newheight = floor( $height / $divisor);
    } else {
        $newheight = $thumbheight;
        $divisor = $height / $thumbheight;
        $newwidth = floor( $width / $divisor );
    }

    // Create a new temporary image.
    $tmpimg = imagecreatetruecolor( $newwidth, $newheight );

    // Copy and resize old image into new image.
    imagecopyresampled( $tmpimg, $img, 0, 0, 0, 0, $newwidth, $newheight, $width, $height );

    // Save thumbnail into a file.
    imagejpeg( $tmpimg, $endfile, $quality);

    // release the memory
    imagedestroy($tmpimg);
    imagedestroy($img);

解决方案

回答

phpThumb尽可能使用ImageMagick来提高速度(如有必要,可以降为GD),并且似乎可以很好地缓存以减少服务器上的负载。试用起来非常轻巧(要调整图像的大小,只需使用包含图形文件名和输出尺寸的GET查询调用phpThumb.php即可),因此我们可以试试看它是否满足需求。

回答

这是我在项目中使用过的php.net文档的一个片段,可以正常工作:

<?
function fastimagecopyresampled (&$dst_image, $src_image, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h, $quality = 3) {
    // Plug-and-Play fastimagecopyresampled function replaces much slower imagecopyresampled.
    // Just include this function and change all "imagecopyresampled" references to "fastimagecopyresampled".
    // Typically from 30 to 60 times faster when reducing high resolution images down to thumbnail size using the default quality setting.
    // Author: Tim Eckel - Date: 09/07/07 - Version: 1.1 - Project: FreeRingers.net - Freely distributable - These comments must remain.
    //
    // Optional "quality" parameter (defaults is 3). Fractional values are allowed, for example 1.5. Must be greater than zero.
    // Between 0 and 1 = Fast, but mosaic results, closer to 0 increases the mosaic effect.
    // 1 = Up to 350 times faster. Poor results, looks very similar to imagecopyresized.
    // 2 = Up to 95 times faster.  Images appear a little sharp, some prefer this over a quality of 3.
    // 3 = Up to 60 times faster.  Will give high quality smooth results very close to imagecopyresampled, just faster.
    // 4 = Up to 25 times faster.  Almost identical to imagecopyresampled for most images.
    // 5 = No speedup. Just uses imagecopyresampled, no advantage over imagecopyresampled.

    if (empty($src_image) || empty($dst_image) || $quality <= 0) { return false; }
    if ($quality < 5 && (($dst_w * $quality) < $src_w || ($dst_h * $quality) < $src_h)) {
        $temp = imagecreatetruecolor ($dst_w * $quality + 1, $dst_h * $quality + 1);
        imagecopyresized ($temp, $src_image, 0, 0, $src_x, $src_y, $dst_w * $quality + 1, $dst_h * $quality + 1, $src_w, $src_h);
        imagecopyresampled ($dst_image, $temp, $dst_x, $dst_y, 0, 0, $dst_w, $dst_h, $dst_w * $quality, $dst_h * $quality);
        imagedestroy ($temp);
    } else imagecopyresampled ($dst_image, $src_image, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h);
    return true;
}
?>

http://us.php.net/manual/zh/function.imagecopyresampled.php#77679

回答

人们说ImageMagick更快。充其量只是比较两个库并进行度量即可。

  • 准备1000张典型图像。
  • 编写两个脚本-一个用于GD,一个用于ImageMagick。
  • 将它们都运行几次。
  • 比较结果(总执行时间,CPU和I / O使用率,结果图像质量)。

其他人最好的东西可能对我们来说不是最好的。

另外,我认为ImageMagick具有更好的API接口。

回答

我建议我们按照以下方式工作:

  • 对上传的文件执行getimagesize(),以检查图像类型和大小
  • 将所有小于700x700px的上载JPEG图像保存到目标文件夹"原样"中
  • 将GD库用于中等大小的图像(有关代码示例,请参见本文:使用PHP和GD库调整图像大小)
  • 使用ImageMagick放大图像。如果愿意,可以在后台使用ImageMagick。

要在后台使用ImageMagick,请将上传的文件移动到一个临时文件夹中,并安排一个CRON作业,以将所有文件"转换"为jpeg并相应地调整其大小。请参阅以下命令的语法:imagemagick-命令行处理

我们可以提示用户该文件已上载并计划进行处理。 CRON作业可以安排为每天在特定间隔运行。处理后可以删除源图像,以确保不对图像进行两次处理。

回答

对于较大的图像,请使用phpThumb()。使用方法如下:http://abcoder.com/php/problem-with-resizing-corrupted-images-using-php-image-functions/。它也适用于大尺寸损坏的图像。