简体   繁体   English

imagecreatefrompng()png的透明部分是黑色的

[英]imagecreatefrompng() transparent part of png is black

elaphant

I am creating a poster using PHP and I want to add figures on the poster. 我正在使用PHP创建一张海报,我想在海报上添加数字。

I use the following code to add them: 我使用以下代码添加它们:

$src1 = imagecreatefrompng("m2.png");
$widthsrc=0;
$heightsrc=0;
list($widthsrc, $heightsrc, $typesrc, $attrsrc) = getimagesize("m2.png");

$background = imagecolorallocate($src1, 0, 0, 0);
imagecolortransparent($src1, $background);
imagealphablending($src1, false);
imagesavealpha($src1, true);

imagecopyresampled($my_img,$src1,$line2X1+100*$resize,$line2Y1,0,0,1000*$resize,1000*$resize,$widthsrc,$heightsrc);

The problem is that the places that the figures should be transparent, they are black. 问题是数字应该是透明的,它们是黑色的。

I have already looked at the following posts: 我已经查看了以下帖子:

  1. imagecreatefrompng-makes-a-black-background-instead-of-transparent imagecreatefrompng-品牌-A-黑色背景代替-的透明
  2. hp-resizing-png-images-generate-black-background HP-调整大小-PNG图像-产生黑色背景
  3. png-has-black-background PNG-有黑色背景

But I haven't been able to create a solution that works for me. 但我无法创建适合我的解决方案。

Well that was easy XD Converting comment to answer: 那很容易XD转换评论回答:

Your mistake was in defining the background colour. 你的错误在于定义背景颜色。 You should use this: 你应该用这个:

$background = imagecolorallocatealpha($src,0,0,0,127);

However, it is probably a good idea to be safe, and avoid using a "transparent" colour that already exists on your image. 但是,保证安全并避免使用图像中已存在的“透明”颜色可能是个好主意。 The "traditional" transparent colour from old sprite-based games is magenta, since it is very unlikely that you'll have straight magenta on your image! 来自旧的基于精灵的游戏的“传统”透明色是洋红色,因为你的图像上不太可能有直的洋红色!

$background = imagecolorallocatealpha($src,255,0,255,127);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM