PHP GD issues with ImageCreateTrueColor and PNGs

对着背影说爱祢 提交于 2019-12-20 04:30:50

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/2647931/php-gd-issues-with-imagecreatetruecolor-and-pngs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!