PHP中高效的JPEG图像大小调整 [英] Efficient JPEG Image Resizing in PHP

查看:87
本文介绍了PHP中高效的JPEG图像大小调整的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

What's the most efficient way to resize large images in PHP?

我目前正在使用 GD 功能imagecopyresampled采取高分辨率图像,且干净地调整它们的大小下降到一个尺寸为web查看(宽大致700像素×700个像素高)。

I'm currently using the GD function imagecopyresampled to take high resolution images, and cleanly resize them down to a size for web viewing (roughly 700 pixels wide by 700 pixels tall).

这适用于小型(2 MB以下)照片,整个调整大小操作在服务器上只需不到一秒钟。但是,该网站最终将为可能上传最大10 MB图像的摄影师提供服务(或者图片尺寸最大为5000x4000像素)。

This works great on small (under 2 MB) photos and the entire resize operation takes less than a second on the server. However, the site will eventually service photographers who may be uploading images up to 10 MB in size (or images up to 5000x4000 pixels in size).

进行此类调整大小使用大图像的操作往往会使内存使用量大幅增加(较大的图像会使脚本的内存使用量超过80 MB)。有没有办法让这个调整大小的操作更有效率?我应该使用替代图像库例如 ImageMagick的

Doing this kind of resize operation with large images tends to increase the memory usage by a very large margin (larger images can spike the memory usage for the script past 80 MB). Is there any way to make this resize operation more efficient? Should I be using an alternate image library such as ImageMagick?

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

Right now, the resize code looks something like this

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);


推荐答案

人们说ImageMagick要快得多。最好只比较两个库并测量它。

People say that ImageMagick is much faster. At best just compare both libraries and measure that.


  1. 准备1000张典型图像。

  2. 写两个脚本 - 一个用于GD,一个用于
    用于ImageMagick。

  3. 运行它们几次。

  4. 比较结果(总执行
    时间,CPU和I / O使用,结果
    图像质量)。

  1. Prepare 1000 typical images.
  2. Write two scripts -- one for GD, one for ImageMagick.
  3. Run both of them a few times.
  4. Compare results (total execution time, CPU and I/O usage, result image quality).

最好的每个人否则,对你来说不是最好的。

Something which the best everyone else, could not be the best for you.

另外,在我看来,ImageMagick有更好的API接口。

Also, in my opinion, ImageMagick has much better API interface.

这篇关于PHP中高效的JPEG图像大小调整的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆