Re: [GD-DEVEL] Replacing one color with another

From: Date: Sat, 05 May 2007 08:03:11 +0000
Subject: Re: [GD-DEVEL] Replacing one color with another
References: 1  Groups: php.gd.devel 
Request: Send a blank email to php-gd-devel+get-190@lists.php.net to get a copy of this message
Hi Andrie, On 5/4/07, Andrei Stebakov <lispercat@gmail.com> wrote:
I was wondering if there is a way to replace one color for another in an image? Let's say I have an image with a white background and a number of solid color figures (circles) in say red color. I need to replace those red circles into black ones. If not gdlib, could you suggest a way I can go here?
Something like this should work for both truecolor and palette image: black = gdImageColorAllocate(im, 0,0,0); red = gdImageColorAllocate(im, 255,0,0); //red for (y=0; y < gdImageSY(im); y++) { for (x=0; x < gdImageSx(im); x++) { int c = gdImageGetPixel(im, x,y); if (c == red) { gdImageSetPixel(im, x, y, black); } } } For palette images only, you can replace the red color index values directly, it should do it as well (a bit hackish though with the current API). --Pierre

« previous php.gd.devel (#190) next »