这是我的问题:
我想通过将图像复制到另一个透明图像来更改图像的不透明度。
我的代码:
$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时,我的导航器中的图像是透明的!
是否有解决方案可以更改我的图像的不透明度,而无需将其添加到彩色背景上?
答案 0 :(得分:1)
图片上似乎有imagecopymerge
does not support the alpha(透明度)频道。幸运的是,您可以使用imagecopy
的变通方法来正确执行此操作。这是一个功能,取自php.net上的评论:
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);
}