使用PHP删除图像背景并保存透明PNG

7

我希望能够在基于PHP平台的网站上删除上传的任何图像的白色背景。上传功能已经完成,但是这个功能却出现了问题。

我在这里找到了一个链接: Remove white background from an image and make it transparent

但这是相反的。我想删除有颜色的背景并使其成为具有透明背景的图像。


4
请说明负投票的原因。 - Harsimran Singh
请详细解释并展示您所做的内容,我不是那个给你点踩的人。 - Rohit Choudhary
7个回答

6

由于您只需要单色透明度,最简单的方法是使用imagecolortransparent()将白色定义为透明。可以尝试使用以下代码(未经测试):

$img = imagecreatefromstring($your_image); //or whatever loading function you need
$white = imagecolorallocate($img, 255, 255, 255);
imagecolortransparent($img, $white);
imagepng($img, $output_file_name);

我尝试了这个代码,但屏幕上显示了不想要的字符:$file = 'itsmehere.png'; // png 图像路径 $img = imagecreatefrompng($file); // 打开图像 $white = imagecolorallocate($img, 255, 255, 255); imagecolortransparent($img, $color); imagepng($img, $output_file_name); - Harsimran Singh
请详细说明“不需要的字符”。 - Maerlyn
这是一个警告(imagefill()获取无效资源),然后是一张PNG图片。 - Maerlyn
但我需要保存PNG图像,但无法获取它。下一步该怎么办? - Harsimran Singh
如果你想保存它,需要将文件名作为第二个参数传递给imagepng。你目前传递的是null,这就是为什么它输出图片而不是保存的原因。 - Maerlyn

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

3
尝试使用ImageMagick,它对我很有帮助。您还可以控制需要删除的颜色量。只需传递图像路径、背景色作为RGB数组和模糊度(以百分比表示)。只要您的系统/主机上安装了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;
        }

1
获取图像中白色的索引并将其设置为透明。
$whiteColorIndex = imagecolorexact($img,255,255,255);
$whiteColor = imagecolorsforindex($img,$whiteColorIndex);
imagecolortransparent($img,$whiteColor);

如果您不知道确切的颜色,可以使用imagecolorclosest()。


1
警告:imagecolortransparent() 函数期望第二个参数为整数,但实际传递了一个数组。如何将整数传递给数组的三个键? - VishalParkash

1

@geoffs3310的函数应该是这里被接受的答案,但请注意,保存的png文件不包含alpha通道。

要删除背景并将新的png文件保存为带有alpha通道的透明png,请使用以下代码

$_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
使用PHP图像处理和GD库,逐像素读取图像,如果RGB分量都是255(像素为白色),则将alpha通道设置为255(透明)。根据上传的文件类型是否支持alpha通道,可能需要更改图像的文件类型。

0

将URL版本转换并返回到页面:

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接