Board index   FAQ   Search  
Register  Login
Board index php forum :: php coding PHP coding => Images

imagecreatefrompng - no inherit transparency

images php coding issues or problems here.

Moderators: macek, egami, gesf

imagecreatefrompng - no inherit transparency

Postby nbasso713 » 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
    //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.
nbasso713
New php-forum User
New php-forum User
 
Posts: 15
Joined: Sat Dec 10, 2011 4:00 pm

Return to PHP coding => Images

Who is online

Users browsing this forum: No registered users and 1 guest

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.

cron