Hi,
Well, the code I came up with is something like so:
[size=x-small]
/*
array createResampledJpeg(dest, source, maxW, maxH, [quality=80])
Generates a resampled JPEG image from an existing one on the server
dest: destination image (server path/file - not the url)
source: original image (server path/file - not the url)
maxW: maximum width of destination image
maxH: maximum height of destination image
quality: quality for jpeg encoding (default = 80)
returns: array of final width/height/path of resampled image
Requires php gd library
Thumbnail folder must have write permission!
*/
function createResampledJpeg($dstImgPath, $srcImgPath, $maxW, $maxH, $quality=80)
{
$srcProps = getimagesize($srcImgPath);
$srcAspect = $srcProps[0]/$srcProps[1];
$frmAspect = $maxW/$maxH;
$finalW = $maxW;
$finalH = $maxH;
if ($frmAspect >= $srcAspect) {
$finalH = $maxH;
$finalW = $maxH * $srcAspect;
}
else {
$finalW = $maxW;
$finalH = $maxW / $srcAspect;
}
$srcImg = imagecreatefromjpeg($srcImgPath);
$dstimg = imagecreatetruecolor($finalW,$finalH);
imagecopyresampled($dstImg, $srcImg, 0, 0, 0, 0, $finalW, $finalH, $srcProps[0], $srcProps[1]);
imagejpeg($dstImg, $dstImgPath, $quality);
imagedestroy($srcImg);
imagedestroy($dstImg);
return array($finalW, $finalH, $dstImgPath);
}
[/size]
[size=x-small]
/*
array getThumbnail(source, thumb, name, maxW, maxH, [quality=80])
Returns a cached thumbnail of the source image, or generates a fresh one when there one is not available or has become stale (older than the source image file).
source: destination path for soource image (the server folder!)
thumb: source path for thumbnail image (the server folder!)
name: source image name
maxW: maximum width of destination image
maxH: maximum height of destination image
quality: quality for jpeg encoding (default = 80)
returns: array of final width/height/path of thumbnail image
*/
function getThumbnail($imageFolder, $thumbFolder, $name, $defImg, $maxW, $maxH, $q=80)
{
$srcName = $imageFolder . $name;
// change this for fancier thumbnail names ;-)
$thmName = $thumbFolder . $name;
// Source image must exist - else return $defImage (a spacer)
if (!file_exists($srcName))
{
return array($maxW, $maxH, $defImg);
}
// Does thumbnail exist?
if (file_exists($thmName))
{
// Is source image older than the thumbnail?
if (filectime($srcName) <= filectime($thmName))
{
// return the existing thumbnail properties
$thmProps = GetImageSize($thmName);
return array($thmProps[0], $thmProps[1], $thmName);
}
}
// Create a new thumbnail and return it's properties
return createResampledJpeg($thmName, $srcName, $maxW, $maxH, $q);
}
[/size]
The above isnt totally robust but it relies on the fact that it is never fed bad parameters within its intended use.
It seems to achieve what I set out to do, but I will definately have to test it more closely ;-)