如何使用PHP-GD为图像中的特定背景色设置图像透明度

如何使用PHP-GD为图像中的特定背景色设置图像透明度,php,gd,php-5.3,Php,Gd,Php 5.3,假设我有任何图像(比如护照类型的图像,用户周围的背景相同) 我想做的是使用PHPGD使背景图像透明。所以,请让我知道我如何才能做到这一点?示例图像如下所示。我希望黄色是透明的。 基本上,您要做的是替换与背景颜色“相近”的颜色。“接近”是指与之相似的颜色: // $src, $dst...... $src = imagecreatefromjpeg("dmvpic.jpg"); // Alter this by experimentation define( "MAX_DIFFERENCE",

假设我有任何图像(比如护照类型的图像,用户周围的背景相同)

我想做的是使用PHPGD使背景图像透明。所以,请让我知道我如何才能做到这一点?示例图像如下所示。我希望黄色是透明的。

基本上,您要做的是替换与背景颜色“相近”的颜色。“接近”是指与之相似的颜色:

// $src, $dst......
$src = imagecreatefromjpeg("dmvpic.jpg");

// Alter this by experimentation
define( "MAX_DIFFERENCE", 20 );

// How 'far' two colors are from one another / color similarity.
// Since we aren't doing any real calculations with this except comparison,
// you can get better speeds by removing the sqrt and just using the squared portion.
// There may even be a php gd function that compares colors already. Anyone?
function dist($r,$g,$b) {
   global $rt, $gt, $bt;
   return sqrt( ($r-$rt)*($r-$rt) + ($g-$gt)*($g-$gt) + ($b-$bt)*($b-$bt) );
}

// Alpha color (to be replaced) is defined dynamically as 
// the color at the top left corner...
$src_color = imagecolorat( $src ,0,0 );
$rt = ($src_color >> 16) & 0xFF;
$gt = ($src_color >> 8) & 0xFF;
$bt = $src_color & 0xFF;

// Get source image dimensions and create an alpha enabled destination image
$width = imagesx($src);
$height = imagesy($src);
$dst = =imagecreatetruecolor( $width, $height ); 
imagealphablending($dst, true);
imagesavealpha($dst, true);

// Fill the destination with transparent pixels
$trans = imagecolorallocatealpha( $dst, 0,0,0, 127 ); // our transparent color
imagefill( $dst, 0, 0, $transparent ); 

// Here we examine every pixel in the source image; Only pixels that are
// too dissimilar from our 'alhpa' or transparent background color are copied
// over to the destination image.
for( $x=0; $x<$width; ++$x ) {
  for( $y=0; $y<$height; ++$y ) {
     $rgb = imagecolorat($src, $x, $y);
     $r = ($rgb >> 16) & 0xFF;
     $g = ($rgb >> 8) & 0xFF;
     $b = $rgb & 0xFF;

     if( dist($r,$g,$b) > MAX_DIFFERENCE ) {
        // Plot the (existing) color, in the new image
        $newcolor = imagecolorallocatealpha( $dst, $r,$g,$b, 0 );
        imagesetpixel( $dst, $x, $y, $newcolor );
     }
  }
}

header('Content-Type: image/png');
imagepng($dst);
imagedestroy($dst);
imagedestroy($src);
/$src,$dst。。。。。。
$src=imagecreatefromjpeg(“dmvpic.jpg”);
//通过实验改变这一点
定义(“最大差值”,20);
//两种颜色之间的距离/颜色相似性。
//因为除了比较,我们没有做任何实际的计算,
//通过移除sqrt并仅使用平方部分,可以获得更好的速度。
//甚至可能有一个php gd函数已经可以比较颜色了。任何人
功能区($r、$g、$b){
全球$rt、$gt、$bt;
返回sqrt($r-$rt)*($r-$rt)+($g-$gt)*($g-$gt)+($b-$bt)*($b-$bt));
}
//Alpha颜色(待替换)动态定义为
//左上角的颜色。。。
$src_color=imagecolorat($src,0,0);
$rt=($src_color>>16)和0xFF;
$gt=($src_color>>8)&0xFF;
$bt=$src_颜色&0xFF;
//获取源图像尺寸并创建启用alpha的目标图像
$width=imagesx($src);
$height=imagesy($src);
$dst==ImageCreateTureColor($width,$height);
imagealphablending($dst,true);
imagesavealpha($dst,true);
//用透明像素填充目标
$trans=imagecolorallocatealpha($dst,0,0,0127);//我们的透明色
imagefill($dst,0,0,$transparent);
//这里我们检查源图像中的每个像素;只有像素是
//与我们的“alhpa”或透明背景颜色太不相同,因此被复制
//转到目标图像。
对于($x=0;$x 16)&0xFF;
$g=($rgb>>8)和0xFF;
$b=$rgb&0xFF;
if(距离($r,$g,$b)>最大差值){
//在新图像中打印(现有)颜色
$newcolor=imagecolorallocatealpha($dst、$r、$g、$b,0);
imagesetpixel($dst、$x、$y、$newcolor);
}
}
}
标题('Content-Type:image/png');
图像PNG(dst);
图像销毁($dst);
(1)(src);

请注意,上面的代码未经测试,我只是在stackoverflow中键入了它,因此可能会有一些拼写错误,但它应该在最短的时间内为您指出正确的方向。

如果您发现任何语法错误,请告诉我,我将编辑源代码,以便它也对其他人有用。一定要搞乱最大的区别。您可以使用的其他增强功能是,您可以将像素与当前像素的左/右/上/下进行比较,以查看它们与最大差值的接近程度,从而在不将M_D变量调高的情况下获得更好的精度。您还可以使用梯度函数行sin/cos,而不是我上面编程的二进制开/关。欢迎来到图像编辑的世界=]!我现在正在研究如何改进它。。目前为止这项工作做得很好……工作做得很好……我找到了前进的方向……谢谢!