does anyone know a way to color a PNG image, and preserve the transparency? so for example if you had this image here: http://clipartist.org/RSS/openclipart.org/2012/March/June/11-Saturday/wheat_black_and_white_background_wall_paper_wallpaper-555px.png

Is there a way (preferably using PHP or jQuery) to dynamically add a color to the black part of the image on the fly while keeping the transparent pixels?

有帮助吗?

解决方案

Use a svg file instead for such simple background images. A svg file can be can be changed dynamicly in php or either in javascript/jquery by changing the css color of the shape. This can be done in the svg itself or an external stylesheet.

其他提示

As far as I know, PHP's GD support does not support color replacement unless you interate through all pixels one at a time, compare the value, and conditionally replace it with the new color. It will be very slow, though.

Check out imagemagick's replace function. You could write a simple PHP script that runs your image through that command to the stdout direct to the browser. (See the PHP passthru function.)

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top