简体   繁体   中英

imagecreatefrompng() transparent part of png is black

elaphant

I am creating a poster using PHP and I want to add figures on the poster.

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
  2. hp-resizing-png-images-generate-black-background
  3. png-has-black-background

But I haven't been able to create a solution that works for me.

Well that was easy XD Converting comment to answer:

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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