Hi All
Still having a problem with my thumbnail images. Although the quality of the image is very high, the resized thumbnail is quite poor quality. The original image is a 640X480 jpeg the thumnail is a 160X120 jpeg.
I am using GD on a linux server.
Here is my Code:
echo "<b>Begin Thumb Creation</b><br>";
//Next, we retrieve the original dimensions of the file:
//GetImageSize returns an array that contains several indexes. Two of these indexes are
//the width and height of the image. It also returns the image type as a multi-dimensional
//array, but we don't need this.
//GetImageSize() takes one argument, a string containing the location of an image
//(the version in PHP 4.0.5 will even let you give an URL to the image instead of a local path),
//and returns a four-element array containing the following:
// Index 0: The width of the image, in pixels
// Index 1: The height of the image, in pixels
// Index 2: A flag containing the image type, 1 = GIF, 2 = JPG, 3 = PNG, 4 = SWF
// Index 3: A string containing the height and width elements suitable for inserting in an <img> tag (e.g.: width="480" height="640").
//If the string passed to GetImageSize() doesn't contain the location of an image,
//or holds otherwise incorrect data, GetImageSize() will return null and print out an ugly error message.
//set the input variables
//this code expects an image around 640X480 or 480 & 640
$img_orig_size = getimagesize($uploadFile);
$img_orig_width = $img_orig_size[0];
$img_orig_height = $img_orig_size[1];
// echo "File Size: $img_orig_size<br>";
// echo "File Width: $img_orig_width<br>";
// echo "File Height: $img_orig_height<br>";
//adjust for portrait or landscape
if ($img_orig_width >= 620)
{
$thumb_max_width = 160;
$thumb_max_height = 120;
}
else
{
// Article thumbnail sizes
$thumb_max_width = 120;
$thumb_max_height = 160;
}
$thumb_quality = 100;
//In the code below we use the ImageCreateFromJpeg() function
//to create a new image based on the uploaded JPEG image.
$img_original = ImageCreateFromJpeg($uploadFile);
// Because the image can be any size, such as 10x10px, we need to check if the image needs to be resized
//in the first place. The easiest way to do this is the following piece of code:
if($img_orig_width <= $IMG_WIDTH || $img_orig_height <= $IMG_HEIGHT)
{
echo "<b>The image that you selected didnt need to be resized</b>.<br>";
}
else
{
//the image will need to be resized, as its too big. It will be a thumbnail
//This just checks to see if the original width and height of the image are less than the resized dimensions.
//Because we are keeping the thumbnail image in proportion to the original image,
//we need to calculate the resize ratio ($mlt), then times that by the original dimensions
//and round it to the closest whole number. If you were to echo $mlt then you would
//see something like 0.45356534245345.
$mlt_w = $thumb_max_width / $img_orig_width;
$mlt_h = $thumb_max_height / $img_orig_height;
$mlt = $mlt_w < $mlt_h ? $mlt_w:$mlt_h;
//#### Calculate new dimensions
$img_new_width = round($img_orig_width * $mlt);
$img_new_height = round($img_orig_height * $mlt);
//Now that we have our new width and height dimensions calculated, it's time to create our thumbnail.
//These two functions create our thumbnail. The first line creates a new blank image, into which
//we are going to resize the original image and "paste" it into $img_resized.
$img_resized = ImageCreate($img_new_width, $img_new_height);
//Imagecopyresized, is the actual function that resizes the data. The first two parameters are the
//blank image and the image data that we want resized. In this case the file type is a JPEG.
//The next 4 parameters should be left as the default value of 0 - they can be used to displace the image.
//The last 4 parameters are where we specify the new width and height as well as the original width and height of the image.
imagecopyresized($img_resized, ImageCreateFromJpeg($uploadFile), 0 , 0 , 0 , 0, $img_new_width, $img_new_height, $img_orig_width, $img_orig_height);
//create the image
//prottype: int imagejpeg ( resource image [, string filename [, int quality]])
// The filename argument is optional, and if left off, the raw image stream will be output directly
// quality is optional, and ranges from 0 (worst quality, smaller file) to 100 (best quality, biggest file).
//The default is the default IJG quality value (about 75)
Imagejpeg($img_resized, "$thumbUploadFile",$thumb_quality);
//We also destroy the image data with a call to the ImageDestroy() function
//-- this is not needed, but it's good programming practice.
ImageDestroy($img_resized);
$mg_new_size = filesize("$thumbUploadFile");
//Lastly, we output some stats about the resizing of the image and display the resized image:
echo "<b>Image Resized</b><br>
Width: $img_new_width<br>
Height: $img_new_height<br>
FileSize: $mg_new_size (in bytes)<br>";
}