Reputation: 777
Hey guys i am trying to recolor the following image with php:
I want to give my user the possibility to change the color of the banner with a a few clicks in my php application. So i used this little experimental script i got here
This is my script to change one rgb color of my image:
$imgname = "test.png";
$im = imagecreatefrompng($imgname);
imagealphablending($im, false);
for ($x = imagesx($im); $x--;) {
for ($y = imagesy($im); $y--;) {
$rgb = imagecolorat($im, $x, $y);
$c = imagecolorsforindex($im, $rgb);
if ($c['red'] == 0 && $c['green'] == 94 && $c['blue'] == 173) {
$colorB = imagecolorallocatealpha($im, 255, 0, 255, $c['alpha']);
imagesetpixel($im, $x, $y, $colorB);
}
}
}
imageAlphaBlending($im, true);
imageSaveAlpha($im, true);
header('Content-Type: image/png');
imagepng($im);
imagedestroy($im);
The problem is the blue color of the banner are multiple different rgb colors so how can i change all blue rgb colors at once without affecting the other colors.
Upvotes: 0
Views: 269
Reputation: 6080
Just define a Threshold that holds for all wanted colors.
Change:
if ($c['red'] == 0 && $c['green'] == 94 && $c['blue'] == 173)
To something like:
if ($c['red'] == 0 && $c['green'] == 94 && $c['blue'] > 173 && $c['blue'] < 240)
Do this for all 3 Channels and Test what color Range fits best.
Upvotes: 2