Flourish PHP Unframework
This is an archived copy of the forum for reference purposes

fImage and runtime thumbnail

posted by masterix21 9 years ago

Hi,

i need to generate a runtime thumbnail, but if i use this code, it will overwrite my file:

$img = new fImage(dirname($img)."/".basename($img));
$img->resize($w, $h, false);
$img->saveChanges('jpg', $q);

I need to print the content and send header "image/jpeg".

Any solution?

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.

posted by wbond 9 years ago

mhm.... it's not the code that i needed. For my project, i need to generate some thumb at runtime, without write each time on disk a new file.

I tried this solution:

$img = new fImage(dirname($img)."/".basename($img));
$img->resize($w, $h, false);
$img->output(true);

but the resize, don't work (and i can't set a quality parameter to thumb). What i'm wrong?

posted by masterix21 9 years ago

Unfortunately it sounds like fImage isn't the solution for your problem. fImage queues all image modifications and runs that when the changes are saved. This is done because !ImageMagick is a command line program and it would be slower and lossier to do one modification at a time. Even if fImage selects the GD backend, it will still function in the same way.

You'll need to either save the thumbnail to a temporary file and then delete it once output, or use raw GD functions.

posted by wbond 9 years ago