How to Improve my php image resizer to support alpha png and transparent GIFs

人盡茶涼 提交于 2019-12-04 20:33:33

The ugly black background disappears if you output the image to png. So here are the two alternative solutions, both tested:

  1. If you can store the thumbnail as png, just do it: change imagejpeg($dimg,$dest,100); to imagepng($dimg,$dest);

  2. If it's important to store it as jpeg, remove the lines imagealphablending( $dimg, false ); and imagesavealpha( $dimg, true ); -- the default values of true and false, respectively, will provide the desired effect. Disabling alpha blending only makes sense if the result image also has an alpha channel.

I haven't tested it myself but this was an idea I've had for doing exactly that when working on a project of mine.

First, find a color that isn't used in the image and create a new image with that as the background (really flashy green for example, just like they do with motion capture)

Next, copy your image with transparency over it (I know this works with PHP I used to do this to put borders over images)

Then, use the function imagecolortransparent to define which color in that image is transparent and give it your flashy green color.

I think it would work but I haven't tested it.

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