Changes to PHP Image Resize Function - $20

Discussion in 'Programming' started by adamjblakey, Nov 6, 2009.

  1. #1
    Hi,

    I have the following function to resize all my images. It is just not cutting it with the quality though and think i need to use image magic or another of the same quality.

    I will pay $20 to someone who can change the following function so it produces much better quality images.

    Here is the function.

    
    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, Nov 6, 2009 IP
  2. mrphp

    mrphp Well-Known Member

    Messages:
    682
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    110
    As Seller:
    100% - 0
    As Buyer:
    100% - 0
    #2
    use this function imagecopyresampled() to have a great quality.
     
    mrphp, Nov 6, 2009 IP
  3. adamjblakey

    adamjblakey Active Member

    Messages:
    1,121
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    80
    As Seller:
    100% - 0
    As Buyer:
    100% - 0
    #3
    I have already tried this and produces the exact same quality.
     
    adamjblakey, Nov 6, 2009 IP
  4. stephan2307

    stephan2307 Well-Known Member

    Messages:
    1,277
    Likes Received:
    33
    Best Answers:
    7
    Trophy Points:
    150
    As Seller:
    100% - 0
    As Buyer:
    100% - 2
    #4
    This is from php.net
    imagejpeg  ( resource $image  [, string $filename  [, int $quality  ]] )
    Code (markup):
    add the quality parameter and set it to 100. Default is 75
     
    stephan2307, Nov 6, 2009 IP
  5. adamjblakey

    adamjblakey Active Member

    Messages:
    1,121
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    80
    As Seller:
    100% - 0
    As Buyer:
    100% - 0
    #5
    I have done this in the past but the difference is barely unnoticeable that's why i want to rebuild it.
     
    adamjblakey, Nov 6, 2009 IP
  6. stephan2307

    stephan2307 Well-Known Member

    Messages:
    1,277
    Likes Received:
    33
    Best Answers:
    7
    Trophy Points:
    150
    As Seller:
    100% - 0
    As Buyer:
    100% - 2
    #6
    stephan2307, Nov 6, 2009 IP
  7. Bohra

    Bohra Prominent Member

    Messages:
    12,573
    Likes Received:
    537
    Best Answers:
    0
    Trophy Points:
    310
    As Seller:
    100% - 0
    As Buyer:
    100% - 0
    #7
    yes ImageMagick is better than gd but its not there on all the servers
     
    Bohra, Nov 6, 2009 IP
  8. adamjblakey

    adamjblakey Active Member

    Messages:
    1,121
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    80
    As Seller:
    100% - 0
    As Buyer:
    100% - 0
    #8
    All Done. Thanks.
     
    adamjblakey, Nov 6, 2009 IP
  9. AffiliateStoreMaster

    AffiliateStoreMaster Peon

    Messages:
    12
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    As Seller:
    100% - 0
    As Buyer:
    100% - 0
    #9
    Try this class. I tested on GIF,JPEG,PNG. The quality is acceptable. In any cate you should use imagecopyresampled instead imagecopyresized. It is possible that you have not gd2 lib (?) where imagecopyresampled is not declared.
    
    <?php
    
    class ImageCropper {
    
    	function chkgd2 () {
    		$testGD = get_extension_funcs( "gd" ); // Grab function list
    		if( !$testGD ) { echo "GD not installed."; exit; }
    		if( in_array ( "imagegd2", $testGD ) )
    			return true;
    		else 
    			return false; 
    	}
    
    	function createThumb( $src, $dest, $maxWidth, $maxHeight, $quality ) { 
    	    if( file_exists( $src ) && isset( $dest ) ) { 
    	        $destInfo  = pathInfo( $dest ); 
    	        $srcSize   = getImageSize( $src ); 
    
    	        // image dest size $destSize[0] = width, $destSize[1] = height 
    	        $srcRatio  = $srcSize[0] / $srcSize[1]; // width/height ratio 
    			if( !$maxWidth ) $maxWidth = 50;
    			if( !$maxHeight ) $maxHeight = 50;
    	        $destRatio = $maxWidth / $maxHeight; 
    	        if ($destRatio > $srcRatio) { 
    	            $destSize[1] = $maxHeight; 
    	            $destSize[0] = $maxHeight * $srcRatio; 
    	        } else { 
    	            $destSize[0] = $maxWidth; 
    	            $destSize[1] = $maxWidth / $srcRatio; 
    	        } 
    
    /* uncomment if you want to crop GIF to JPG
    	        if( strtoupper( $destInfo['extension'] ) == "GIF")
    	            $dest = substr_replace( $dest, 'jpg', -3 ); 
    */
    	        $gd2 = $this->chkgd2 ();
    			if( $gd2 && function_exists( imagecreatetruecolor ) ) {
    		        $destImage = @imagecreatetruecolor( $destSize[0], $destSize[1] ) or  $destImage = imagecreate( $destSize[0], $destSize[1] );
    				if( function_exists( imageantialias ) )
    		        	@imageantialias( $destImage, true ); 
    			} else
    		        $destImage = imagecreate( $destSize[0], $destSize[1] );
    
    	        switch ($srcSize[2]) { 
    	            case 1: //GIF 
    		            $srcImage = imagecreatefromgif( $src ); 
    		            break; 
    	            case 2: //JPEG 
    		            $srcImage = imagecreatefromjpeg( $src ); 
    		            break; 
    	            case 3: //PNG 
    		            $srcImage = imagecreatefrompng( $src ); 
    		            break; 
    	            default: 
    		            return false; 
    		            break; 
    	        } 
    
    	        // resampling 
    			if( $gd2 && function_exists( imagecopyresampled ) ) {
    		       if( !@imagecopyresampled( $destImage, $srcImage, 0, 0, 0, 0, $destSize[0], $destSize[1], $srcSize[0], $srcSize[1] ) )
    		        	imagecopyresized( $destImage, $srcImage, 0, 0, 0, 0, $destSize[0], $destSize[1], $srcSize[0], $srcSize[1] );
    			} else
    	        	imagecopyresized( $destImage, $srcImage, 0, 0, 0, 0, $destSize[0], $destSize[1], $srcSize[0], $srcSize[1] );
    
    	        // generating image 
    	        switch( $srcSize[2] ) { 
    	            case 1: 
    //* comment if you want to crop GIF to JPG
    					$white = imagecolorallocate ( $srcImage, 255, 255, 255 );
    					$transparentColor = imagecolortransparent ( $srcImage, $white );
    					$colorIndex = imagecolorsforindex ( $srcImage, $transparentColor );
    					$transparentColor = imagecolorexact ( $destImage, $colorIndex["red"], $colorIndex["green"], $colorIndex["blue"] );
    					imagefill ( $destImage, 0, 0, $transparentColor );
    					imagecolortransparent ( $destImage, $transparentColor );
    		            if( !@imagegif( $destImage, $dest ) ) return false;
    		            break; 
    //*/
    				case 2: 
    		            if( !@imagejpeg( $destImage, $dest, $quality ) ) return false;
    	            case 3: 
    		            if( !@imagepng( $destImage, $dest ) ) return false;
    		            break; 
    	        } 
    	        return true; 
    	    } else
    	        return false; 
    	}
    }
    
    
    $ic = new ImageCropper ();
    $ic->createThumb ( "logo.gif", 'test.gif', 150, 150, 100 );
    $ic->createThumb ( "logo.jpg", 'test.jpg', 150, 150, 100 );
    $ic->createThumb ( "logo.png", 'test.png', 150, 150, 100 );
    
    PHP:
     
    AffiliateStoreMaster, Nov 6, 2009 IP
  10. cloudnthunder

    cloudnthunder Peon

    Messages:
    163
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    As Seller:
    100% - 0
    As Buyer:
    100% - 0
    #10
    check pm....
     
    cloudnthunder, Nov 10, 2009 IP