Image Resize Function - Not The Best

Discussion in 'PHP' started by adamjblakey, Sep 29, 2009.

  1. #1
    Hi,

    I have been using the following function to re size my images but to be honest i am not really happy with the end result as the images are not 100% clear.

    Is there anything i can do to make the images clearer?

    
    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 < 175){
    		$ThumbWidth = $ewidth;
    	}else{
    		$ThumbWidth = 175;
    	}
    	if ($alwidth == ''){
    		
    		if ($ewidth < 600){
    			$ImgWidth = ($ewidth + 40);
    		}else{
    			$ImgWidth = 640;
    		}
    		
    	}else{
    		$ImgWidth = $alwidth;
    	}
    	
    	if ($ewidth < 220){
    		$medWidth = $ewidth;
    	}else{
    		$medWidth = 220;
    	}
    
    	//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);
    		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);
    		
    		$resized_img3 = imagecreatetruecolor($newwidth3, $newheight3);
    		imagecopyresized($resized_img3, $new_img, 0, 0, 0, 0, $newwidth3, $newheight3, $width, $height);
    		
    		imagejpeg($resized_img, "imgtemp/thumbs/".$rand_name.".".$file_ext);  
    		imagejpeg($resized_img2, "imgtemp/big/".$rand_name.".".$file_ext);
    		imagejpeg($resized_img3, "imgtemp/med/".$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/");
    		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:
    Cheers,
    Adam
     
    adamjblakey, Sep 29, 2009 IP
  2. Martinoes

    Martinoes Peon

    Messages:
    110
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Try to add quality parameter for your 'imagejpeg' functions:

    imagejpeg ( resource $image [, string $filename [, int $quality ]] ) - default if about 75, set to 100.

    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);
     
    Martinoes, Sep 29, 2009 IP
  3. olddocks

    olddocks Notable Member

    Messages:
    3,275
    Likes Received:
    165
    Best Answers:
    0
    Trophy Points:
    215
    #3
    olddocks, Sep 29, 2009 IP
  4. creativeGenius

    creativeGenius Well-Known Member

    Messages:
    273
    Likes Received:
    5
    Best Answers:
    1
    Trophy Points:
    120
    #4
    well, if this is for study purposes then it defeats the idea, but if indeed you just want to process your images then you're better off with imagemagick and other ready made libraries
     
    creativeGenius, Sep 29, 2009 IP
  5. adamjblakey

    adamjblakey Active Member

    Messages:
    1,121
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    80
    #5
    I tried this but did not make a difference so looks like it is set to 100% already. Is there anything else i can do at all to this.

    Thanks for the suggestions about using e.g. imagemagick but i want to stick with the current function.
     
    adamjblakey, Sep 30, 2009 IP