PHP - Replace Colour Within Image

Replace a color with another in an image with PHP

If you meant using GD library in PHP, you should give a check on imagefilter()

Steps are:

  • Start with a .PNG image, use white for inner, alpha for outer.
  • Use imagefilter($img, IMG_FILTER_COLORIZE, 0, 255, 0)) Where 0,255,0 is your RGB color (bright green in this example)
  • Save the alpha and print out result.

Edit, Working code and clarification.

I meant, using alpha for OUTER of the black lines, and white INSIDE. Here's the sample image:
WhiteInAlphaOut

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.

PHP - Replace colour within image

You need to open the input file and scan each pixel to check for your chromokey value.

Something like this:

// Open input and output image
$src = imagecreatefromJPEG('input.jpg') or die('Problem with source');
$out = ImageCreateTrueColor(imagesx($src),imagesy($src)) or die('Problem In Creating image');

// scan image pixels
for ($x = 0; $x < imagesx($src); $x++) {
for ($y = 0; $y < imagesy($src); $y++) {
$src_pix = imagecolorat($src,$x,$y);
$src_pix_array = rgb_to_array($src_pix);

// check for chromakey color
if ($src_pix_array[0] == 0 && $src_pix_array[1] == 0 && $src_pix_array[2] == 255) {
$src_pix_array[2] = 254;
}

imagesetpixel($out, $x, $y, imagecolorallocate($out, $src_pix_array[0], $src_pix_array[1], $src_pix_array[2]));
}
}

// write $out to disc

imagejpeg($out, 'output.jpg',100) or die('Problem saving output image');
imagedestroy($out);

// split rgb to components
function rgb_to_array($rgb) {
$a[0] = ($rgb >> 16) & 0xFF;
$a[1] = ($rgb >> 8) & 0xFF;
$a[2] = $rgb & 0xFF;

return $a;
}

Replace a color with another color in an image with PHP

EDIT 2 :
You might need to optimize something and change hueAbsoluteError to suit your needs, but hue is the way to enlightenment and sharper picture quality (functions taken from https://gist.github.com/brandonheyer/5254516):

<?php
function RGBtoHSL( $r, $g, $b ) {
$r /= 255;
$g /= 255;
$b /= 255;
$max = max( $r, $g, $b );
$min = min( $r, $g, $b );
$l = ( $max + $min ) / 2;
$d = $max - $min;
if( $d == 0 ){
$h = $s = 0;
} else {
$s = $d / ( 1 - abs( 2 * $l - 1 ) );
switch( $max ){
case $r:
$h = 60 * fmod( ( ( $g - $b ) / $d ), 6 );
if ($b > $g) {
$h += 360;
}
break;
case $g:
$h = 60 * ( ( $b - $r ) / $d + 2 );
break;
case $b:
$h = 60 * ( ( $r - $g ) / $d + 4 );
break;
}
}
return array( round( $h, 2 ), round( $s, 2 ), round( $l, 2 ) );
}

function HSLtoRGB( $h, $s, $l ){
$c = ( 1 - abs( 2 * $l - 1 ) ) * $s;
$x = $c * ( 1 - abs( fmod( ( $h / 60 ), 2 ) - 1 ) );
$m = $l - ( $c / 2 );
if ( $h < 60 ) {
$r = $c;
$g = $x;
$b = 0;
} else if ( $h < 120 ) {
$r = $x;
$g = $c;
$b = 0;
} else if ( $h < 180 ) {
$r = 0;
$g = $c;
$b = $x;
} else if ( $h < 240 ) {
$r = 0;
$g = $x;
$b = $c;
} else if ( $h < 300 ) {
$r = $x;
$g = 0;
$b = $c;
} else {
$r = $c;
$g = 0;
$b = $x;
}
$r = ( $r + $m ) * 255;
$g = ( $g + $m ) * 255;
$b = ( $b + $m ) * 255;
return array( floor( $r ), floor( $g ), floor( $b ) );
}

/* ---------------CHANGE THESE------------------- */
$colorToReplace = RGBtoHSL(255, 0, 255);
$hueAbsoluteError = 0.4;
$replacementColor = RGBtoHSL(0, 192, 239);
/* ---------------------------------------------- */

$filename = 'img/Mascots_Aviators_General-copy.png';
$im = imagecreatefrompng($filename);
$out = imagecreatetruecolor(imagesx($im), imagesy($im));
$transColor = imagecolorallocatealpha($out, 254, 254, 254, 127);
imagefill($out, 0, 0, $transColor);

for ($x = 0; $x < imagesx($im); $x++) {
for ($y = 0; $y < imagesy($im); $y++) {
$pixel = imagecolorat($im, $x, $y);

$red = ($pixel >> 16) & 0xFF;
$green = ($pixel >> 8) & 0xFF;
$blue = $pixel & 0xFF;
$alpha = ($pixel & 0x7F000000) >> 24;

$colorHSL = RGBtoHSL($red, $green, $blue);

if ((($colorHSL[0] >= $colorToReplace[0] - $hueAbsoluteError) && ($colorToReplace[0] + $hueAbsoluteError) >= $colorHSL[0])){
$color = HSLtoRGB($replacementColor[0], $replacementColor[1], $colorHSL[2]);
$red = $color[0];
$green= $color[1];
$blue = $color[2];
}

if ($alpha == 127) {
imagesetpixel($out, $x, $y, $transColor);
}
else {
imagesetpixel($out, $x, $y, imagecolorallocatealpha($out, $red, $green, $blue, $alpha));
}
}
}
imagecolortransparent($out, $transColor);
imagesavealpha($out, TRUE);
header('Content-type: image/png');
imagepng($out);

Sample Image

EDIT :
Better solution - determine if color needs replacement (using this method). Determine replaced color's hue (I have no idea if it's correct term, what I mean is lightness and darkness). Apply it to replacement color to give it a shade or AA feeling.


So, as I have said in my comment, you need to determine if this color is really ping (dark, light, etc.). Easiest solution is to apply absolute error method for specific color channels. There may be (there definitely is) better universal method, but I hope this will do:

$color = [255, 0, 255];
$colorAbsoluteError = [150, 0, 150];
$replacementColor = [0, 192, 239];
$filename = 'img/Mascots_Aviators_General-copy.png';
$im = imagecreatefrompng($filename);
$out = imagecreatetruecolor(imagesx($im), imagesy($im));
$transColor = imagecolorallocatealpha($out, 254, 254, 254, 127);
imagefill($out, 0, 0, $transColor);

for ($x = 0; $x < imagesx($im); $x++) {
for ($y = 0; $y < imagesy($im); $y++) {
$pixel = imagecolorat($im, $x, $y);

$red = ($pixel >> 16) & 0xFF;
$green = ($pixel >> 8) & 0xFF;
$blue = $pixel & 0xFF;
$alpha = ($pixel & 0x7F000000) >> 24;

if ((($red >= $color[0] - $colorAbsoluteError[0]) && ($color[0] + $colorAbsoluteError[0]) >= $red) &&
(($green >= $color[1] - $colorAbsoluteError[1]) && ($color[1] + $colorAbsoluteError[1]) >= $green) &&
(($blue >= $color[2] - $colorAbsoluteError[2]) && ($color[2] + $colorAbsoluteError[2]) >= $blue)){
$red = $replacementColor[0];
$green= $replacementColor[1];
$blue = $replacementColor[2];
}

if ($alpha == 127) {
imagesetpixel($out, $x, $y, $transColor);
}
else {
imagesetpixel($out, $x, $y, imagecolorallocatealpha($out, $red, $green, $blue, $alpha));
}
}
}
imagecolortransparent($out, $transColor);
imagesavealpha($out, TRUE);
header('Content-type: image/png');
imagepng($out);

Sample Image

Replace the specific RGB color with another image using PHP

You can just read two images, source and background, and take pixels from background image and set to the source.

Below is the last part of your code above which shows this idea:

$filename = 'images/01.png';
$bgFilename = 'images/background.png';
$im = imagecreatefrompng($filename);
$bg = imagecreatefrompng($bgFilename);
$out = imagecreatetruecolor(imagesx($im), imagesy($im));
$transColor = imagecolorallocatealpha($out, 254, 254, 254, 127);
imagefill($out, 0, 0, $transColor);

for ($x = 0; $x < imagesx($im); $x++) {
for ($y = 0; $y < imagesy($im); $y++) {
$pixel = imagecolorat($im, $x, $y);
$bgPixel = imagecolorat($bg, $x, $y);

$red = ($pixel >> 16) & 0xFF;
$green = ($pixel >> 8) & 0xFF;
$blue = $pixel & 0xFF;
$alpha = ($pixel & 0x7F000000) >> 24;
$colorHSL = RGBtoHSL($red, $green, $blue);

if ((($colorHSL[0] >= $colorToReplace[0] - $hueAbsoluteError) && ($colorToReplace[0] + $hueAbsoluteError) >= $colorHSL[0])){
// Instead of taking the replacementColor
/* $color = HSLtoRGB($replacementColor[0], $replacementColor[1], $colorHSL[2]); */
/* $red = $color[0]; */
/* $green= $color[1]; */
/* $blue = $color[2]; */
// We just take colors from the backround image pixel
$red = ($bgPixel >> 16) & 0xFF;
$green = ($bgPixel >> 8) & 0xFF;
$blue = $bgPixel & 0xFF;
}

if ($alpha == 127) {
imagesetpixel($out, $x, $y, $transColor);
}
else {
imagesetpixel($out, $x, $y, imagecolorallocatealpha($out, $red, $green, $blue, $alpha));
}
}
}
imagecolortransparent($out, $transColor);
imagesavealpha($out, TRUE);
header('Content-type: image/png');
imagepng($out);

The result can look like this:

Sample Image

PHP Replace Colour in truecolorimage

Use imagetruecolortopalette to create a palette image, then you can use imagecolorexact to find the index of the color you're looking for and then call imagecolorset to change it.

How can I replace one color with another in a png 24 alpha transparent image with GD

You can try the following:

  • cycle all points
  • get the color of that point
  • if it matches your colorA, set that pixel to the desired colorB

Code:

for ($x=imagesx($im); $x--; ) {
for ($y=imagesy($im); $y--; ) {
$c = imagecolorat($im, $x, $y);
if ($c[0] == 0 && $c[1] == 0 && $c[2] == 0) {
// here we use the new color, but the original alpha channel
$colorB = imagecolorallocatealpha($im, 255, 0, 255, $c[3]);
imagesetpixel($im, $x, $y, $colorB);
}
}
}

Hope this helps!

Php color replace on image

Because in the original image the letters have a border that it is not a perfect black, so the color is not

R = 0
G = 0
B = 0

I have change a little bit your original code to avoid unnecessary steps. Is this what do you need?

<?php
ob_start();
$txtcolor="20FF00";
$r1txt=hexdec(substr($txtcolor,0,2));
$g1txt=hexdec(substr($txtcolor,2,2));
$b1txt=hexdec(substr($txtcolor,4,2));

$backcolor="FF0ED9";
$r1back=hexdec(substr($backcolor,0,2));
$g1back=hexdec(substr($backcolor,2,2));
$b1back=hexdec(substr($backcolor,4,2));
$imgname="demo_the-crown-prints_work-hard_5x7.jpg";

$gd = imagecreatefromjpeg($imgname);
$w = imagesx($gd);
$h = imagesy($gd);
for($x=0;$x<$w;$x++)
{
for($y=0;$y<$h;$y++)
{
$rgb = imagecolorat($gd, $x, $y);
$r = ($rgb >> 16) & 0xFF;
$g = ($rgb >> 8) & 0xFF;
$b = $rgb & 0xFF;

if ($r>200 && $g>200 && $b>200)
{
$pixelColor=imagecolorallocate($gd,$r1back,$g1back,$b1back);
imagesetpixel($gd,$x,$y,$pixelColor);
} else {
$pixelColor=imagecolorallocate($gd,$r1txt,$g1txt,$b1txt);
imagesetpixel($gd,$x,$y,$pixelColor);
}
}
}

imagejpeg($gd,'simpletext.jpg', 100);
imagepng($gd,'simpletext.png', 0);
?>

I have saved the result on files to let you see the difference of quality between the imagejpeg and imagepng

Change the colour of an image with PHP

Here's a simple GD example in PHP that might help you get started. Viewed in a browser it'll show you a red version of that image. You'll need to have the original PNG file in the same directory.

Bear in mind that GD and other image manipulation (especially on transparent images) are really quite complicated subjects; there's a lot to learn and going through a few basic tutorials, even if they don't seem to be directly related to what you want to achieve, might really help your understanding.

<?php

// Load the image into $img
$img = imagecreatefrompng('internt_web_technology-08-512.png');

// Because your chosen image has transparency, we need to make sure
// we save the alpha channel information.
imageSaveAlpha($img, true);

// Filter the image to mid-red (RGB values 127, 0, 0) using the
// "colourize" filter. This works nicely with your chosen image
// because it's a shape with a uniform dark colour.
imagefilter($img, IMG_FILTER_COLORIZE, 127, 0, 0);

// Make sure our content type is PNG, otherwise it'll just
// display the image data as text.
header('Content-Type: image/png');

// Output to browser.
imagepng($img);

Example input:

Small version of example image for posterity

Example output:

Output of my example script



Related Topics



Leave a reply



Submit