Jump to content

Archived

This topic is now archived and is closed to further replies.

nimshon

GD Help

Recommended Posts

Hello

I am almost killing myself....
I am trying to change all the magenta to another color (doesnt matter) in an image.
how can I do that?
please help

thanks a lot
Nemesh

Share this post


Link to post
Share on other sites
For a gif or an 8bit png, you can change the specific color in the palette:
[code]<?php
$image = imagecreatefromgif("image.gif");
$from = "ff0000";
$to = "0000ff";

$c1 = sscanf($from,"%2x%2x%2x");
$c2 = sscanf($to,"%2x%2x%2x");

$index = imagecolorexact($image,$c1[0],$c1[1],$c1[2]);
imagecolorset($image,$index,$c2[0],$c2[1],$c2[2]);

header("Content-Type: image/png");
imagepng($image);
?>[/code]

If you are wanting to do this with a true colour image, such as a jpeg, it's a little more involving - The only way I have thought about doing this is checking/changing the colour of each pixel (pixel by pixel).

Share this post


Link to post
Share on other sites
[quote author=SemiApocalyptic link=topic=101101.msg399871#msg399871 date=1153323819]
$c1 = sscanf($from,"%2x%2x%2x");
$c2 = sscanf($to,"%2x%2x%2x");
[/quote]

Neat!

I'll adapt and use that as
[code]<?php
$color = "#ff0000";
list ($r, $g, $b) = sscanf($color,"#%2x%2x%2x");
?>[/code]

Share this post


Link to post
Share on other sites
Thanks a lot !
but if the color i want to change is in different opacities?

Nemesh

Share this post


Link to post
Share on other sites
Technically, gifs and 8bit pngs do not support different opacities, other then totally transparent or not transparent at all. So you'd be looking at replacing a range of colours in the palette. Looking at the manual, I am unsure of how you'd easily get a list of all of the colours in the palette, so it looks like it might be a case of checking each pixel. This option would also then work with truecolour images too.

I'm personally working on a colour replacement script at the moment for replacing colours in photographs, although my script only replaces the hue (still working on the saturation and value - Having difficulty trying to get it to work there).

So here's what I have found out so far - You'll need a range of colours you want to replace - a 'threshold'. The best awy I could work out how to do this is to convert the colour from hexadecimal, to RGB, and then from RGB into HSV. I chose HSV because of how it works makes it a lot easier to select 'similar' colours, or in my case hues. If you are not clued up on the HSV model there's a good explination on Wikipedia, but basically, HSV is made up of Hue, Saturation and Value. Hue ranges from 0 - 360 degrees (normally displayed as a 'ring' of merging colours), Saturation and Value are pretty much other names for Brightness and Contrast I believe and each range from 0 - 100%.

When seeing the colour wheel, you'll see why this is going to be the easier option for working out the 'range' or colours you want to change.

You'll need to loop through each pixel in the image with two for loops, one nested inside the other to loop through both the x and y coordinates, and then check each colours hue to see if it is in your selected 'range' by doing the conversion and checking if its hue (degrees) lies within the threshold of your predefined (converted) hue. If so, change the pixels hue (this time, doing the conversion in reverse - HSV -> RGB -> HEX).

Thats pretty much it, in a nutshell. I think it's a good starting point for you :)

The formulae for the conversions, HEX - > RGB can be seen in my above example, made a bit clearer in Barands adaptation. And RGB -> HSV, there is a formula on the Wikipedia entery for HSV I think...

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.