Transparent PNG - Black Background on Upload

Discussion in 'PHP' started by adamjblakey, Dec 20, 2011.

  1. #1
    Hi,

    I have the following script i use to upload images which works fine however when i upload PNG's with a transparent background the background is turning black.

    Any ideas? The code is below:

    
    function uploadimage($value, $alwidth)
    {
       
       $file_type = $value['type'];
        $file_name = $value['name'];
        $file_size = $value['size'];
        $file_tmp = $value['tmp_name'];
    
    //check file extension
           $ext = strrchr($file_name,'.');
           $ext = strtolower($ext);
           if (($extlimit == "yes") && (!in_array($ext,$limitedext))) {
              echo "Wrong file extension.  <br>--<a href=\"$_SERVER[PHP_SELF]\">back</a>";
              exit();
           }
    
        //get the file extension.
        $getExt = explode ('.', $file_name);
        $file_ext = $getExt[count($getExt)-1];
    
        //create a random file name
        $rand_name = md5(time());
        $rand_name= rand(0,999999999);
        //get the new width variable.
        list($ewidth, $eheight) = getimagesize($file_tmp);
        if ($ewidth < 300){
            $ThumbWidth = $ewidth;
        }else{
            $ThumbWidth = 300;
        }
       
    
        //keep image type
        if($file_size){
            if($file_type == "image/pjpeg" || $file_type == "image/jpeg"){
                $new_img = imagecreatefromjpeg($file_tmp);
            }elseif($file_type == "image/x-png" || $file_type == "image/png"){
                $new_img = imagecreatefrompng($file_tmp);
            }elseif($file_type == "image/gif"){
                $new_img = imagecreatefromgif($file_tmp);
            }
            //list width and height and keep height ratio.
            list($width, $height) = getimagesize($file_tmp);
                    
            $bigger = ($width >= $height) ? $width : $height; 
        
            $imgratio = $bigger / $ThumbWidth;
            $newwidth = intval($width / $imgratio);
            $newheight = intval($height / $imgratio);
            
            
            $resized_img = imagecreatetruecolor($newwidth, $newheight);
            imagecopyresampled($resized_img, $new_img, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
            
            imagejpeg($resized_img, "imgtemp/".$rand_name.".".$file_ext,100);  
    
            //save images
            ftpupload($rand_name.".".$file_ext, "imgtemp/".$rand_name.".".$file_ext, "/httpdocs/uploads/images/");
            
            unlink("imgtemp/".$rand_name.".".$file_ext);
            
            imagedestroy($resized_img);
            imagedestroy($new_img);
    
        }   
        $value = $rand_name.".".$file_ext;
        return $value;
    }
    
    PHP:
    Thanks in advanced.
    Cheers,
    Adam
     
    adamjblakey, Dec 20, 2011 IP
  2. proactiv3

    proactiv3 Peon

    Messages:
    55
    Likes Received:
    7
    Best Answers:
    4
    Trophy Points:
    0
    #2
    Just opened up the PHP manual page for the imagecreatefrompng function and found out the following comment:

    Would you give it a try and give us feedback?
     
    proactiv3, Dec 20, 2011 IP