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);
3 回答
![?](http://img1.sycdn.imooc.com/545863080001255902200220-100-100.jpg)
holdtom
TA贡献1805条经验 获得超10个赞
人们说ImageMagick要快得多。最好只比较两个库并测量它。
准备1000张典型图像。
写两个脚本 - 一个用于GD,一个用于ImageMagick。
两次运行它们。
比较结果(总执行时间,CPU和I / O使用情况,结果图像质量)。
其他人最好的东西,对你来说不是最好的。
另外,在我看来,ImageMagick有更好的API接口。
![?](http://img1.sycdn.imooc.com/5333a207000118af02200220-100-100.jpg)
宝慕林4294392
TA贡献2021条经验 获得超8个赞
phpThumb尽可能使用ImageMagick来提高速度(必要时回退到GD)并且似乎缓存很好以减少服务器上的负载。尝试它是非常轻量级的(调整图像大小,只需用包含图形文件名和输出尺寸的GET查询调用phpThumb.php),这样你就可以试一试它是否符合你的需求。
- 3 回答
- 0 关注
- 672 浏览
添加回答
举报
0/150
提交
取消