Colorizing and swapping colors with PHP GD Image Library?
-
23-08-2019 - |
Question
Hello I am in the process of trying to colorize and swap colors on an image using GD image library with PHP.
I am using an original image located here: http://korlon.com/youknowbetter/test.jpg
And wish to get it to a point where it is orange face with black clothes and hair much like you see here: http://youknowdifferent.org/
So far I've used the following code:
<?php
header('Content-type: image/jpeg');
$im = imagecreatefromjpeg('test.jpg');
imagefilter($im, IMG_FILTER_GRAYSCALE);
imagefilter($im, IMG_FILTER_CONTRAST, 255);
imagefilter($im, IMG_FILTER_NEGATE);
imagefilter($im, IMG_FILTER_COLORIZE, 252, 137, 36);
imagejpeg($im);
?>
To get it close but I am still missing the ability to turn all the white shades to black shades. http://korlon.com/youknowbetter/filter.php
I have tried swapping out white for black as instructed in this question here: Can I swap colors in image using GD library in PHP?
However that doesn't seem to work. It doesn't actually swap out the colors. Is that because I am using a jpg and not a gif? Is there something I need to do with the color palette?
Thanks, any help would be greatly appreciated!
STOOB
Solution
use Blue (#0276DB) instead of orange, and then inverse the image (using IMAGE_FILTER_NEGATE
) to get orange and black.
So, your code will be:
<?php
header('Content-type: image/jpeg');
$im = imagecreatefromjpeg('test.jpg');
imagefilter($im, IMG_FILTER_GRAYSCALE);
imagefilter($im, IMG_FILTER_CONTRAST, 255);
imagefilter($im, IMG_FILTER_NEGATE);
imagefilter($im, IMG_FILTER_COLORIZE, 2, 118, 219);
imagefilter($im, IMG_FILTER_NEGATE);
imagejpeg($im);
?>