#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    64
    Rep Power
    3

    Imagecreatefrompng - no inherit transparency


    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:
    PHP Code:
    function watermarkPNG($newname){
       
        
    $img imagecreatefrompng($newname);
        
    imagesavealpha($imgtrue);
        
    imagealphablending($imgfalse);
        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($newImage000127);
            
    imagefill($newImage00$transparent);

            
    ImageAlphaBlending($newImagefalse);
            
    ImageSaveAlpha($newImagetrue);

        
        
    //choose RGB color
        //$white = imagecolorallocate($newImage,233,234,235);
        
        //fill canvas
        //imagefill($newImage,0,$y,$white);
        
        
    imagecopymerge($newImage$img0000$width$height100);
        
    imagecopymerge($newImage$watermark$x$y0020040100);
        
        
    //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.
  2. #2
  3. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,908
    Rep Power
    6352
    The manual comments address this:
    Originally Posted by Manual Comments
    If you're trying to load a translucent png-24 image but are finding an absence of transparency (like it's black), you need to enable alpha channel AND save the setting. I'm new to GD and it took me almost two hours to figure this out.

    PHP Code:
    <?php
    $imgPng 
    imageCreateFromPng($strImagePath);
    imageAlphaBlending($imgPngtrue);
    imageSaveAlpha($imgPngtrue);

    /* Output image to browser */
    header("Content-type: image/png");
    imagePng($imgPng); 
    ?>
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    64
    Rep Power
    3
    Originally Posted by ManiacDan
    The manual comments address this:
    Unfortunately I've tried this already with no luck. I tried it again as you have suggested for the hell of it, but it's still producing the same result.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    64
    Rep Power
    3
    I still haven't been able to solve this problem if anyone has any suggestions it would be greatly appreciated.

IMN logo majestic logo threadwatch logo seochat tools logo