在PHP中使用imagecopymerge在图像上添加一些不透明度
我的问题是: 我想通过在另一个透明图像上复制来更改图像的不透明度 我的代码:在PHP中使用imagecopymerge在图像上添加一些不透明度,php,image,transparency,opacity,Php,Image,Transparency,Opacity,我的问题是: 我想通过在另一个透明图像上复制来更改图像的不透明度 我的代码: $opacity = 50; $transparentImage = imagecreatetruecolor($width, $height); imagesavealpha($transparentImage, true); $transColour = imagecolorallocatealpha($transparentImage , 0, 0, 0, 127); imagefill($transparen
$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时,该图像在我的导航器中是透明的
是否有一种解决方案可以改变我的图像的不透明度,而不将其添加到彩色背景上?似乎图像上的
imagecopymerge
(透明度)通道。幸运的是,您可以使用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);
}
似乎是图像上的
imagecopymerge
(透明)通道。幸运的是,您可以使用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);
}
您好,事实上我想保持$dst_im的透明度,这是透明的,而不仅仅是$src_im的透明度:(您好,事实上我想保持$dst_im的透明度,这是透明的,而不仅仅是$src_im的透明度:(