imagecreatefrompng - no inherit transparency

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
nbasso713
Forum Commoner
Posts: 29
Joined: Fri Nov 04, 2011 7:51 am

imagecreatefrompng - no inherit transparency

Post by nbasso713 »

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
    //imagefill($newImage,0,$y,$white);
    
    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);
 
    imagedestroy($img);
    imagedestroy($newImage);
    imagedestroy($watermark);
    
}
Note: The bottom area of the newly merged image containing the watermark is completely transparent.
Post Reply