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).
Go to the Image menu, then to Adjustments , and choose Replace Color . When the dialog box opens, the first step is to sample the color in the image you want to replace by clicking on it. Now go to the Hue , Saturation , and Lightness controls to set the color you want to use as a replacement.
Color Swapping simply refers to the idea of substituting a specific color/colors in an image with some targeted color for the purpose of exploration, design, image creation, etc.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With