I'm working on a page that allows users to upload images. The code processes the images and creates three versions of each: a large version, a medium sized thumbnail and a small sized thumbnail, then uploads them all to certain folders. The code does everything perfectly except inexplicably, the medium and small thumbs are saved with the uploaded sized smaller than the size I've specified thus generating a black edge on the bottom and right hand side.
I'm using the same piece of code for all three images and simply changing the dimensions which is what's confusing me. If it works fine for the large image, then what's different about the thumbs?
See: http://bit.ly/193Gh9q
//Grab the width and height of our original image
list($width, $height) = getimagesize($image_location);
//check for portrait photos
if ($height >= $width)
{
$image_ratio = $width / $height;
$main_image_height = 326;
$main_image_width = $main_image_height / $image_ratio;
$thumb_height = 150;
$thumb_width = 150 / $image_ratio;
$gallery_thumb_height = 65;
$gallery_thumb_width = 65 / $image_ratio;
}
else
{
//We have a landscape image.
//Set the width and height of our two types of thumbnails
$main_image_width = 490;
$main_image_height = 326;
$thumb_width = 235;
$thumb_height = 150;
$gallery_thumb_width = 97;
$gallery_thumb_height = 65;
}
//The next three steps are repeated. Once for each type of image - Main, thumbnail, gallery thumbnail.
//MAIN
// create a new temporary image
$tmp_img = imagecreatetruecolor( $main_image_width, $main_image_height );
$tmp_img_old = imagecreatefromjpeg($image_location);
// copy and resize old image into new image
imagecopyresampled( $tmp_img, $tmp_img_old, 0, 0, 0, 0, $main_image_width, $main_image_height, $width, $height );
// save thumbnail into a file
imagejpeg( $tmp_img, $image_location, 90);
unset ($tmp_image);
unset ($tmp_img_old);
//THUMBNAIL1
// create a new temporary image
$tmp_img = imagecreatetruecolor( $thumb_width, $thumb_height );
$tmp_img_old = imagecreatefromjpeg($image_location);
// copy and resize old image into new image
imagecopyresampled( $tmp_img, $tmp_img_old, 0, 0, 0, 0, $thumb_width, $thumb_height, $width, $height );
// save thumbnail into a file
imagejpeg( $tmp_img, $thumbs_location, 90);
unset ($tmp_image);
unset ($tmp_img_old);
//THUMBNAIL2
// create a new temporary image
$tmp_img = imagecreatetruecolor( $gallery_thumb_width, $gallery_thumb_height );
$tmp_img_old = imagecreatefromjpeg($image_location);
// copy and resize old image into new image
imagecopyresampled( $tmp_img, $tmp_img_old, 0, 0, 0, 0, $gallery_thumb_width, $gallery_thumb_height, $width, $height );
// save thumbnail into a file
imagejpeg( $tmp_img, $gallery_thumbs_location, 90);
unset ($tmp_image);
unset ($tmp_img_old);