imagecreatefrompng() Makes a black background instead of transparent?

After imagecreatetruecolor():

<?php

// ... Before imagecreatetruecolor()

$dimg = imagecreatetruecolor($width_new, $height_new); // png ?: gif

// start changes
switch ($stype) {

    case 'gif':
    case 'png':
        // integer representation of the color black (rgb: 0,0,0)
        $background = imagecolorallocate($dimg , 0, 0, 0);
        // removing the black from the placeholder
        imagecolortransparent($dimg, $background);

        // turning off alpha blending (to ensure alpha channel information
        // is preserved, rather than removed (blending with the rest of the
        // image in the form of black))
        imagealphablending($dimg, false);

        // turning on alpha channel information saving (to ensure the full range
        // of transparency is preserved)
        imagesavealpha($dimg, true);
        break;

    default:
        break;
}
// end changes

$wm = $w/$nw;
$hm = $h/$nh;

// ...

Leave a Comment