Remove Image background with php and save transparent png

后端 未结 7 1239
心在旅途
心在旅途 2020-12-15 01:23

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.

相关标签:
7条回答
  • 2020-12-15 02:25

    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;
            }
    
    0 讨论(0)
提交回复
热议问题