imagecreatefrompng - no inherit transparency

images php coding issues or problems here.

Moderators: egami, macek, gesf

Post Reply
New php-forum User
New php-forum User
Posts: 18
Joined: Sat Dec 10, 2011 4:00 pm

Tue Dec 25, 2012 5:27 am

Problem: A PNG's transparency is not carried over to the resource when using imagecreatefrompng(). I've tried several suggested methods and different variations of alpha settings over the last couple hours, but so far nothing has worked.

I'm currently using the following function to create watermarked png images:

Code: Select all

function watermarkPNG($newname){
    $img = imagecreatefrompng($newname);
    imagesavealpha($img, true);
    imagealphablending($img, false);
    list($width, $height) = getimagesize($newname);
    $watermark = imagecreatefrompng("classes/watermark.png");
    //set new image height (image height + watermark height) 
    $newHeight = $height + 40;
    //create canvas
    $newImage = imagecreatetruecolor($width, $newHeight);
    //calculate watermark cords
    $y = $newHeight-40;
    $x = ($width - 200)/2;
    //makes $newImage background transparent
    $transparent = imagecolorallocatealpha($newImage, 0, 0, 0, 127);
        imagefill($newImage, 0, 0, $transparent);

        ImageAlphaBlending($newImage, false);
        ImageSaveAlpha($newImage, true);

    //choose RGB color
    //$white = imagecolorallocate($newImage,233,234,235);
    //fill canvas
    imagecopymerge($newImage, $img, 0, 0, 0, 0, $width, $height, 100);
    imagecopymerge($newImage, $watermark, $x, $y, 0, 0, 200, 40, 100);
    //saves file
    imagepng($newImage, $newname);

Note: The bottom area of the newly merged image containing the watermark is completely transparent.

Post Reply