This is a simple script to demonstrate using my image resizing function
. It scans the directory you place it in for any images, generates a thumbnail for each image, then outputs html to display the thumbnails as links to each image.
It requires php 5.1+ as it uses the Directory Iterator to get the files in the directory. If you’d like to see a version compatible with earlier versions of php leave a comment below.
For each file it checks that:
- It is a jpeg, png or gif image (or at least has that file extension)
- The filename doesn’t end in "_t.jpg" (as this is what is uses for naming the thumbnails)
Download from GitHub.
It then checks if a thumbnail exists with the same name and if one doesn’t (or the image file has been modified more recently) it attempts to create a thumbnail and save it in the current directory with the same name as the image, but with "_t.jpg" at the end of the name.
It pretty simple and meant more as an example for the resizing function, but would be quite easy to extend to show the image in the page with links to the previous and next image for example.
The code below needs the image resizing function to work which needs to be in the same directory. You can copy and paste or download both in a zip.
index.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
<?php // thumbnail configuration $thumb_width = 75; $thumb_height = 75; $thumb_method = 'crop'; $thumb_bgColour = null;//array(255,255,240); $thumb_quality = 60; // Let script run for as long as it needs to when creating thumbnails. // Some web hosts won't let you use this function. In that case you'll need // to comment it out and just refresh the script until it has thumbnailed all the images. set_time_limit(0); // include the thumbnail function require_once dirname(__FILE__) . '/paGdThumbnail.php'; // get the path to the current directory $path = dirname(__FILE__); // get the url for the images $path_info = pathinfo($_SERVER['SCRIPT_NAME']); $url = $path_info['dirname']; // create an array to store image names in. $images = array(); $dir = new DirectoryIterator($path); /* Loop through the directory, finding all images that aren't thumbnails. For each image: - if it doesn't already have a thumbnail, or the thumbnail is older than the image, create a thumbnail. - get info about the image - add the image to our images array */ foreach( $dir as $entry ){ if( $entry->isFile() ){ if( preg_match('#^(.+?)(_t)?.(jpg|gif|png)#i', $entry->getFilename(), $matches) ){ list( ,$name, $is_a_thumb, $extension) = $matches; // if its not a thumbnail if( !$is_a_thumb ){ // does a valid thumbnail exist? $has_thumb = false; $thumb_file = $path . '/' . $matches[1] . '_t.jpg'; if( file_exists($thumb_file) && filemtime($thumb_file) > filemtime($entry->getPathname()) ){ $has_thumb = true; } else{ // no thumbnail, so we shall create one! // create a gd image. reading the contents of the image file into a string, then // using imagecreatefromstring saves having to check the filetype and which // imagecreatefrom(jpeg/gif/png) function to use $image = imagecreatefromstring(file_get_contents($entry->getPathname())); if( $image ){ // create the thumbnail $thumb = paGdThumbnail($image, $thumb_width, $thumb_height, $thumb_method, $thumb_bgColour); // free the image resource imagedestroy($image); if( $thumb ){ // save the thumbnail if( imagejpeg($thumb, $thumb_file, $thumb_quality) ){ $has_thumb = true; } // free the memory used by the thumbnail image imagedestroy($thumb); } } } if( $has_thumb ){ $images[$entry->getFilename()] = $name; } } } } } // sort the images array so always in the same order ksort($images); // end or processing, now display the gallery ?> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <html> <head> <title>Thumbnails :p</title> <style type="text/css"> <!-- #thumbs{ position: relative; } #thumbs div{ float: left; width: <?php echo $thumb_width + 30?>px; height: <?php echo $thumb_height + 30?>px; text-align: center; } #thumbs a:link img, #thumbs a:visited img{ border: 1px solid #acacac; padding: 5px; } #thumbs a:hover img{ border: 1px solid black; } --> </style> </head> <body> <div id="thumbs"> <?php foreach( $images as $imagename => $name ){ ?> <div> <a href="<?php echo $url . '/' . $imagename?>" title="Full Size"><img src="<?php echo $url . '/' . $name . '_t.jpg'?>" /></a> </div> <?php }?> </div> <div id="pb"> powered by <a href="https://blog.samyapp.com/">a simple php image thumbnail script</a> </div> </body> </html> |
Hi I seen get this
Fatal error: Cannot instantiate non-existent class: directoryiterator in /homepages/6/
New to php so not sure why ! I guess its the directoryiterator thats causing the problem.
If you could help that would be fab.
Thanks in Advance
Andrew
Hi Andrew,
Apologies for the late reply. If you’re still interested, I think the problem is probably that your version of php does not have the directoryiterator class – this script needs php 5+ to work.
If there’s any demand for a php4 version of this script I’ll modify the example when I have some time.
Cheers,
Sam