Making thumbnails 'on the fly'.

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • The Natural Philosopher

    Making thumbnails 'on the fly'.

    I have a databse containing amongst other things many images stored as
    BLOBS.

    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.


  • Norman Peelman

    #2
    Re: Making thumbnails 'on the fly'.

    The Natural Philosopher wrote:
    I have a databse containing amongst other things many images stored as
    BLOBS.
    >
    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.
    >
    >
    imagecreatefrom string() is your friend. Then just resize to your liking
    and output as normal.

    --
    Norman
    Registered Linux user #461062

    Comment

    • The Natural Philosopher

      #3
      Re: Making thumbnails 'on the fly'.

      Norman Peelman wrote:
      The Natural Philosopher wrote:
      >I have a databse containing amongst other things many images stored as
      >BLOBS.
      >>
      >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.
      >>
      >>
      >
      imagecreatefrom string() is your friend. Then just resize to your liking
      and output as normal.
      >
      Looks good.Any special libraries needed? having hell trying to install
      imagemagick..

      Comment

      • The Natural Philosopher

        #4
        Re: Making thumbnails 'on the fly'.

        Norman Peelman wrote:
        The Natural Philosopher wrote:
        >I have a databse containing amongst other things many images stored as
        >BLOBS.
        >>
        >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.
        >>
        >>
        >
        imagecreatefrom string() is your friend. Then just resize to your liking
        and output as normal.
        >
        Well MANY thanks..

        After struggling to get Gdlib on one machine..it 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_thum bnail.php?id=%d \"
        width=\"100px\" >",$product_id) ;
        ?>
        and send_thumbnail. php itself is..

        <?php
        $privilege_leve l=0; // only we an access anything..exter nal users must
        match,
        include('shopli b.php'); // deals with privilege levels and database
        opening..
        // include('mimeli b.php'); //Always use JPEG now!!
        open_database() ; // ready to check
        $id=$_GET['id'];
        $query="select picture from product where id='".$id."'";
        //echo $query;
        $result=mysql_q uery($query);
        if(($result>0) && (($rows=mysql_n umrows($result) ) == 1)) //got some data
        {
        $content=mysql_ result($result, 0,'picture');
        }
        else die();
        if ($name="") die();

        // now to shrink the picture..
        $im=imagecreate fromstring($con tent);
        // get sizes
        $width=imagesx( $im);
        $height=imagesy ($im);
        // our thumbnails are 100px wide..dont care about the height so scale as
        width
        $newheight=roun d(($height*100)/$width);
        $newwidth=100;
        $thumbnail=imag ecreatetruecolo r($newwidth,$ne wheight); // make empty new
        wotsit.
        imagecopyresamp led($thumbnail,
        $im,0,0,0,0,$ne wwidth,$newheig ht,$width,$heig ht);
        header("Content-Type: image/jpeg");
        imagejpeg( $thumbnail,null ,75);
        ?>

        Comment

        • Victor Remose

          #5
          Re: Making thumbnails 'on the fly'.

          Michael Fesser wrote:
          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).


          Comment

          • The Natural Philosopher

            #6
            Re: Making thumbnails 'on the fly'.

            Victor Remose wrote:
            Michael Fesser wrote:
            >
            >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...

            Comment

            • petersprc

              #7
              Re: Making thumbnails 'on the fly'.

              FYI, phpThumb support a lot of different formats, caching, and some
              other interesting features: http://phpthumb.sourceforge.net/

              On Feb 16, 6:28 am, The Natural Philosopher <a...@b.cwrot e:
              I have a databse containing amongst other things many images stored as
              BLOBS.
              >
              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.

              Comment

              • Toby A Inkster

                #8
                Re: Making thumbnails 'on the fly'.

                The Natural Philosopher wrote:
                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.
                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 2.6.17.14-mm-desktop-9mdvsmp, up 20 days, 18:15.]

                Bottled Water

                Comment

                • AnrDaemon

                  #9
                  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($n ame)
                  {
                  $fname = filesFilter($na me);
                  if(false === $fname)
                  {
                  echo '<html><body><h 3>404 No file &quot;'.htmlspe cialchars($name ).'&quot;</h3></body></html>';
                  }
                  else
                  {
                  if(!file_exists (GALLERY_BASE_D IR."/.preview/{$fname}"))
                  {
                  if(!file_exists (GALLERY_BASE_D IR.'/.preview'))
                  {
                  mkdir(GALLERY_B ASE_DIR.'/.preview');
                  }

                  $img = imagecreatefrom jpeg(GALLERY_BA SE_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;
                  }

                  imagecopyresamp led($img2 = imagecreatetrue color($dx, $dy), $img,
                  0, 0, 0, 0,
                  $dx, $dy, $sx, $sy);
                  imagejpeg($img2 , GALLERY_BASE_DI R."/.preview/{$fname}", 90);
                  }
                  header('Content-type: image/jpeg');
                  readfile(GALLER Y_BASE_DIR."/.preview/{$fname}");
                  }
                  }

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


                  --
                  Sincerely Yours, AnrDaemon <anrdaemon@free mail.ru>

                  Comment

                  Working...