0

When trying to convert PNG to GIF, return transparent to black:

$file = "example.png"

$whf = getimagesize($file); 
$wf = $whf[0];
$hf = $whf[1];

$h = "100";
$w = "100";

$img = imagecreatetruecolor($w, $h);
$imgi = imagecreatefrompng($file);

// Here means to be some magic code...

imagecopyresampled($img, $imgi, 0, 0, 0, 0, $w, $h, $wf, $hf);
imagegif($img, "example.gif");

imagedestroy($img);

Codes I've tryed but nothing:

1º:

imagesavealpha($img, true);
imagecolortransparent($img, 127<<24);

2º:

imagealphablending($img, false);
imagesavealpha($img, true);

This works! But with one detail. You need absolute transparent background without "png gradients transparencies". Imagick uses half gradient transparencies to absolute transparent and other half to absolute plain. Thanks isalgueiro!

$black = imagecolorallocate($img, 0, 0, 0);
imagecolortransparent($img, $black);    
lucasgabmoreno
  • 1,001
  • 1
  • 10
  • 18

1 Answers1

0

I think you need to call imagecolorallocate to get the color reference and pass that to imagecolortransparent:

$black = imagecolorallocate($im, 0, 0, 0);
imagecolortransparent($img, $black);
isalgueiro
  • 1,973
  • 16
  • 20
  • Returns black background – lucasgabmoreno Jan 27 '15 at 16:29
  • This is the right way, but you need to re-convert the image after. Btw, don't forget to set a background. – Ismael Miguel Jan 27 '15 at 16:31
  • 1
    Sorry, forgot to mention to set the same background color. You can check http://php.net/manual/en/function.imagefill.php for an idea, check the comment zone. Also, check http://stackoverflow.com/questions/11752451/how-do-i-set-a-fixed-background-image-for-a-php-file – Ismael Miguel Jan 27 '15 at 16:39