douhui3760 2010-06-15 23:09
浏览 182
已采纳

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

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). Using imagejpeg (quality of 80) instead of imagepng it's about 4kB.

How can this be, especially at the lowest quality setting for imagepng? I tried using imagecopy instead of imagecopyresampled, and imagecreate instead of the true color version. 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)? Or do I have to use JPEG?

  • 写回答

1条回答 默认 最新

  • dq23171 2010-06-15 23:17
    关注

    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.

    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.

    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).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 python验证码滑块图像识别
  • ¥15 QT6颜色选择对话框显示不完整
  • ¥20 能提供一下思路或者代码吗
  • ¥15 用twincat控制!
  • ¥15 请问一下这个运行结果是怎么来的
  • ¥15 单通道放大电路的工作原理
  • ¥30 YOLO检测微调结果p为1
  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)