Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove Image background with php and save transparent png

Tags:

php

I want to remove the white background of any image uploaded on the site working on PHP platform. The uploading function is done but messed up with this functionality.

Here is the link I found here: Remove white background from an image and make it transparent

But this is doing reverse. I want to remove the colored background and make it image with transparent background.

like image 819
Harsimran Singh Avatar asked May 25 '12 08:05

Harsimran Singh


People also ask

How do I save a PNG with a transparent background?

On the opened Save for Web box, from the right section, click to select PNG-24 option from the Settings drop-down list. Check the Transparency checkbox. Finally click the Save button to save the image with the transparent background.

How do you save an image so the background is transparent?

Select the > Save As option. From the Format options, choose either TIFF, PNG, or GIF. If you've picked the GIF or TIFF format, make sure to check the > Save Transparency box on the bottom left.

How do I make the background of a PNG transparent in HTML?

Transparency is not done in HTML, but is a part of the image itself. The browser will see the image as a PNG and display it as a PNG automatically. To add transparency to the image, you will have to edit the file with a graphics editor like Photoshop. Save this answer.


1 Answers

Since you only need single-color transparency, the easiest way is to define white with imagecolortransparent(). Something like this (untested code):

$img = imagecreatefromstring($your_image); //or whatever loading function you need
$white = imagecolorallocate($img, 255, 255, 255);
imagecolortransparent($img, $white);
imagepng($img, $output_file_name);
like image 52
Maerlyn Avatar answered Sep 19 '22 17:09

Maerlyn