<?php

$filearray = array(); if ($fil = @opendir("images/")) {
while (($file = readdir($fil)) !== false) {
if ($file != "." && $file != "..") { $filearray[] = $file;

?>

<img scr="<?php echo $file; ?>" border=0>

<?php } ?>

So, this will display all images in the /images/ directory - but it will display it at full size, I want to resize these images down to thumbnail size so it dosnt take too long to download... and I dont want to save the thumbnails - I just want them to be created at the time of viewing and preserve the original images.

Any help?

Thanks
-Arron

    Have a look at the GD Lib [man]Image[/man] functions.

      As Weedpacket stated there are special functions used to do that. I did this recently. This is what to do:
      you put the code below in a new php file that you save in your images directory; you might name this file resize_image.php. This is the code:

      <?php

      //set the width of the thumbnail
      (double)$max_width = 150;
      //set the height of the thumbnail
      (double)$max_height = 100;
      //take the size of the original image
      $size = GetImageSize("$image");
      (double)$width = $size[0];
      (double)$height = $size[1];
      //make calculations for the redimensioning of the original image into a thumbnail,
      // keeping proportions intact
      $x_ratio = $max_width/$width;
      $y_ratio = $max_height/$height;
      if (($width <=$max_width)&&($height<=$max_height))
      {
      $tn_width = $width;
      $tn_height = $height;

      }
      else if (($x_ratio$height)<$max_height) {
      $tn_height = ceil($x_ratio
      $height);
      $tn_width = $max_width;
      }
      else {
      (double)$tn_width = ceil($y_ratio*$width);
      $tn_height = $max_height;
      //echo $tn_width;
      //echo $tn_height;
      //exit;
      }
      //create the thumbnail for a JPEG image, use ImageCreateFromGIF() for GIFs
      $src = ImageCreateFromJPEG("$image");
      $dst = ImageCreate($tn_width, $tn_height);

      ImageCopyResized($dst, $src, 0,0,0,0, $tn_width, $tn_height, $width, $height);
      error_reporting(0);
      header("Content-type: image/jpeg");
      ImageJPEG($dst, null, -1);
      ImageDestroy($src);
      ImageDestroy($dst);

      ?>

      In your actual page you use this line for the <src> tag:
      <?php
      echo "<img src=\"images\/resize_image.php?image=".$file.".jpg\">";
      ?>

      You let me know whether this worked for you.

        Hm, just wondering why the thumb quality is so low? How can I boost this? ( sorry I am GD inept ) 😛

          It depends on the size of your original picture. If your pics are big you might have to change the thumbnail size of 150x100 to something bigger, say, 200x150 , if you want more clarity. Or the reverse is to reduce the size of the original pics.

            listenmirndt wrote:

            Hm, just wondering why the thumb quality is so low? How can I boost this? ( sorry I am GD inept ) 😛

            Try changing this:

            $dst = ImageCreate($tn_width, $tn_height);

            to this:

            $dst = ImageCreateTrueColor($tn_width, $tn_height);

            And also this:

            ImageCopyResized($dst, $src, 0,0,0,0, $tn_width, $tn_height, $width, $height);

            to this:

            ImageCopyResampled($dst, $src, 0,0,0,0, $tn_width, $tn_height, $width, $height);

            Let me know how that works out.

              That is a lot better, it was looking like 256 color before, now its looking good.. just a bit of jpeg artifacting, the quality looks like about 50-60%..

              Ive seen some methods to set the thumbnail quality in some GD script somwhere... perhaps Il have a dig around...

              Thanks alot to both of you!

                Write a Reply...