Re: Making thumbnails 'on the fly'.
Greetings, The Natural Philosopher.
In reply to Your message dated Sunday, February 17, 2008, 00:35:01,
>>> You should definitely add some caching to store the resized images on
>>> disk
>>
>> ...why make them on the fly?
>> Weather blobs in the database or images on the file system,
>> the big photos are already there. Adding a few thumbnails won't break
>> the disk space bank. If you do make them on the fly, then you would
>> need caching.....but it seems likely you'd run out of ram before
>> disk space. So making them in advance is still a better solution,
>> I think. That's what I do (make any missing thumbs at night, from cron).
>>
>>
> Well, if I had thought about it first, I would have made them on the fly
> when uploaded and stuffed them in the database..as well..but suh is history.
> It's a quick hack to solve a problem *well enough* for now...
I typically do nothing on the upload, except what is REALLY need to ensure
upload process are done right. Just if I RE-upload image, I go ahead and
delete corresponding thumbnail.
Creating thumbnails, storing them in cache, and display them from cache...
That's separate thing and it's done in a separate file.
So every time image or thumbnail accessed, it being sent to client from disk
only, using plain readfile().
Here is a code sample:
function imagePreview($name)
{
$fname = filesFilter($name);
if(false === $fname)
{
echo '<html><body><h3>404 No file "'.htmlspecialchars($name).'"</h3></body></html>';
}
else
{
if(!file_exists(GALLERY_BASE_DIR."/.preview/{$fname}"))
{
if(!file_exists(GALLERY_BASE_DIR.'/.preview'))
{
mkdir(GALLERY_BASE_DIR.'/.preview');
}
$img = imagecreatefromjpeg(GALLERY_BASE_DIR."/{$fname}");
$sx = imagesx($img);
$sy = imagesy($img);
$dx = GALLERY_PREVIEW_X;
$dy = GALLERY_PREVIEW_Y;
if(($sx > $dx) || ($sy > $dy))
{
if($sx > $sy)
$dy = $dx * $sy / $sx;
else
$dx = $dy * $sx / $sy;
}
imagecopyresampled($img2 = imagecreatetruecolor($dx, $dy), $img,
0, 0, 0, 0,
$dx, $dy, $sx, $sy);
imagejpeg($img2, GALLERY_BASE_DIR."/.preview/{$fname}", 90);
}
header('Content-type: image/jpeg');
readfile(GALLERY_BASE_DIR."/.preview/{$fname}");
}
}
GALLERY_BASE_DIR typically protected and/or restricted to only .php files
being accessible.
--
Sincerely Yours, AnrDaemon <anrdaemon@freemail.ru>
|