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 = $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);
holdtom
宝慕林4294392