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 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值