【问题标题】:Adding some opacity on an image with imagecopymerge in PHP在 PHP 中使用 imagecopymerge 在图像上添加一些不透明度
【发布时间】:2012-03-19 20:47:31
【问题描述】:

这是我的问题:

我想通过将图像复制到另一个透明图像上来更改图像的不透明度。

我的代码:

$opacity = 50;

$transparentImage = imagecreatetruecolor($width, $height);
imagesavealpha($transparentImage, true);
$transColour = imagecolorallocatealpha($transparentImage , 0, 0, 0, 127);
imagefill($transparentImage , 0, 0, $transColour);

imagecopymerge($transparentImage, $image, 0, 0, 0, 0, $width, $height, $opacity);

$image = $transparentImage;

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

通过这样做,当我使用 imagecopymerge 时,$transparentImage 会失去透明度...因此 $image 会合并到黑色图像上...而不是透明图像上!

但是,当我在调用 imagecopymerge 之前显示 $transparentImage 时,图像在我的导航器中是透明的!

有没有办法改变我的图像的不透明度,而不在彩色背景上添加它?

【问题讨论】:

    标签: php image transparency opacity


    【解决方案1】:

    似乎imagecopymergedoes not support the alpha(透明度)通道在图像上。幸运的是,您可以使用imagecopy 的解决方法来正确执行此操作。这是一个执行此操作的函数,取自 php.net 上的 cmets:

    function imagecopymerge_alpha($dst_im, $src_im, $dst_x, $dst_y, $src_x, $src_y, $src_w, $src_h, $pct){
        // creating a cut resource
        $cut = imagecreatetruecolor($src_w, $src_h);
    
        // copying relevant section from background to the cut resource
        imagecopy($cut, $dst_im, 0, 0, $dst_x, $dst_y, $src_w, $src_h);
    
        // copying relevant section from watermark to the cut resource
        imagecopy($cut, $src_im, 0, 0, $src_x, $src_y, $src_w, $src_h);
    
        // insert cut resource to destination image
        imagecopymerge($dst_im, $cut, $dst_x, $dst_y, 0, 0, $src_w, $src_h, $pct);
    } 
    

    【讨论】:

    • 你好,其实我是想保留我的$dst_im的透明度,它是透明的,而不仅仅是$src_im的透明度:(
    猜你喜欢
    • 2012-12-31
    • 2023-03-11
    • 2011-07-28
    • 2021-06-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-11
    相关资源
    最近更新 更多