7

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.

Community
  • 1
  • 1
Harsimran Singh
  • 698
  • 4
  • 13
  • 29

7 Answers7

6

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);
Maerlyn
  • 33,687
  • 18
  • 94
  • 85
  • I tried dis but showd unwanted characters on the screen: $file = 'itsmehere.png'; // path to png image $img = imagecreatefrompng($file); // open image $white = imagecolorallocate($img, 255, 255, 255); imagecolortransparent($img, $color); imagepng($img, $output_file_name); – Harsimran Singh May 25 '12 at 09:36
  • Please elaborate on 'unwanted characters'. – Maerlyn May 25 '12 at 19:17
  • It's a warning (that imagefill() gets an invalid resource), then a PNG image. – Maerlyn May 28 '12 at 08:25
  • but i need to save the png image. and not able to get it. What to do next? – Harsimran Singh May 28 '12 at 11:10
  • If you want to save it, you need to pass a filename to imagepng as the second argument. You currently pass null, that's why it outputs the picture instead. – Maerlyn May 28 '12 at 11:38
4
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');
Community
  • 1
  • 1
geoffs3310
  • 5,599
  • 11
  • 51
  • 104
3

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;
        }
Alex Gurin
  • 61
  • 3
1

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.

Taha Paksu
  • 15,371
  • 2
  • 44
  • 78
  • 1
    Warning: imagecolortransparent() expects parameter 2 to be integer, array given. How to pass integer for 3 keys of an array? – VishalParkash Dec 02 '19 at 12:29
1

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);
paj
  • 1,177
  • 16
  • 33
0

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.

Matjaž
  • 81
  • 4
0

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