Jump to content

[SOLVED] Replace one color with another in an image?


kernelgpf

Recommended Posts

I have the following script that replaces all red pixels on an image with a specified image tile, but I want to be able to swap the red with another hex color which I'll feed to the script. Could somebody show me how to do this? Thanks!

 

$v=$_SERVER["DOCUMENT_ROOT"];
$skinfile = isset($_GET['skin']) ? $_GET['skin'] : "$v/Etc/dragonskins/bluewater.gif";
$skin = imagecreatetruecolor(500,508);
$tile = imagecreatefromgif($skinfile);
imagesettile($skin, $tile);
imagefill($skin, 250, 205, IMG_COLOR_TILED);
/**
* convert dragon png to truecolor
*/
$im = imagecreatetruecolor(500,508);
$dragon = imagecreatefrompng("$v/waterdragon.png");
imagecopy($im,$dragon,0,0,0,0,500,508);

/**
* select color to be replaced by skin
*/
$red = imagecolorat($im, 255,400);     // coords of a red pixel
for ($x=0; $x<500; $x++) {
    for ($y=0; $y<508; $y++) {
        if (imagecolorat($im,$x,$y) == $red) {
            $skinpix = imagecolorat($skin,$x,$y);
            imagesetpixel($im,$x,$y,$skinpix);
        }
    }
}
#header("content-type: image/png");
imagepng($im, 'bluedragon.png');
imagedestroy($im);
imagedestroy($skin);
imagedestroy($dragon);
imagedestroy($tile);

Archived

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

×
×
  • Create New...

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.