Jump to content

On the fly thumbnails


dennismonsewicz

Recommended Posts

this is the one i use on my scouting website, if you want to see it in action have a look at

 

<a href="http://www.brompton-scouts.co.uk/info.php"> Brompton Scouts </a>

 

function createThumbs( $pathToImages, $pathToThumbs, $thumbWidth )
{

  // open the directory
  $dir = opendir( $pathToImages );

  // loop through it, looking for any/all JPG files:
  while (false !== ($fname = readdir( $dir ))) {
    // parse path for the extension
    $info = pathinfo($pathToImages . $fname);
    // continue only if this is a JPEG image
    if ( strtolower($info['extension']) == 'jpg' )
    {
           // load image and get image size
      $img = imagecreatefromjpeg( "{$pathToImages}{$fname}" );
      $width = imagesx( $img );
      $height = imagesy( $img );

      // calculate thumbnail size
      $new_width = $thumbWidth;
      $new_height = floor( $height * ( $thumbWidth / $width ) );

      // create a new temporary image
      $tmp_img = imagecreatetruecolor( $new_width, $new_height );

      // copy and resize old image into new image
      imagecopyresized( $tmp_img, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height );

      // save thumbnail into a file
      imagejpeg( $tmp_img, "{$pathToThumbs}{$fname}" );
    }	
  }
  // close the directory
  closedir( $dir );
}

function createGallery( $pathToImages, $pathToThumbs )
{
  $output = "<html>";
  $output .= "<head><title>Thumbnails</title></head>";
  $output .= "<body>";
  $output .= "<table cellspacing=\"0\" cellpadding=\"2\" width=\"500\">";
  $output .= "<tr>";

  // open the directory
  $dir = opendir( $pathToThumbs );

  $counter = 0;
  // loop through the directory
  while (false !== ($fname = readdir($dir)))
  {
    // strip the . and .. entries out
    if ($fname != '.' && $fname != '..')
    {
      $output .= "<td valign=\"middle\" align=\"center\"><a href=\"{$pathToImages}{$fname}\">";
      $output .= "<img src=\"{$pathToThumbs}{$fname}\" border=\"0\" />";
      $output .= "</a></td>";

      $counter += 1;
      if ( $counter % 4 == 0 ) { $output .= "</tr><tr>"; }
    }
  }
  // close the directory
  closedir( $dir );

  $output .= "</tr>";
  $output .= "</table>";
  $output .= "</body>";
  $output .= "</html>";

  // open the file
  $fhandle = fopen( "gallery.html", "w" );  // gallery.html has to exist even in name to start with or it throws an error
  // write the contents of the $output variable to the file
  fwrite( $fhandle, $output );
  // close the file
  fclose( $fhandle );
}

createThumbs( "images/general/", "images/gen_th/", 100 ); //you will have to use your locations obviously
createGallery("images/general/", "images/gen_th/" );

?>

 

forgot a big bit

 

add the following to your page to show the bloody thing when you have created it

 

<div align="center">
<iframe align="middle" frameborder="0" width="95%" height="400px" src="gallery.html" >
</iframe>
</div>

Link to comment
Share on other sites

I tried your script but It is displaying a blank page. like nothing loads in the iframe. This is going to sound like a newb question but,

 

When you say $dir = opendir( $pathToImages ); do i need to put in the path or keep the var the same?

You do not modify any of the code in the createThumbs or createGallery functions.

 

You need call them like so:

 

createThumbs( "images/general/", "images/gen_th/", 100 ); //you will have to use your locations obviously

createGallery("images/general/", "images/gen_th/" );

 

images/genral is the location of your images

images/gen_th is the path in which the generated thumbnails get stored to.

Link to comment
Share on other sites

apart from the two functions the following is what makes my page function

 

<form action="" method="post" name="formCubs" id="formCubs">
    <p class="style1"> Centenary Bench Dedication at Richmond Castle 8th December 2007     </p>
    <?php	
createThumbs( "images/general/", "images/gen_th/", 100 );
createGallery("images/general/", "images/gen_th/" );
?>
<div align="center">
<iframe align="middle" frameborder="0" width="95%" height="400px" src="gallery.html" >
</iframe>
</div>
<div align="center">
<img  id='finger' src="/images/bBack.jpg" alt="Back" width="110" height="50" onclick="history.go(-1)"/></div>
  </form>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.