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 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵