【发布时间】:2010-12-14 21:00:08
【问题描述】:
在 PHP 中使用 imagepng() 函数时,如何确保我保存的图像以透明背景保存?
【问题讨论】:
在 PHP 中使用 imagepng() 函数时,如何确保我保存的图像以透明背景保存?
【问题讨论】:
只需这样做:
imagealphablending($img, false);
imagesavealpha($img, true);
输出前。确保所有源文件(如果您使用任何源文件)都设置为具有透明度的 32 位 PNG - 如果不是,输出可能与黑色背景不同或透明度不符合要求。
【讨论】:
有一个名为imagecolortransparent 的函数可让您设置透明的颜色。我不知道这是否回答了你的问题。
【讨论】:
这是imagecolortransparent 函数的示例(如果有帮助):
<?php
// Create a 55x30 image
$im = imagecreatetruecolor(55, 30);
$red = imagecolorallocate($im, 255, 0, 0);
$black = imagecolorallocate($im, 0, 0, 0);
// Make the background transparent
imagecolortransparent($im, $black);
// Draw a red rectangle
imagefilledrectangle($im, 4, 4, 50, 25, $red);
// Save the image
imagepng($im, './imagecolortransparent.png');
imagedestroy($im);
?>
【讨论】:
这是一个例子
$newimage = imagecreatetruecolor($dst_w, $dst_h);
imagealphablending($newimage, false);
imagesavealpha($newimage, true);
$transparentindex = imagecolorallocatealpha($newimage, 255, 255, 255, 127);
imagefill($newimage, 0, 0, $transparentindex);
【讨论】: