Can someone help me with a simple script to replace a specific color with another color in an image using PHP? Here is a example (color changed from green to yellow).
Can someone help me with a simple script to replace a specific color with another color in an image using PHP? Here is a example (color changed from green to yellow).
If you meant using GD library in PHP, you should give a check on imagefilter()
Steps are:
imagefilter($img, IMG_FILTER_COLORIZE, 0, 255, 0))
Where 0,255,0 is your RGB color (bright green in this example)Edit, Working code and clarification.
I meant, using alpha for OUTER of the black lines, and white INSIDE. Here's the sample image:
And here's a working code for colorizing white parts:
header('Content-Type: image/png');
/* RGB of your inside color */
$rgb = array(0,0,255);
/* Your file */
$file="../test.png";
/* Negative values, don't edit */
$rgb = array(255-$rgb[0],255-$rgb[1],255-$rgb[2]);
$im = imagecreatefrompng($file);
imagefilter($im, IMG_FILTER_NEGATE);
imagefilter($im, IMG_FILTER_COLORIZE, $rgb[0], $rgb[1], $rgb[2]);
imagefilter($im, IMG_FILTER_NEGATE);
imagealphablending( $im, false );
imagesavealpha( $im, true );
imagepng($im);
imagedestroy($im);
Note: We must negate values since colorize only works for non-white parts. We could have a workaround to this by having white-bordered image with black inside.
Note: This code only works for black-border and white-inner images.
The slow but sure approach, iterating over every pixel.
function ReplaceColour($img, $r1, $g1, $b1, $r2, $g2, $b2)
{
if(!imageistruecolor($img))
imagepalettetotruecolor($img);
$col1 = (($r1 & 0xFF) << 16) + (($g1 & 0xFF) << 8) + ($b1 & 0xFF);
$col2 = (($r2 & 0xFF) << 16) + (($g2 & 0xFF) << 8) + ($b2 & 0xFF);
$width = imagesx($img);
$height = imagesy($img);
for($x=0; $x < $width; $x++)
for($y=0; $y < $height; $y++)
{
$colrgb = imagecolorat($img, $x, $y);
if($col1 !== $colrgb)
continue;
imagesetpixel ($img, $x , $y , $col2);
}
}
I guess the answer would be to have multiple versions of the image and then load the correct image depending on the chosen colour?
You could use a switch statement to load the correct image
//get selected colour
switch ($colour) {
case "red":
echo "<img src='RED IMAGE' ";
break;
case "blue":
echo "<img src='blue IMAGE' ";
break;
case "green":
echo "<img src='green IMAGE' ";
break;
}
Hope this helps.
I tried this:
<?php
$imgname = "1.gif";
$im = imagecreatefromgif ($imgname);
$index = imagecolorexact ($im,0,128,0);
imagecolorset($im,$index,240,255,0);
$imgname = "result.gif";
imagegif($im,$imgname);
?>
<img src="result.gif">
And instead of replacing every green pixel I got this (shirt color didn't changed):
<?php
header("Content-type: image/png");
$im = imagecreate(200, 200)
imagefill($im, 0, 0, $red);
// above could come from an uploaded image
// find a blue in the image
$newblue = imagecolorclosest($im, 0, 0, 255);
// change it to green
imagecolorset($im, $newblue, 0, 255, 0);
imagepng($im);
imagedestroy($im);
?php>
Here you find closest color to the blue and replace with green.