简体   繁体   English

使用imagejpeg使图像文件变小

[英]Using imagejpeg to make image files unnoticeable smaller

I'm making an image sharing website with php. 我正在用php创建图像共享网站。 I was reading about google's pagespeed, and they said I needed to optimize my images, so I've been trying to do that with imagejpeg, then hopefully I'll be able to use basically the same code with png and gif. 我在读有关Google的Pagespeed的文章,他们说我需要优化我的图像,所以我一直在尝试使用imagejpeg来做到这一点,然后希望我能够在png和gif中使用基本相同的代码。

This is the code I have at the moment: 这是我目前的代码:

$img = imagecreatefromjpeg($original);

if ($extension === "jpg" || $extension === "jpeg"){
    $location = $_SERVER['DOCUMENT_ROOT'] . "/uploads/" . $id . "/fullsize." . $extension;
    $temp = fopen($location, 'w');
    imagejpeg($img, $location, 80);
    fclose($temp);
}

And it does the job of taking the image at $original, and saving a copy that's been through imagejpeg to $location. 它的工作是以$ original拍摄图像,并将通过imagejpeg保存的副本保存到$ location。

But sometimes the resulting file is larger than the original was! 但是有时生成的文件比原始文件大! This is where I get stumped. 这就是我感到难过的地方。

Why is this happening? 为什么会这样呢?

How can I make it stop happening? 我怎样才能使它停止发生?

I'm trying to reduce image file sizes without hurting their appearance, is there a better way to do it than this? 我正在尝试减小图像文件的大小而又不损害其外观,有没有比这更好的方法了?

Thanks, 谢谢,

Liam 利亚姆

imagejpeg will only shrink the file if you save it at a lower quality setting than the original was produced with 如果您将文件保存为比使用以下文件生成的原始文件质量低的质量设置,则imagejpeg仅会收缩文件

eg 例如

imagejpeg($gd, 'temp.jpg', 50);
$temp = imagecreatefromjpeg('temp.jpg');
imagejpeg($gd, 'temp2.jpg', 70);

will probably produce a larger temp2 file, because it's at a higher quality setting, which roughly translates to "don't compress as much". 可能会生成一个更大的temp2文件,因为它的质量设置较高,这大概会转化为“压缩程度不高”。

There's no way to predict what the final output size will be for a given image and quality setting without actually going through the whole jpeg compress process. 如果没有实际完成整个jpeg压缩过程,就无法预测给定图像和质量设置的最终输出大小。 You may have to do a few trial recompresses of an image and lower the quality setting each time until you start getting some shrinkage. 您可能需要对图像进行几次尝试性的重新压缩,并每次都降低质量设置,直到开始出现收缩。

of course, if you lower the quality TOO low, you'll basically trash the image and definitely introduce visible artifacts. 当然,如果您将质量降低得太低,则基本上会破坏图像,并肯定会引入可见的伪像。

It definitely depends on the quality of the original image. 这绝对取决于原始图像的质量。 If your image is quality 60 originally, saving at quality 80 will generally result in a larger file size. 如果原始图像的质量为60,则保存为质量80通常会导致文件较大。

However, it also depends on what software created the original image. 但是,这还取决于创建原始图像的软件。 For example, I've observed many times where Adobe Photoshop can save an image at quality 60 that looks as good as 80 from GD (the graphics library you are using). 例如,我观察到很多次Adobe Photoshop可以保存质量为60的图像,而该图像看起来可以比GD(您使用的图形库)中的80好。 Also, Photoshop can often save images at the same quality as another graphics package but with a smaller size. 此外,Photoshop经常可以以与其他图形包相同的质量保存图像,但是尺寸较小。 This is subjective, of course. 当然,这是主观的。 I'm not necessarily concluding Photoshop is "better," but it does appear to have it's options well tuned. 我不一定要得出结论,Photoshop是“更好的”,但是它似乎确实调整了选项。

I recommend you try ImageMagick, as I've seen it generally do a better job (file sizes, image quality) than GD. 我建议您尝试ImageMagick,因为我发现它通常比GD做得更好(文件大小,图像质量)。 You should also determine the original quality setting per image so you don't accidentally grow images. 您还应该确定每张图像的原始质量设置,以免意外增大图像。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM