Skip to content


Simple background removal with PHP and GD.

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.

  1. function HexToRGB($hex) {
  2.         $hex = str_replace("#", "", $hex);
  3.         $colors = array();
  4.        
  5.         if(strlen($hex) == 3) {
  6.             $colors['r'] = hexdec(substr($hex, 0, 1) . $r);
  7.             $colors['g'] = hexdec(substr($hex, 1, 1) . $g);
  8.             $colors['b'] = hexdec(substr($hex, 2, 1) . $b);
  9.         }
  10.         else if(strlen($hex) == 6) {
  11.             $colors['r'] = hexdec(substr($hex, 0, 2));
  12.             $colors['g'] = hexdec(substr($hex, 2, 2));
  13.             $colors['b'] = hexdec(substr($hex, 4, 2));
  14.         }
  15.         $colors;
  16.     }

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.

  1. function RemoveBackground($image, $foregroundColor, $difference = 0) {
  2.         if(file_exists($image)) {
  3.             list($width, $height, $type) = getimagesize($image);
  4.             $ext = strtolower(array_pop(explode(".", $image)));
  5.             $img = null;
  6.            
  7.             switch($ext) {
  8.                 case "jpg":
  9.                 case "jpeg":
  10.                     $img = imagecreatefromjpeg($image);
  11.                     break;
  12.                 case "png":
  13.                     $img = imagecreatefrompng($image);
  14.                     break;
  15.                 case "gif":
  16.                     $img = imagecreatefromgif($image);
  17.                     break;
  18.                 case "bmp":
  19.                     $img = imagecreatefromwbmp($image);
  20.                     break;
  21.                 case "xbm":
  22.                     $img = imagecreatefromxbm($image);
  23.                     break;
  24.                 case "xpm":
  25.                     $img = imagecreatefromxpm($image);
  26.                     break;
  27.             }
  28.            
  29.             if($img != null) {
  30.                 list($r, $g, $b) = HexToRGB($foregroundColor);
  31.                 $color = imagecolorallocate($img, $r, $g, $b);
  32.                 $white = imagecolorallocate($img, 255, 255, 255);
  33.                 $black = imagecolorallocate($img, 0, 0, 0);
  34.                
  35.                 for($x = 0; $x < $width; $x++) {
  36.                     for($y = 0; $y < $height; $y++) {
  37.                         $pixelColor = imagecolorat($img, $x, $y);
  38.                         $pixelR = ($pixelColor >> 16) & 0xFF;
  39.                         $pixelG = ($pixelColor >> 8) & 0xFF;
  40.                         $pixelB = $pixelColor & 0xFF;
  41.                        
  42.                         if($pixelR < ($r - $difference) || $pixelR > ($r + $difference) ||
  43.                             $pixelG < ($g - $difference) || $pixelG > ($g + $difference) ||
  44.                             $pixelB < ($b - $difference) || $pixelB > ($b + $difference)) {
  45.                             if($color != $white) {
  46.                                 imagesetpixel($img, $x, $y, $white);
  47.                             }
  48.                             else {
  49.                                 imagesetpixel($img, $x, $y, $black);
  50.                             }
  51.                         }
  52.                         else {
  53.                             imagesetpixel($img, $x, $y, $pixelColor);
  54.                         }
  55.                     }
  56.                 }
  57.                 header("Content-Type: image/png");
  58.                 imagepng($img, null);
  59.             }
  60.         }
  61.     }

It is a simple as that. Below is a zip file containing the functions above, as well as a test image.
Background Removal (116)

Posted in PHP, Software, code..

0 Responses

Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.

Some HTML is OK

(required)

(required, but never shared)

or, reply to this post via trackback.