Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Senior Coder
    Join Date
    Jun 2008
    Location
    New Jersey
    Posts
    2,536
    Thanks
    45
    Thanked 259 Times in 256 Posts

    Setting default transparency color for read png

    So I've got a pretty straight forward script:

    PHP Code:
                list($imgWidth$imgHeight$imgType) = getimagesize($_FILES['img']['tmp_name']);
                if (
    image_type_to_mime_type($imgType) == 'image/jpeg' || image_type_to_mime_type($imgType) == 'image/pjpeg'$tempImg imagecreatefromjpeg($_FILES['img']['tmp_name']);
                elseif (
    image_type_to_mime_type($imgType) == 'image/gif'$tempImg imagecreatefromgif($_FILES['img']['tmp_name']);
                elseif (
    image_type_to_mime_type($imgType) == 'image/png'$tempImg imagecreatefrompng($_FILES['img']['tmp_name']);
                
                
    $xRatio $maxWidth $imgWidth;
                
    $yRatio $maxHeight $imgHeight;
                
                if (
    $imgWidth <= $maxWidth && $imgHeight <= $maxHeight) {
                    
    $finalWidth $imgWidth;
                    
    $finalHeight $imgHeight;
                } elseif ((
    $xRatio $imgHeight) < $maxHeight) {
                    
    $finalWidth $maxWidth;
                    
    $finalHeight ceil($xRatio $imgHeight);
                } else {
                    
    $finalWidth ceil($yRatio $imgWidth);
                    
    $finalHeight $maxHeight;
                }
                
                
    $tempColor imagecreatetruecolor($finalWidth$finalHeight);
                
    imagecopyresampled($tempColor$tempImg0000$finalWidth$finalHeight$imgWidth$imgHeight);
                
    // Define $imageID here
                
                
    imagejpeg($tempColorFILEROOT."/images/uploads/$imageID.jpg"100);
                
    imagedestroy($tempImg);
                
    imagedestroy($tempColor); 
    However, when I upload a PNG, any transparency in it goes black. I'm assuming the same would happen with a GIF. I'm trying to figure out how to make the transparency go white instead. Anyone have thoughts? I've looked at functions such as imagecolortransparent, but can't seem to use it correctly, if it is the right function to use at all.

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    I'm thinking set the imagealphablending on for $tempColor, then paint the background white. After that, copy the image from $tempImg onto it.
    Try that, see what it does. I can't test that where I am.

  • #3
    Senior Coder
    Join Date
    Jun 2008
    Location
    New Jersey
    Posts
    2,536
    Thanks
    45
    Thanked 259 Times in 256 Posts
    Haha, I'll be honest, I have no idea how to really do what you said :P I found that script online... I really need to learn GD... not asking for the specific code (though I'd be stupid to turn it down), but could you point me in the right direction?

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    That would be a matter of:
    PHP Code:
    $white imagecolorallocate($tempColor255255255);
    imagealphablending($tempColortrue);
    imagefill($tempColor00$white); 
    Then perform the copy.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •