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
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
I have done this in the past but the difference is barely unnoticeable that's why i want to rebuild it.
There is not much else you can do with the GD library. Using ImageMagick (http://uk2.php.net/manual/en/book.imagick.php) might do the trick. can you use it on your server?
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: