【问题标题】:Want to use imagecopy or imagecopyresampled with 50% opacity想要使用 50% 不透明度的 imagecopy 或 imagecopyresampled
【发布时间】:2012-11-29 18:19:39
【问题描述】:

我正在使用 PHP GD imagecopyimagecopyresampled 将具有透明度的 PNG 图像合并到另一个 PNG 图像(基本上是创建带有设计的 T 恤模型)。

当我使用 imagecopymerge() 时,它不尊重透明度。但不透明度设置选项有效。但是当我使用imagecopy()imagecopyresampled() 时,透明度可以工作,但不透明度设置不可用。

那么如何合并既尊重透明度又提供 50% 不透明度的图像呢?

我的代码是:

$img1 = imagecreatefrompng('m1.png');
$img2 = imagecreatefrompng('m2.png');

imagealphablending( $img2, false );
imagesavealpha( $img2, true );

$x1 = imagesx($img1);
$y1 = imagesy($img1);
$x2 = imagesx($img2);
$y2 = imagesy($img2);

//imagecopyresampled($img1, $img2, 205, 170, 0, 0, $x2-40, $y2-40, $x2, $y2);
imagecopy($img1, $img2, 205, 170, 0, 0, $x2-40, $y2-40);

header('Content-Type: image/png');
imagepng($img1);

请帮忙。

【问题讨论】:

    标签: php gd


    【解决方案1】:

    你可以看看这个链接:

    https://bugs.php.net/bug.php?id=23815

    imagecopymerge 不支持图像 Alpha。因此,他们要求创建一个新函数 imagecopymergealpha 来完成这种工作。

    https://github.com/php/php-src/pull/211

    【讨论】:

      【解决方案2】:
      imagecopymerge($img1, $img2, 0, 0, 0, 0, $x1, $y1, 50);
      
      header('Content-Type: image/png');
      imagepng($img1);
      

      【讨论】:

        【解决方案3】:

        请使用此示例代码检查

        $imageName = 'path/to/your/image/file'
        $im_src = create_image_from_type($imageName);
        $size = getimagesize($imageName);
        $im_dst = create_image_from_type($imageName);
        $white = imagecolorallocate($im_dst, 255, 255, 255);
        imagecolortransparent($im_dst, $white);
        imagefilledrectangle($im_dst, 0, 0, $size[0], $size[1], $white);
        $opacityVal = 50;// put the opacity value here
        imagecopymerge($im_dst, $im_src, 0, 0, 0, 0,$size[0], $size[1], $opacityVal);
        save_image($im_dst, $imageName, 100);
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2011-08-11
          • 1970-01-01
          • 2013-08-03
          • 2010-10-18
          • 2013-12-15
          • 2010-09-07
          • 1970-01-01
          相关资源
          最近更新 更多