Making thumbnails 'on the fly'.

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
I have a databse containing amongst other things many images stored as

When listing, currently I download all the images full size and let he
browser do the reduction to thumbnails.

It's getting a little slow on long lists.

Is there anyway to take the data and scale it down on the fly to a
smaller image? I have plenty of processor power, just not much bandwidth.

The images are a mixture of GIF, JPEG and PNG.

Re: Making thumbnails 'on the fly'.

The Natural Philosopher wrote:
Quoted text here. Click to load it

imagecreatefromstring() is your friend. Then just resize to your liking
and output as normal.

Registered Linux user #461062

Re: Making thumbnails 'on the fly'.

Norman Peelman wrote:
Quoted text here. Click to load it
Looks good.Any special libraries needed? having hell trying to install

Re: Making thumbnails 'on the fly'.

Norman Peelman wrote:
Quoted text here. Click to load it
Well MANY thanks..

After struggling to get Gdlib on one was magically on the
other..I didn't realise that php5-gd was an extra debian package

the final send-thumbnail.php is, with obvious inclides to access
databases etc, as follows.

It sends a 100PX wide picture of anything it understands in the
database: speed is about ten times greater than with the original mostly
480px wide images.

The actual code that calls the sub file is
printf("<IMG src=\"send_thumbnail.php?id=%d\"
and send_thumbnail.php itself is..

$privilege_level=0; // only we an access anything..external users must
include('shoplib.php'); // deals with privilege levels and database
// include('mimelib.php'); //Always use JPEG now!!
open_database(); // ready to check
$query="select picture from product where id='".$id."'";
//echo $query;
if(($result>0) && (($rows=mysql_numrows($result)) == 1)) //got some data
else die();
if ($name="") die();

// now to shrink the picture..
// get sizes
// our thumbnails are 100px wide..dont care about the height so scale as
$thumbnail=imagecreatetruecolor($newwidth,$newheight); // make empty new
header("Content-Type: image/jpeg");
imagejpeg( $thumbnail,null,75);

Re: Making thumbnails 'on the fly'.

.oO(The Natural Philosopher)

Quoted text here. Click to load it

You should definitely add some caching to store the resized images on
disk (outside the document root) or in the DB as well. Scaling images is
one of the most time-consuming processes. You don't want to do the same
thing over and over again, since this just creates another bottleneck.


Re: Making thumbnails 'on the fly'.

Michael Fesser wrote:
Quoted text here. Click to load it
Good point, but in this case it not necessary here at this point.

The machine is a small commercial server on the end of an ADSL line
essentially 448Kbps as far as downloading FROM it goes..

If it were to get 1000 hits a day we would be over the moon.
Its does noting else BUT display HTML stuff largely, so has plenty spare

The original images are not huge..mostly 480-640 pixels wide...its
purely to display shop style product shots so we don't have a huge
amount of RAM or processing power being taken up either. In any case RAM
is cheap.

The reason to resize was taken because we had an excess of processing
power over bandwidth.

Ecah picture in the list is a separate threaded instance of php/apache
anyway, so its not a huge per prioess load. Just a lot of little
processes. ;-)

Re: Making thumbnails 'on the fly'.

Michael Fesser wrote:

Quoted text here. Click to load it

...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).

Re: Making thumbnails 'on the fly'.

Victor Remose wrote:
Quoted text here. Click to load it
Well, if I had thought about it first, I would have made them on the fly
when uploaded and stuffed them in the well..but suh is history.

It's a quick hack to solve a problem *well enough* for now...

Re: Making thumbnails 'on the fly'.

The Natural Philosopher wrote:

Quoted text here. Click to load it

Well, why not (now) create a new column in your database, and then write a
quick script to run through the database and create all the thumbnails for
records "WHERE thumbnail IS NULL"? Then run that every night?

Toby A Inkster BSc (Hons) ARCS
[Geek of HTML/SQL/Perl/PHP/Python/Apache/Linux]
[OS: Linux, up 20 days, 18:15.]

                               Bottled Water

Re: Making thumbnails 'on the fly'.

Greetings, The Natural Philosopher.
In reply to Your message dated Sunday, February 17, 2008, 00:35:01,

Quoted text here. Click to load it

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

        $img = imagecreatefromjpeg(GALLERY_BASE_DIR."/");
        $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;
            $dx = $dy * $sx / $sy;

        imagecopyresampled($img2 = imagecreatetruecolor($dx, $dy), $img,
        0, 0, 0, 0,
        $dx, $dy, $sx, $sy);
        imagejpeg($img2, GALLERY_BASE_DIR."/.preview/", 90);
      header('Content-type: image/jpeg');

GALLERY_BASE_DIR typically protected and/or restricted to only .php files
being accessible.


Re: Making thumbnails 'on the fly'.

FYI, phpThumb support a lot of different formats, caching, and some
other interesting features: /

Quoted text here. Click to load it

Site Timeline