Delete image using php and save transparent png

I want to remove the white background of any image uploaded to a site running on the PHP platform. The download function is running, but messed up with this functionality.

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

But this is done the other way around. I want to remove a colored background and make it with a transparent background.

+5
source share
6 answers

Since you only need transparency with one color, the easiest way is to define white color with imagecolortransparent(). Something like this (unverified 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);
+4
source
function transparent_background($filename, $color) 
{
    $img = imagecreatefrompng('image.png'); //or whatever loading function you need
    $colors = explode(',', $color);
    $remove = imagecolorallocate($img, $colors[0], $colors[1], $colors[2]);
    imagecolortransparent($img, $remove);
    imagepng($img, $_SERVER['DOCUMENT_ROOT'].'/'.$filename);
}

transparent_background('logo_100x100.png', '255,255,255');
+4
source

ImageMagick, . , . , bgcolor, RGB, fuzz . ImageMagick, /. - .

ImageMagick 6.2.8

:

    $image = "/path/to/your/image.jpg";
    $bgcolor = array("red" => "255", "green" => "255", "blue" => "255");
    $fuzz = 9;
    remove_image_background($image, $bgcolor, $fuzz); 

        protected function remove_image_background($image, $bgcolor, $fuzz)
        {
            $image = shell_exec('convert '.$image.' -fuzz '.$fuzz.'% -transparent "rgb('.$bgcolor['red'].','.$bgcolor['green'].','.$bgcolor['blue'].')" '.$image.'');
            return $image;
        }
+3

.

$whiteColorIndex = imagecolorexact($img,255,255,255);
$whiteColor = imagecolorsforindex($img,$whiteColorIndex);
imagecolortransparent($img,$whiteColor);

imagecolorclosest(), .

+1

Use php and GD image processing, read the image pixel by pixel if all RGB components are 255 (pixel white), set the alpha channel to 255 (transparent). You may need to change the image file type depending on whether the downloaded file type supports the alpha channel.

0
source

The function from @ geoffs3310 should be the accepted answer here, but note that the saved png does not contain an alpha channel.

To remove the background and save the new png as a transparent png with alpha, the following code works

$_filename='/home/files/IMAGE.png';
$_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);
0
source

All Articles