【问题标题】:How can I set the transparancy to image for particular background color in image - using PHP - GD如何为图像中特定背景颜色设置图像的透明度 - 使用 PHP - GD
【发布时间】:2012-04-19 08:25:10
【问题描述】:

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

我想要做的是使用 PHP GD 使背景图像透明。所以请让我知道我怎样才能做到这一点?示例图像显示在这里。我希望黄色是透明的。

【问题讨论】:

    标签: php gd php-5.3


    【解决方案1】:

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

    // $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);
    

    请注意,上面的代码未经测试,我只是在 stackoverflow 中输入了它,所以我可能有一些迟缓的拼写错误,但它应该在最小的点上让你朝着正确的方向前进。

    【讨论】:

    • 如果您发现任何语法错误,请告诉我并编辑源代码,以便以后对其他人有用。一定要弄乱 MAX_DIFFERENCE。您可以使用的其他增强功能是您可以将像素与当前像素的左/右/上/下进行比较,以查看它们与 MAX_DIFFERENCE 的接近程度,从而在不将 M_D 变量调得太高的情况下获得更好的精度。您还可以使用梯度函数线 sin/cos,而不是像我上面编程的那样使用二进制 ON/OFF。欢迎来到图像编辑的世界 =] !
    • 我现在正在看它,看看它可以如何改进..到目前为止..这是一项做得很好的工作......
    • 效果很好..我找到了前进的方向..谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-12-02
    • 1970-01-01
    • 2011-07-06
    • 2017-08-11
    • 1970-01-01
    • 1970-01-01
    • 2021-10-26
    相关资源
    最近更新 更多