Welcome Guest, Not a member yet? Register   Sign In
GD Newbie Question
#1

[eluser]Vik[/eluser]
This is my first time using GD. I've got an image with a transparent background. All the other pixels are the identical color (#8bc4ed). I want to replace all those pixels with a given color, say #CCC5AC. Here's my code that doesn't work yet:

Code:
$filepath = $_SERVER['DOCUMENT_ROOT'] . "/images/source_image.gif";
$image = imagecreatefromgif($filepath);

$indexToSearchFor = imagecolorat($image, 3, 0);
imagecolorset($image, $indexToSearchFor, 204, 197, 172);

$filepath = $_SERVER['DOCUMENT_ROOT'] . "/images/target.gif";
imagegif ($image,  $filepath);

The output image is identical to the input image - no color is replaced.

What is the correct way to do this?

I'm attaching the image I'm trying to change in case you can tell anything from it. Thanks very much to all in advance for any info!
#2

[eluser]Vik[/eluser]
I got it working using this code. I'm not sure if this is the most elegant approach, but it seems to work. Smile

Code:
function ReplaceAllColorsInImageWithThisColor(&$image, $RBGColor)
    {
        for($i=0;$i<imagecolorstotal($image);$i++)
        {
         $col=ImageColorsForIndex($image,$i);
         imagecolorset($image,$i,$RBGColor['R'], $RBGColor['G'], $RBGColor['B']);
        }
    }




Theme © iAndrew 2016 - Forum software by © MyBB