Harsimran Singh
Harsimran Singh

Reputation: 738

Remove Image background with php and save transparent png

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.

Upvotes: 7

Views: 50517

Answers (7)

Anderson Ferreira
Anderson Ferreira

Reputation: 122

Version to conversion from URL and return on page:

$img = imagecreatefromjpeg('http://mypage.com/image.jpg');

$remove = imagecolorallocate($img, 255, 255, 255); // Define color rgb to remove
imagecolortransparent($img, $remove);

ob_start();
imagepng($img);
$imgData = ob_get_clean();
imagedestroy($img);

$data_img = 'data:image/png;base64,'.base64_encode($imgData);
echo '<body style="background: #f00;"><img src="'.$data_img.'"></body>';

Upvotes: 0

paj
paj

Reputation: 1187

The function from @geoffs3310 should be the accepted answer here, but note, that the png saved 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);

Upvotes: 1

Alex Gurin
Alex Gurin

Reputation: 61

Try ImageMagick it did the trick for me. You can also control the amount of color that needs to be removed. Just pass image path, bgcolor as an array of RGB, and fuzz in percent. As long as you have ImageMagick installed on your system/hosting. I had my hosting provider install it for me as a module.

I'm using ImageMagick version 6.2.8

Example:

    $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;
        }

Upvotes: 3

Maerlyn
Maerlyn

Reputation: 34105

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);

Upvotes: 6

tuscan88
tuscan88

Reputation: 5829

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');

Upvotes: 4

Taha Paksu
Taha Paksu

Reputation: 15616

get the index of white color in the image and set it to transparent.

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

you can alternatively use imagecolorclosest() if you don't know the exact color.

Upvotes: 1

Matjaž
Matjaž

Reputation: 81

Use the php image processing and GD, read the image pixel by pixel if the RGB components are all 255 (The pixel is white), set the alpha channel to 255 (transparent). You may have to change the filetype of the image depending if the uploaded filetype supports an alpha channel.

Upvotes: 0

Related Questions