I haven’t posted in awhile, but I got a short, article on background removal in images using PHP and the GD image library.
The way I did this is take a specified image and a specified color to look for as well as a difference or a maximum difference in each color allowed. After that it will take the values entered for each of the items just listed and go through each pixel looking for the colors in the range specified. If the pixel color is not within range, it set’s the color to white, or black depending on the color you are looking for. Anyhow here are the functions which do this.
Firstly HexToRGB which will turn an Hexadecimal color value to RGB integer values.
function HexToRGB($hex) {
$hex = str_replace("#", "", $hex);
$colors = array();
if(strlen($hex) == 3) {
$colors['r'] = hexdec(substr($hex, 0, 1) . $r);
$colors['g'] = hexdec(substr($hex, 1, 1) . $g);
$colors['b'] = hexdec(substr($hex, 2, 1) . $b);
}
else if(strlen($hex) == 6) {
$colors['r'] = hexdec(substr($hex, 0, 2));
$colors['g'] = hexdec(substr($hex, 2, 2));
$colors['b'] = hexdec(substr($hex, 4, 2));
}
$colors;
}
Last is the RemoveBackground function, which is the one that does all the hard work. It scans the image and removes unwanted colors. But is quite and simple function. As of right now it just draws the image to the browser as a png, but that is easily modified.
You will have to play around with the difference when calling the RemoveBackground function to get your desired results.
function RemoveBackground($image, $foregroundColor, $difference = 0) {
if(file_exists($image)) {
list($width, $height, $type) = getimagesize($image);
$ext = strtolower(array_pop(explode(".", $image)));
$img = null;
switch($ext) {
case "jpg":
case "jpeg":
$img = imagecreatefromjpeg($image);
break;
case "png":
$img = imagecreatefrompng($image);
break;
case "gif":
$img = imagecreatefromgif($image);
break;
case "bmp":
$img = imagecreatefromwbmp($image);
break;
case "xbm":
$img = imagecreatefromxbm($image);
break;
case "xpm":
$img = imagecreatefromxpm($image);
break;
}
if($img != null) {
list($r, $g, $b) = HexToRGB($foregroundColor);
$color = imagecolorallocate($img, $r, $g, $b);
$white = imagecolorallocate($img, 255, 255, 255);
$black = imagecolorallocate($img, 0, 0, 0);
for($x = 0; $x < $width; $x++) {
for($y = 0; $y < $height; $y++) {
$pixelColor = imagecolorat($img, $x, $y);
$pixelR = ($pixelColor >> 16) & 0xFF;
$pixelG = ($pixelColor >> 8) & 0xFF;
$pixelB = $pixelColor & 0xFF;
if($pixelR < ($r - $difference) || $pixelR > ($r + $difference) ||
$pixelG < ($g - $difference) || $pixelG > ($g + $difference) ||
$pixelB < ($b - $difference) || $pixelB > ($b + $difference)) {
if($color != $white) {
imagesetpixel($img, $x, $y, $white);
}
else {
imagesetpixel($img, $x, $y, $black);
}
}
else {
imagesetpixel($img, $x, $y, $pixelColor);
}
}
}
header("Content-Type: image/png");
imagepng($img, null);
}
}
}
It is a simple as that. Below is a zip file containing the functions above, as well as a test image.
Background Removal (34)



