1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

PNG Transparency

Discussion in 'PHP' started by adamjblakey, Feb 19, 2012.

  1. #1
    Hi,

    I am using the following function which works fine however when uploading PNG's that are transparent it does not keep the transparancy and makes it black. What do i need to change on the below to keep the transparency?

    
     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 < 250){
            $ThumbWidth = $ewidth;
        }else{
            $ThumbWidth = 250;
        }
        if ($alwidth == ''){
            
            if ($ewidth < 600){
                $ImgWidth = ($ewidth + 40);
            }else{
                $ImgWidth = 640;
            }
            
        }else{
            $ImgWidth = $alwidth;
        }
        
        if ($ewidth < 420){
            $medWidth = $ewidth;
        }else{
            $medWidth = 420;
        }
    
        //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);
            
            $imgratio = $bigger / $ImgWidth;
            $newwidth2 = intval($width / $imgratio);
            $newheight2 = intval($height / $imgratio);
            
            $imgratio = $bigger / $medWidth;
            $newwidth3 = intval($width / $imgratio);
            $newheight3 = intval($height / $imgratio);
            
            
            $resized_img = imagecreatetruecolor($newwidth, $newheight);
            imagecopyresampled($resized_img, $new_img, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
            
            $resized_img2 = imagecreatetruecolor($newwidth2, $newheight2);
            imagecopyresampled($resized_img2, $new_img, 0, 0, 0, 0, $newwidth2, $newheight2, $width, $height);
            
            $resized_img3 = imagecreatetruecolor($newwidth3, $newheight3);
            imagecopyresampled($resized_img3, $new_img, 0, 0, 0, 0, $newwidth3, $newheight3, $width, $height);
            
            imagejpeg($resized_img, "imgtemp/thumbs/".$rand_name.".".$file_ext,100);  
            imagejpeg($resized_img2, "imgtemp/big/".$rand_name.".".$file_ext,100);
            imagejpeg($resized_img3, "imgtemp/med/".$rand_name.".".$file_ext,100);
    
            //save images
            ftpupload($rand_name.".".$file_ext, "imgtemp/thumbs/".$rand_name.".".$file_ext, "/httpdocs/uploads/images/thumbs/");
            ftpupload($rand_name.".".$file_ext, "imgtemp/big/".$rand_name.".".$file_ext, "/httpdocs/uploads/images/big/");
            ftpupload($rand_name.".".$file_ext, "imgtemp/med/".$rand_name.".".$file_ext, "/httpdocs/uploads/images/med/");
            
            unlink("imgtemp/big/".$rand_name.".".$file_ext);
            unlink("imgtemp/thumbs/".$rand_name.".".$file_ext);
            unlink("imgtemp/med/".$rand_name.".".$file_ext);
            
            imagedestroy($resized_img);
            imagedestroy($resized_img2);
            imagedestroy($resized_img3);
            imagedestroy($new_img);
    
        }   
        $value = $rand_name.".".$file_ext;
        return $value;
    }
    
    PHP:
    Thanks in advance.
    Adam
     
    adamjblakey, Feb 19, 2012 IP
  2. RadioBounce

    RadioBounce Banned

    Messages:
    4,171
    Likes Received:
    16
    Best Answers:
    1
    Trophy Points:
    0
    #2
    Hello, Google 'keep png transparency when uploading' and the first result from Stack Overflow looks promising. ​
     
    RadioBounce, Feb 19, 2012 IP
  3. adityamenon

    adityamenon Peon

    Messages:
    28
    Likes Received:
    0
    Best Answers:
    1
    Trophy Points:
    0
    #3
    You need more than imagecreatefrompng() if you want to preserve transparency. Check this out.
     
    adityamenon, Feb 19, 2012 IP
  4. EricBruggema

    EricBruggema Well-Known Member

    Messages:
    1,740
    Likes Received:
    28
    Best Answers:
    13
    Trophy Points:
    175
    #4
    EricBruggema, Feb 19, 2012 IP