GifCreator icon indicating copy to clipboard operation
GifCreator copied to clipboard

PNG with transparent background changes to black

Open gesior opened this issue 10 years ago • 1 comments

When I load 2 PNGs with transparent backgrounds, it changes to black. I use small code change from (PNG to GIF converter - https://gist.github.com/gesior/6f5f7d15cb50f59a88fd) and now it works fine for PNGs. Before you add it, you must check, if other coversions still works fine.

Modified part of script:

        } elseif (is_string($frames[$i])) { // File path or URL or Binary source code

            if (file_exists($frames[$i]) || filter_var($frames[$i], FILTER_VALIDATE_URL)) { // File path

                $frames[$i] = file_get_contents($frames[$i]);                    
            }

            $resourceImg = imagecreatefromstring($frames[$i]);

            $background = array(255, 255, 255);
            $width = imagesx($resourceImg);
            $height = imagesy($resourceImg);
            $image = imagecreatetruecolor($width, $height);
            imagefill($image, 0, 0, $bgcolor = imagecolorallocate($image, $background[0], $background[1], $background[2]));
            imagecopyresampled($image, $resourceImg, 0, 0, 0, 0, $width, $height, $width, $height);
            imagecolortransparent($image, $bgcolor);

            ob_start();
            imagegif($image);
            $this->frameSources[] = ob_get_contents();
            ob_end_clean();

            imagedestroy($image);
        } else { // Fail

EDIT: I did test it with these PNG as resources. When I use 'imagepng', it save them with transparent background. When I use 'imagegif', it save them with black background. There must be something bad with PHP library that saves same image resource with/without transparency.

gesior avatar Jul 29 '15 08:07 gesior

If you can, pls help me: Sybio/ImageWorkshop#95

fwcanlam avatar May 26 '17 08:05 fwcanlam