17.Sep.2008 at 17 | sam
Create thumbnails for all images in a directory
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: | thumbnailer.zip |
|---|---|
| Version: | 1.0 |
| Updated: | October 30, 2008 |
| Size: | 5.18 KB |
Powered by Drain Hole
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
<?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="http://www.phpace.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