How to remove a white background image using PHP Code

Possible duplicate:
Translate Ruby into PHP code with the following code

I found very useful Ruby code for removing a white background image.

See the following code: Remove the white background from the image and make it transparent

I tried to translate the code in php. However, I get an undesirable result. This is my first question after the question, can someone please give me some recommendations and forgive my poor English.

function setTransparency($new_image,$image_source) { $transparencyIndex = imagecolortransparent($image_source); $transparencyColor = array('red' => 255, 'green' => 255, 'blue' => 255); if ($transparencyIndex >= 0) { $transparencyColor = imagecolorsforindex($image_source, $transparencyIndex); } $transparencyIndex = imagecolorallocate($new_image, $transparencyColor['red'], $transparencyColor['green'], $transparencyColor['blue']); imagefill($new_image, 0, 0, $transparencyIndex); imagecolortransparent($new_image, $transparencyIndex); } //create image from the source link $image = imagecreatefrompng('http://i.stack.imgur.com/k7E1F.png'); //create image mask layer $new_image = ImageCreateTruecolor(imagesx($image), imagesy($image)); //remove white background setTransparency($new_image,$image); //merge mask with original image source ImageCopyMerge($new_image, $image, 0, 0, 0, 0, imagesx($image), imagesy($image), 100); imagejpeg($new_image, null, 95); 
+4
source share
1 answer

JPEG format does not support transparency. You should consider using png as the output format. Change the last line to:

 imagepng($new_image, null, 9); 
+5
source

All Articles