简体   繁体   English

PHP:imagepng正在创建非常大的文件

[英]PHP: imagepng is creating inordinately large files

I'm using a simple thumbnailing script I wrote and it's pretty standard: 我正在使用我编写的简单缩略图脚本,它非常标准:

$imgbuffer = imagecreatetruecolor($thumbwidth, $thumbheight);
switch($type) {
  case 1: $image = imagecreatefromgif($img); break;
  case 2: $image = imagecreatefromjpeg($img); break;
  case 3: $image = imagecreatefrompng($img); break;
  case 6: $image = imagecreatefrombmp($img); break;
  case 15: $image = imagecreatefromwbmp($img); break;
  default: return log_error("Tried to create thumbnail from $img: not a valid image");
}
imagecopyresampled($imgbuffer, $image, 0, 0, 0, 0, $thumbwidth, $thumbheight, $width, $height);
$output = imagepng($imgbuffer, "$album/thumbs/$imgname.png", 9);

9 is the lowest quality setting, yet from a 400 x 600 JPEG image (at 56kB) I'm getting a thumbnail 27 kB in size (140 x 140). 9是最低质量的设置,但是从400 x 600 JPEG图像(56kB)我得到一个27 kB的缩略图(140 x 140)。 Using imagejpeg (quality of 80) instead of imagepng it's about 4kB. 使用imagejpeg(质量为80)而不是imagepng它大约是4kB。

How can this be, especially at the lowest quality setting for imagepng? 这怎么可能,特别是在imagepng的最低质量设置? I tried using imagecopy instead of imagecopyresampled, and imagecreate instead of the true color version. 我尝试使用imagecopy而不是imagecopyresampled,并尝试使用imagecreate而不是真正的颜色版本。 Unfortunately the images come out mangled somehow. 不幸的是,图像以某种方式出现了损坏。

Is there any way to get PNG thumbnails of a reasonably small file size (about 4 kB at 140 x 140)? 是否有任何方法可以获得相当小的文件大小的PNG缩略图(大约4 kB,140 x 140)? Or do I have to use JPEG? 或者我必须使用JPEG?

PNG is a lossless format and will not yield good compression ratios for photos and other complex images that are typically compressed in JPEG files. PNG是一种无损格式,对于通常在JPEG文件中压缩的照片和其他复杂图像,不会产生良好的压缩比。

To make things worse, if you're converting JPEG files to PNG, the PNG will also have to reproduce pixel-for-pixel the compression artifacts caused by the JPEG lossy compression. 更糟糕的是,如果您要将JPEG文件转换为PNG,PNG还必须逐像素地重现由JPEG有损压缩引起的压缩伪像。

Use PNG only for computer graphics and other images that are highly compressible or when you absolutely cannot lose any data (or, as Kris correctly pointed, when you need an alpha channel). 仅将PNG用于计算机图形和其他高度可压缩的图像,或者当您绝对不能丢失任何数据时(或者,正如Kris正确指出的那样,当您需要Alpha通道时)。

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

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