Page 1 of 1

imagecreatefrompng - no inherit transparency

Posted: Tue Dec 25, 2012 7:29 am
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.