views:

186

answers:

1

I am resizing PNG images using the GD image library function ImageCopyResampled(). It all works fine, I can even keep alpha blending transparency with the use of ImageCreateTrueColor() rather than using ImageCreate() to create the resized image.

The problem is, that if I use ImageCreateTrueColor() rather than ImageCreate() the file size of PNG files increases from something like 80kb to 150kb. If I use ImageCreate() the file size stays around the same size, but colors screw!

So my question is, how can I retain alpha blending when resizing PNG images without increasing the file size?

Oh and I am reducing the dimensions of the PNGs.

A: 

With imagecreate() you're creating an indexed-color PNG file and with imagecreatetruecolor() you're creating a 24-bit color PNG file. Of course the resampling quality is going to appear much better with the true color image, since it has a much larger range of colors to use when resampling. With imagecreate(), the system can only use the much smaller palette.

You can try this out using Photoshop or Gimp, scaling images in the different color modes (indexed and RGB). Unfortunately it's the nature of the game-- the file size will be larger when there are more colors to store.

I'm not sure if it would make a difference, but you could try using imagecopyresampled() to copy to a true-color resource (from imagecreatetruecolor()), then copy (but not resample) that to a palette image resource. This way the palette is determined based on the resampled result. I'm not sure that you'd be able to retain the alpha channel, though.

awgy