views:

21

answers:

1

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?

+2  A: 

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

Artefacto
Thank you, this is just the answer I was hoping for - it clarifies everything for me. Thanks again.
Rafael