I want to remove background color of an image. But it's not working properly. My code can remove background color partially.
Here is my code :
function transparent_background($filename)
{
$output_file_name = $_SERVER['DOCUMENT_ROOT'].'/test/removeBackgroundColor/'.rand(100,200).'.png';
$_filename= $_SERVER['DOCUMENT_ROOT'].'/test/removeBackgroundColor/'.$filename;
$_backgroundColour='0,0,0';
$imageData = file_get_contents($_filename);
$_img = imagecreatefromstring($imageData);
$_backgroundColours = explode(',', $_backgroundColour);
$_removeColour = imagecolorallocate($_img, (int)$_backgroundColours[0], (int)$_backgroundColours[1], (int)$_backgroundColours[2]);
imagecolortransparent($_img, $_removeColour);
imagesavealpha($_img, true);
$_transColor = imagecolorallocatealpha($_img, 0, 0, 0, 127);
imagefill($_img, 0, 0, $_transColor);
imagesavealpha($_img, true);
imagepng($_img, $output_file_name);
echo $output_file_name;
}
transparent_background('6.png');
I have passed this image as input : http://prntscr.com/nldb94
But it return this : http://prntscr.com/nldbg4
I want to remove this section's color also : http://prntscr.com/nldbr2
Can you please help me to figure out the solution ? Thanks in advance
$_transColor = imagecolorallocatealpha($_img, 0, 0, 0, 127);
imagefill($_img, 0, 0, $_transColor);
imagesavealpha($_img, true);
imagepng($_img, $output_file_name);
Use imagecolortransparent($_img, $_removeColour); in the above code.
So please try including the imagecolortransparent code in the second image code also.