相关疑难解决方法(0)

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

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

我目前正在使用GD功能imagecopyresampled来拍摄高分辨率图像,并将它们干净地调整到适合网页查看的大小(大约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 …
Run Code Online (Sandbox Code Playgroud)

php gd jpeg image

81
推荐指数
5
解决办法
8万
查看次数

标签 统计

gd ×1

image ×1

jpeg ×1

php ×1