图像创建从PNG()生成黑色背景而不是图像中的透明部分



我使用 PHP 制作图像,但我的代码将 png 透明度转换为纯黑色,有没有解决方案来改进我的代码?

$filename = 'https://i.ibb.co/9sB9rTM/small-408587143-15774386991of1-2-2-Original-0-test.png';
$widthsrc=0;
$heightsrc=0;

$src1 = imagecreatefrompng($filename);
list($widthsrc, $heightsrc, $typesrc, $attrsrc) = getimagesize($filename);
$background = imagecolorallocatealpha($src1, 255, 255, 255, 127);
$pre = imagecolortransparent($src1, $background);
imagealphablending($src1, false);
imagesavealpha($src1, true);
imagepng($src1);

第一次使用总是像这样的标题:

header("Content-type: image/png"(;

其次,您确定保存图像时"黑色固体"不是trasparent吗?

创建镜像示例:

header("Content-type: image/png");
$string = 'TEXT';
$im     = imagecreatefrompng("images/button1.png");
$orange = imagecolorallocate($im, 220, 210, 60);
$px     = (imagesx($im) - 7.5 * strlen($string)) / 2;
imagestring($im, 3, $px, 9, $string, $orange);
imagepng($im);
imagedestroy($im);
?>

最新更新