dennismonsewicz Posted March 26, 2008 Share Posted March 26, 2008 can someone point me in the right direction on how to create thumbnails of images on the fly using php? Quote Link to comment Share on other sites More sharing options...
paul2463 Posted March 26, 2008 Share Posted March 26, 2008 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> Quote Link to comment Share on other sites More sharing options...
dennismonsewicz Posted March 26, 2008 Author Share Posted March 26, 2008 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? Quote Link to comment Share on other sites More sharing options...
wildteen88 Posted March 26, 2008 Share Posted March 26, 2008 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. Quote Link to comment Share on other sites More sharing options...
paul2463 Posted March 26, 2008 Share Posted March 26, 2008 does gallery.html exist already??? it needs to so it can be opened and re-written to Quote Link to comment Share on other sites More sharing options...
dennismonsewicz Posted March 26, 2008 Author Share Posted March 26, 2008 gallery.html was created when the script ran Quote Link to comment Share on other sites More sharing options...
paul2463 Posted March 26, 2008 Share Posted March 26, 2008 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> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.