1
votes

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?

2
What do you need to do in cases where pixels are only partially transparent?Brad
Ideally they would be tinted relative to the pixel opacity - however, if they were all the same tint that would be fine. I am trying to tint dividers like this xtiaan.splash.me/images/divider4.b.pngChris
So what you're really looking for is a mask of sorts.Brad
Sure, however it can be achieved - but these images will sit on dynamically colored backgrounds, so the original image transparency has to be preserved.Chris
@Ohgodwhy, This can easily be done with imagemagick. As soon as this presidential debate is over, I'll look it up.Brad

2 Answers

1
votes

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.

1
votes

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