Reputation: 167
I m trying to remove background from an image and make it transparent. Searched a lot, and found some great answers from SO. Unfortunately, my skills in PHP are limited.
Here's the code I tried.
$_filename='images-main/images/IM-Chrome Wallpaper.jpg';
$_backgroundColour='0,0,0';
$_img = imagecreatefrompng($_filename);
$_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);
imagepng($_img, $_filename);
I couldn't able to test this as I have no idea how to echo the image that got transparent from the code (if it did).
Can anyone share their expertise on how to test this code and maybe point me in a direction where I can learn to write code that uses no plugins to make an image transparent?
Thanks
Upvotes: 0
Views: 559
Reputation: 53
Since you only need single-color transparency, the easiest way is to define white with imagecolortransparent()
$img = imagecreatefromstring($your_image);
$white = imagecolorallocate($img, 255, 255, 255);
imagecolortransparent($img, $white);
header('Content-Type: image/png');
imagepng($img, $file_name);
imagedestroy($img);
Upvotes: 1