This function is taken from lots of web sources so thanks to all for posting. It creates square or landscape thumbnails from .jpgs from either portait or landscape original images. I decide in advance which way I want the thumbs to display for consistency. I usually sharpen the images as well post-upload, to save on server resources. I can post this code if anyone wants it. I hope this helps someone...
# create thumbnails from jpgs
# usage:
# create_jpgthumb(uploaded file, final file (with path), thumb height, thumb width, jpg quality, scale thumb (true) or fixed size (false);
function create_jpgthumb($original, $thumbnail, $max_width, $max_height, $quality, $scale = true) {
list ($src_width, $src_height, $type, $w) = getimagesize($original);
if (!$srcImage = @imagecreatefromjpeg($original)) {
return false;
}
# image resizes to natural height and width
if ($scale == true) {
if ($src_width > $src_height ) {
$thumb_width = $max_width;
$thumb_height = floor($src_height * ($max_width / $src_width));
} else if ($src_width < $src_height ) {
$thumb_height = $max_height;
$thumb_width = floor($src_width * ($max_height / $src_height));
} else {
$thumb_width = $max_height;
$thumb_height = $max_height;
}
if (!@$destImage = imagecreatetruecolor($thumb_width, $thumb_height)) {
return false;
}
if (!@imagecopyresampled($destImage, $srcImage, 0, 0, 0, 0, $thumb_width, $thumb_height, $src_width, $src_height)) {
return false;
}
# image is fixed to supplied width and height and cropped
} else if ($scale == false) {
$ratio = $max_width / $max_height;
# thumbnail is landscape
if ($ratio > 1) {
# uploaded pic is landscape
if ($src_width > $src_height) {
$thumb_width = $max_width;
$thumb_height = ceil($max_width * ($src_height / $src_width));
if ($thumb_height > $max_width) {
$thumb_height = $max_width;
$thumb_width = ceil($max_width * ($src_width / $src_height));
}
# uploaded pic is portrait
} else {
$thumb_height = $max_width;
$thumb_width = ceil($max_width * ($src_height / $src_width));
if ($thumb_width > $max_width) {
$thumb_width = $max_width;
$thumb_height = ceil($max_width * ($src_height / $src_width));
}
$off_h = ($src_height - $src_width) / 2;
}
if (!@$destImage = imagecreatetruecolor($max_width, $max_height)) {
return false;
}
if (!@imagecopyresampled($destImage, $srcImage, 0, 0, 0, $off_h, $thumb_width, $thumb_height, $src_width, $src_height)) {
return false;
}
# thumbnail is square
} else {
if ($src_width > $src_height) {
$off_w = ($src_width - $src_height) / 2;
$off_h = 0;
$src_width = $src_height;
} else if ($src_height > $src_width) {
$off_w = 0;
$off_h = ($src_height - $src_width) / 2;
$src_height = $src_width;
} else {
$off_w = 0;
$off_h = 0;
}
if (!@$destImage = imagecreatetruecolor($max_width, $max_height)) {
return false;
}
if (!@imagecopyresampled($destImage, $srcImage, 0, 0, $off_w, $off_h, $max_width, $max_height, $src_width, $src_height)) {
return false;
}
}
}
@imagedestroy($srcImage);
if (!@imageantialias($destImage, true)) {
return false;
}
if (!@imagejpeg($destImage, $thumbnail, $quality)) {
return false;
}
@imagedestroy($destImage);
return true;
}