Well, if you care about performance, I wouldn't recommend creating the thumbnail at runtime, or minimally at least cache it once you create it. Here is how I could write it.
$thumbnail_dir = new fDirectory('./path/for/thumbs');
$img = new fImage($img_path);
// Check to see if the thumbnail exists
$thumb_path = $thumbnail_dir->getPath() . pathinfo($img->getName(), PATHINFO_FILENAME) . '.jpg';
if (file_exists($thumb_path)) {
$thumb = new fFile($thumb_path);
// If the thumbnail is out of date, unset so it gets recreated
if ($thumb->getMTime()->lt($img->getMTime())) {
unset($thumb);
}
}
if (!isset($thumb)) {
$thumb = $img->duplicate($thumbnail_dir, TRUE);
$thumb->resize($w, $h);
$thumb->saveChanges('jpg', $q);
}
$thumb->output(TRUE);
This will use the cached file if it is newer than the original, otherwise it will create a thumbnail and then output it to the browser with the proper headers.