Help Editing This Function

Discussion in 'PHP' started by adamjblakey, Apr 10, 2009.

  1. #1
    Hi,

    What i wish to do is within this function is check if the size of the original image to see if it is smaller than $ImgWidth and if so then upload the orginal and not the resized big image.

    Here is the code:
    
    function uploadimage($value)
    {
    	$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.
    	$ThumbWidth = 150;
    	$ImgWidth = 640;
    
    	//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);
    		
    		
    		$resized_img = imagecreatetruecolor($newwidth, $newheight);
    		imagecopyresized($resized_img, $new_img, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
    		
    		$resized_img2 = imagecreatetruecolor($newwidth2, $newheight2);
    		imagecopyresized($resized_img2, $new_img, 0, 0, 0, 0, $newwidth2, $newheight2, $width, $height);
    		
    		imagejpeg($resized_img, "imgtemp/thumbs/".$rand_name.".".$file_ext);  
    		imagejpeg($resized_img2, "imgtemp/big/".$rand_name.".".$file_ext);
    
    		//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/");
    		
    		unlink("imgtemp/big/".$rand_name.".".$file_ext);
    		unlink("imgtemp/thumbs/".$rand_name.".".$file_ext);
    		
    		imagedestroy($resized_img);
    		imagedestroy($resized_img2);
    		imagedestroy($new_img);
    
    	}	
    	$value = $rand_name.".".$file_ext;
    	return $value;
    }
    
    PHP:
    Thanks in Advanced.
    Cheers,
    Adam
     
    adamjblakey, Apr 10, 2009 IP
  2. Agent_Smith

    Agent_Smith Well-Known Member

    Messages:
    890
    Likes Received:
    43
    Best Answers:
    0
    Trophy Points:
    145