I wonder if there is a way to get better image quality on a cropped thumbnail.
The code below is first creating a thumbnail from the original and then it’s cropping the thumbnail before the display. I don’t know if loses quality because of too many alterations of the image? If it would be only making a thumbnail without the cropping it looks ok although it loses some quality. It just seems like after the cropping the thumbnail the quality is really bad.
Is there a way to get around this problem?
I don’t know how they do it on other sites, but there are a lot of sites with the thumbnails cropped that look pretty decent.
$pPath = "../photos/";
if (is_uploaded_file($_FILES['thefile']['tmp_name'])){
move_uploaded_file($_FILES['thefile']['tmp_name'], $pPath.$_FILES['thefile']['name']) or die ("Couldn't copy");
header("Location: ../members.php");
// DEFINE THE CROP SIZE
$width = 100;
$height = 100;
// DEFINE THE FILE
$filename = $pPath.$_FILES['thefile']['name'];
$thumbname = "TN_".$_FILES['thefile']['name'];
$thumbnail = $pPath.$thumbname;
if(file_exists($thumbnail) {
unlink($thumbnail);
}
//UPDATE DATABASE
$qry = "UPDATE members SET photo_path='$filename', thumb_path='$thumbnail' WHERE member_id='$member_id'";
$result = mysql_query($qry) or die("Query failed");
// GET NEW DIMENSIONS
list($width_orig, $height_orig) = getimagesize($filename);
if($width_orig > $height_orig) { # Reduce Height to $height, and Width proportionately
$sy = $height; #S = 100;
$sx = floor(($sy*$width_orig)/$height_orig); #R = SX/Y
} else { #Reduce Width to $width, and height proportionately
$sx = $width; #R
$sy = floor(($sx * $height_orig)/$width_orig); #S = RY/X
}
$canvas = imagecreatetruecolor( $sx,$sy);
$thumb = imagecreatefromjpeg( $filename );
imagecopyresized( $canvas, $thumb, 0,0,0,0, $sx, $sy, $width_orig, $height_orig );
$canvas2 = imagecreatetruecolor( $width, $height );
$sx = floor($sx / 2)-floor($width / 2);
$sy = floor($sy / 2)-floor($height / 2);
imagecopyresized( $canvas2, $canvas, 0,0, $sx, $sy, $width, $height, $width, $height );
imagejpeg( $canvas2, $thumbnail );
imagedestroy( $canvas );
imagedestroy( $canvas2 );
imagedestroy( $thumb );