1

Suppose I have any image(say passport type images where the background will be same around the user).

What I want to do is make that background image as transparent using PHP GD. So please let me know how can I achieve this? The example image is shown here. I want yellow color to be transparent. enter image description here

Madan
  • 691
  • 5
  • 20

1 Answers1

2

What you basically want to do is replace colors 'close' to your background color. By 'close' I mean colors that are similar to it:

// $src, $dst......
$src = imagecreatefromjpeg("dmvpic.jpg");

// Alter this by experimentation
define( "MAX_DIFFERENCE", 20 );

// How 'far' two colors are from one another / color similarity.
// Since we aren't doing any real calculations with this except comparison,
// you can get better speeds by removing the sqrt and just using the squared portion.
// There may even be a php gd function that compares colors already. Anyone?
function dist($r,$g,$b) {
   global $rt, $gt, $bt;
   return sqrt( ($r-$rt)*($r-$rt) + ($g-$gt)*($g-$gt) + ($b-$bt)*($b-$bt) );
}

// Alpha color (to be replaced) is defined dynamically as 
// the color at the top left corner...
$src_color = imagecolorat( $src ,0,0 );
$rt = ($src_color >> 16) & 0xFF;
$gt = ($src_color >> 8) & 0xFF;
$bt = $src_color & 0xFF;

// Get source image dimensions and create an alpha enabled destination image
$width = imagesx($src);
$height = imagesy($src);
$dst = =imagecreatetruecolor( $width, $height ); 
imagealphablending($dst, true);
imagesavealpha($dst, true);

// Fill the destination with transparent pixels
$trans = imagecolorallocatealpha( $dst, 0,0,0, 127 ); // our transparent color
imagefill( $dst, 0, 0, $transparent ); 

// Here we examine every pixel in the source image; Only pixels that are
// too dissimilar from our 'alhpa' or transparent background color are copied
// over to the destination image.
for( $x=0; $x<$width; ++$x ) {
  for( $y=0; $y<$height; ++$y ) {
     $rgb = imagecolorat($src, $x, $y);
     $r = ($rgb >> 16) & 0xFF;
     $g = ($rgb >> 8) & 0xFF;
     $b = $rgb & 0xFF;

     if( dist($r,$g,$b) > MAX_DIFFERENCE ) {
        // Plot the (existing) color, in the new image
        $newcolor = imagecolorallocatealpha( $dst, $r,$g,$b, 0 );
        imagesetpixel( $dst, $x, $y, $newcolor );
     }
  }
}

header('Content-Type: image/png');
imagepng($dst);
imagedestroy($dst);
imagedestroy($src);

Please note above code is untested, I just typed it in stackoverflow so I may have some retarded spelling errors, but it should at bare minimal point you in the right direction.

Authman Apatira
  • 3,994
  • 1
  • 26
  • 33
  • If you find any syntax errors, let me know and ill edit the source so that it'll be useful to others down the line too. Be sure to mess around with MAX_DIFFERENCE. Other enhancements you can use is you can compare pixels to the left/right/top/bottom of the current pixel to see how close they are to MAX_DIFFERENCE as well, to get better accuracy without turning up the M_D variable too high. You can also utilize a gradient function line sin/cos instead of a binary ON/OFF the way I programmed it above. Welcome to the world of image editing =] ! – Authman Apatira Apr 05 '12 at 15:44
  • Am looking at it right now to see how it can be improved .. so far .. the is a JOB well done ...... – Baba Apr 05 '12 at 15:52
  • Thats working quite good..i got the direction to move ahead..thanks! – Madan Apr 06 '12 at 06:54