php如何用GD库读取本地19201080图片使半透明
要使一张图片半透明,可以使用GD库中的imagecopymerge()函数。以下是一个示例代码:
// 读取本地图片
$img = imagecreatefromjpeg('path/to/image.jpg');
// 创建一个与原图一样大小的透明画布
$transparent = imagecreatetruecolor(imagesx($img), imagesy($img));
imagealphablending($transparent, false);
$transparent_color = imagecolorallocatealpha($transparent, 0, 0, 0, 127);
imagefill($transparent, 0, 0, $transparent_color);
imagesavealpha($transparent, true);
// 将原图复制到透明画布上,并设置透明度
imagecopymerge($transparent, $img, 0, 0, 0, 0, imagesx($img), imagesy($img), 50);
// 输出图片
header('Content-Type: image/png');
imagepng($transparent);
imagedestroy($img);
imagedestroy($transparent);
在上面的代码中,我们首先使用imagecreatefromjpeg()函数读取本地图片,然后创建一个与原图一样大小的透明画布。接着,我们使用imagecopymerge()函数将原图复制到透明画布上,并设置透明度为50(可以根据需要调整透明度)。最后,我们使用imagepng()函数将半透明的图片输出到浏览器,并销毁原图和透明画布
原文地址: https://www.cveoy.top/t/topic/c8sl 著作权归作者所有。请勿转载和采集!