使用PHP优化图像到YSlow标准?

时间:2013-01-22 16:17:05

标签: php yslow image-optimization

我无法找到这个问题的明确答案。

我想无损地优化我的图像(主要是jpg),以便它满足YSlow的图像优化标准。我无法确定需要采取哪些措施才能做到这一点。

我知道像smush.it这样的服务,但我不想依赖API或服务。

基本上我正在寻找的是根据YSlow标准优化图像的步骤列表。

仅使用GD并改变质量是不够的,我还需要做什么?

2 个答案:

答案 0 :(得分:3)

您是否尝试使用imageinterlace()生成渐进式JPG?它使较小的图像稍大,但较大的图像要小得多。这是我的图像优化代码的最后一块拼图。

示例代码

<?php
$new_img = imagecreatetruecolor($img_width, $img_height);
imageinterlace($new_img, true); // Use progressive JPGs
$white = imagecolorallocate($new_img, 255, 255, 255);
imagefilledrectangle($new_img, 0, 0, $img_width, $img_height, $white);
imagecopyresampled($new_img, $img, $x, $y, 0, 0, $new_width, $new_height, $width, $height);
header("content-type: image/jpg");
imagejpeg($new_img, NULL, 100);
imagedestroy($img);
imagedestroy($new_img);
die;
?>

答案 1 :(得分:0)

在Mike Brittain的Wesley项目中实现了一个很好的粗略等效的Smush.it功能。它是Perl而不是PHP,但您可以根据自己的需要进行调整,或者只是在命令行中对它进行调整。

它实现了Smush.it FAQ中描述的类似压缩工具和步骤。