0

Hey guys i am trying to recolor the following image with php: enter image description here

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.

Community
  • 1
  • 1
Alesfatalis
  • 769
  • 2
  • 13
  • 32

1 Answers1

2

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.

Mailerdaimon
  • 6,003
  • 3
  • 35
  • 46
  • i will try it is it a good way to start with a very high blue channel? – Alesfatalis Oct 29 '13 at 12:36
  • light blue colors will have high blue values, darker blue values however may have lower blue values. But they are higher than the red and green value. Try printing the values of your colors out if you can not guess a good range. – Mailerdaimon Oct 29 '13 at 12:43
  • Yeah i made a loop which outputs all colors now i can see the range and whith that i can use ur solution thanks man. – Alesfatalis Oct 29 '13 at 13:19