Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PNG with transparent background changes to black #12

Open
gesior opened this issue Jul 29, 2015 · 1 comment
Open

PNG with transparent background changes to black #12

gesior opened this issue Jul 29, 2015 · 1 comment

Comments

@gesior
Copy link

gesior commented Jul 29, 2015

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.

@fwcanlam
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants