3

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.

Community
  • 1
  • 1
Niclas Gleesborg
  • 566
  • 1
  • 5
  • 23

1 Answers1

3

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);
Niet the Dark Absol
  • 320,036
  • 81
  • 464
  • 592