php 用php删除图像背景并保存透明png
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10751227/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Remove Image background with php and save transparent png
提问by Harsimran Singh
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.
我想删除在 PHP 平台上工作的网站上上传的任何图像的白色背景。上传功能已完成,但与此功能混淆。
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.
但这是在做相反的事情。我想删除彩色背景并使其成为具有透明背景的图像。
回答by Maerlyn
Since you only need single-color transparency, the easiest way is to define white with imagecolortransparent(). Something like this (untested code):
由于您只需要单色透明度,最简单的方法是用 定义白色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);
回答by geoffs3310
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');
回答by Alex Gurin
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.
试试 ImageMagick 它对我有用。您还可以控制需要去除的颜色量。只需将图像路径、bgcolor 作为 RGB 数组传递,并以百分比表示模糊。只要您在系统/主机上安装了 ImageMagick。我让我的托管服务提供商将它作为模块安装。
I'm using ImageMagick version 6.2.8
我正在使用 ImageMagick 版本 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;
}
回答by paj
The function from @geoffs3310 should be the accepted answer here, but note, that the png saved does not contain an alpha channel.
@geoffs3310 的函数应该是这里接受的答案,但请注意,保存的 png 不包含 alpha 通道。
To remove the background and save the new png as a transparent png with alpha the following code works
要删除背景并将新的 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);
回答by Taha Paksu
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.
如果您不知道确切的颜色,您也可以使用 imagecolorclosest() 。
回答by Matja?
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.
使用php图像处理和GD,如果RGB分量都是255(像素为白色),则逐像素读取图像,设置alpha通道为255(透明)。您可能需要更改图像的文件类型,具体取决于上传的文件类型是否支持 Alpha 通道。

