Hi All
I''ve got a working upload and resize script for jpeg images, but the thumbnail image looks all washed out and loos of color. I've got the quality set at 100 but they still look crappy.
Here is the code: Can you tell me what I'm doing wrong?
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",100);
//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");
}