0

I use the class of https://github.com/lunakid/AnimGif. it animated me 4 created png images to a gif. Well without imagetruecolortopalette the picture has a bad quality (pixelated). With imagetruecolortopalette it loses the transparent background.

The Code sould cut a image with a stencil and overlay a other image that was also cut with a stencil.

Example stencil

Here is the code:

$im = imagecreatetruecolor(64, 110);

$farbe_body = imagecolorallocate( $im, 255, 0, 255);
imagefill ( $im, 0, 0, $farbe_body );

imagecolortransparent($im, $farbe_body);

$userimage = imagecreatefromstring("$userimage");
imagecopy($im, $userimage, 0, 0, 0, 0, 64, 80);

$cut = imagecreatefromgif("cut_image.gif");
imagecopy($im_1, $cut, 0, 0, 0, 0, 64, 110);



$im_1 = imagecreatetruecolor(110, 110);

$farbe_body_1 = imagecolorallocate ( $im_1, 255, 0, 255 );

imagefill ( $im_1, 0, 0, $farbe_body_1 );
imagecolortransparent($im_1, $farbe_body_1);

$userimage_2 = imagecreatefromstring("$userimage");
imagecopy($im_1, $userimage_2, -3, 0, 0, 0, 64, 90);

$farbe_body_2 = imagecolorallocate ( $im_1, 255, 0, 255 );
imagefill ( $im_3, 0, 0, $farbe_body_2 );
imagecolortransparent($im_1, $farbe_body_2);

$farbe_body_3 = imagecolorallocate($im_1, 255, 0, 255);
$cut_2 = imagecreatefromgif("cut_image_2.gif");

imagecopy($im_1, $cut_2, 0, -1, 0, 0, 64, 110);
imagecolortransparent($im_1, $farbe_body_3);

imagecopymerge($im, $im_1, 3, 0, 0, 0, 64, 110, 100);


$frames = array($im);

$durations = array(20, 20, 20, 20);

$gc = new GifCreator\GifCreator();
$gc->create($frames, $durations, 5);

$gifBinary = $gc->getGif();

header('Content-type: image/gif');
header('Content-Disposition: filename="butterfly.gif"');
echo $gifBinary;
exit;
Bart
  • 3
  • 3
  • It seems that you have to use `imagesavealpha()` as suggested in this post : https://stackoverflow.com/questions/2622319/php-gd-imagetruecolortopalette-not-keeping-transparency – Ugo T. May 06 '19 at 16:14
  • already tried. but does not solve the problem. – Bart May 06 '19 at 16:18
  • Possible duplicate of [PHP/GD - transparent background](https://stackoverflow.com/questions/15246813/php-gd-transparent-background) – miken32 May 06 '19 at 17:37
  • It works, but only when I create one simple image. When I try to create a transparent image with an other transparent image on it, it doesn't work. The background becomes black instead of transparent. – Bart May 06 '19 at 21:49

0 Answers0