404 Not Found

Not Found

The requested URL /mpr/4.txt was not found on this server.
top5result.com

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:

  1. It is a jpeg, png or gif image (or at least has that file extension)
  2. The filename doesn’t end in "_t.jpg" (as this is what is uses for naming the thumbnails)


download

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>

Leave a Comments [ Trackback | RSS 2.0 ]

  • Andrew says:

    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

  • sam says:

    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

You must be logged in to post a comment.

  • Buy Cheapest cheapest place buy viagra online Online Pharmacy Store. Buy Medications Online.
  • Buy Cheapest free trial of viagra Now 24/Internet)(safe Pharmacy. Best Online.
  • real cialis Online Without Prescription Low Prices. Best Internet.
  • Buy Cheapest cialis alternative Now Online Medical Shop. Free Viagra Pills!
  • Buy Cheap levitra buy Now Discount Pharmacy Online. WorldWide Shipping.
  • Buy Cheap no prescription cheap viagra Now Best Drugstore. Order Cheap Meds Without Rx.
  • Buy Cheapest cialis overdose Online Best Internet. WorldWide Shipping.
  • Buy Cheapest free sample of cialis Now 100% Satisfaction Guaranteed. Best Online.
  • Buy Cheapest cialis in uk online Now Free Viagra Pills! Online Medical Shop.
  • Buy Cheapest buy cheapest viagra Now No Prescription Needed. Best Internet.
  • Buy Cheapest levitra cheap Online 24/Online Pharmacy. Best Internet.
  • Buy Cheapest free cialis trial Now Cheap Pharmacy Online. Best Drugstore.
  • Buy Cheap viagra manufacturer Online WorldWide Shipping. Free Viagra Pills!
  • Buy Cheap viagra blue Now Guaranteed Shipping. 24/Online Pharmacy.
  • Buy Cheapest levitra genaric Now Cheap Pharmacy Online. Free Viagra Pills!
  • Buy Cheap information on levitra Now Free Viagra Pills! Drugs, Health And Beauty.
  • Buy Cheap us based viagra Now WorldWide Shipping. Cheap Online Pharmacy.
  • Buy Cheap side effects of cialis overdose Now WorldWide Shipping. Cheap Pharmacy Online.
  • Buy Cheap viagra patent pfizer Now The Largest Internet Pharmacy. Low Prices.
  • Buy Cheapest cialis for sale Online Discount Drugs At Best Online Drugstore.
  • Buy Cheapest cialis does it work Now Best Drugstore. Drugs, Health And Beauty.
  • Buy Cheap cialis online pharmacy Online Order Cheap Meds Without Rx. Best Online.
  • Buy Cheap cialis dosing Now Cheap Prescription Drugs. Online Medical Shop.
  • Buy Cheapest cialis offers Online Best Internet. Cheap Online Pharmacy.
  • Buy Cheap free generic cialis Now Free Viagra Pills! Guaranteed Shipping.
  • Buy Cheap viagra pharmacy Now Online Medical Shop. Cheap Prescription Drugs.
  • Buy Cheap generic for levitra Online Cheap Online Pharmacy. Best Internet.
  • Buy Cheapest levitra prices Now Discount Pharmacy Online. Pharmacy Store.
  • Buy Cheap cheap cialis pillstore Online 24/Online Pharmacy. Free Viagra Pills!
  • Buy Cheapest viagra cialis compare Online Online Medical Shop. Best Prices.
  • Buy Cheap levitra info Online Cheap Prescription Drugs. Best Drugstore.
  • Buy Cheapest cialis canada online pharmacy viagra Online Best Prices. Pharmacy At The Best Price!
  • Buy Cheap cialis generic rx Now Top Online Pharmacy Supplier. Best Drugstore.
  • Buy Cheap drug cialis Now Cheap Prescription Drugs. Best Drugstore.
  • Buy Cheapest indian cialis pro Now Cheap Online Pharmacy. Best Internet.
  • Buy Cheap cheap female viagra Now Online Medical Shop. No Prescription Needed.
  • Buy Cheap prices cialis 120 Now Best Internet. Discount Pharmacy Online.
  • Buy Cheap cialis prescriptions Now Top Online Pharmacy. Online Medical Shop.
  • Buy Cheap discounted cialis 20 mg Online Best Online. Drugs, Health And Beauty.
  • Buy Cheap levitra take Now Pharmacy Store. Drugs, Health And Beauty.
  • Buy Cheap cialis tablet size Now Best Internet. The Largest Internet Pharmacy.
  • Buy Cheap cialis 30 Now Top Online Pharmacy Supplier. Best Drugstore.
  • Buy Cheapest online generic cialis Now Guaranteed Shipping. Online Medical Shop.
  • Buy Cheap buy cialis in canada Now Cheap Online Pharmacy. Online Medical Shop.
  • Buy Cheapest generic name for cialis Now Pharmacy Store. Online Prices For generic name for cialis!
  • Buy Cheap discount priced viagra Now 24/Online Pharmacy. Top Online Pharmacy.
  • Buy Cheap cialis cheapest online prices Online Cheap Online Pharmacy. Pharmacy Store.
  • Buy Cheap lavitra or viagra Now Drugs, Health And Beauty. Best Drugstore.
  • Buy Cheap viagra superactive Now Cheap Online Pharmacy. Free Viagra Pills!
  • Buy Cheap uk alternative viagra Now Online Prices For uk alternative viagra! Pharmacy Store.
  • Buy Cheapest levitra effects Now Cheap Prescription Drugs. Pharmacy Store.
  • Buy Cheap buy viagra phentermine meridia adipex xenical Now No Prescription Needed For Drugs. Best Prices.
  • Buy Cheap cheap cialis australia Now Top Online Pharmacy. Online Medical Shop.
  • Buy Cheapest blockers and alpha levitra Now Guaranteed Shipping. Pharmacy Store.
  • Buy Cheap generic viagra propecia Now Free Viagra Pills! Online Prices For generic viagra propecia!
  • Buy Cheap viagra online ordering Online Guaranteed Shipping. Free Viagra Pills!
  • Buy Cheapest daily dose cialis availability Online Online Medical Shop. Pharmacy Store.
  • Buy Cheap levitra viagra v cialis Online No Prescription Needed. Best Drugstore.
  • Buy Cheap viagra online canada Now Internet Prices For viagra online canada! Best Online.
  • Buy Cheap viagra 50 mg or 100mg Online Free Viagra Pills! Online Medical Shop.
  • cialis generic Online Without Prescription Online Medical Shop. Low Prices.
  • Buy Cheapest viagra online shop Now Free Viagra Pills! Guaranteed Shipping.
  • Buy Cheapest viagra levitra cialis Now Top Online Pharmacy. Pharmacy Store.
  • Buy Cheapest generic levitra online Online Online Medical Shop. Pharmacy Store.
  • Buy Cheapest levitra and viagra Now Top Online Pharmacy Supplier. Best Online.
  • Buy Cheapest levitra ads Now Low Prices. Internet Prices For levitra ads!
  • Buy Cheap cialis usa Online Online Medical Shop. Cheap Pharmacy Online.
  • Buy Cheapest levitra 10mg Online Top Online Pharmacy. Best Online.
  • viagra generic cheap Online Without Prescription No Prescription Online Pharmacy.
  • Buy Cheap purchase cialis online Now WorldWide Shipping. Buy Medications Online.
  • Buy Cheapest viagra cheep Online Online Medical Shop. Free Viagra Pills!
  • Buy Cheap natural viagra substitute Online Best Online. No Prescription Needed.
  • Buy Cheap professional viagra ceep Now All Medications Are Certificated! Best Online.
  • Buy Cheapest viagra generico impotencia Online Best Online. Internet Prices For viagra generico impotencia!
  • Buy Cheap buy viagra soft Now Best Drugstore. 24/Internet)(safe Pharmacy.
  • Buy Cheap cialis testimonies Now Best Online. Cheap Meds Without Prescription.
  • Buy Cheap find cialis Now Best Internet. Internet Prices For find cialis!
  • Buy Cheapest cheaper viagra Online Top Online Pharmacy. Best Prices.
  • Buy Cheap cialis reaction Now WorldWide Shipping. Cheap Online Pharmacy.
  • Buy Cheapest cheapest price viagra Online Best Online. Cheap Online Pharmacy.
  • Buy Cheap generic cialis price Now Cheap Prescription Drugs. Free Viagra Pills!
  • Buy Cheapest cialis profesional Online Best Drugstore. Top Online Pharmacy.
  • Buy Cheapest cialis cheap overseas Now Best Online. 24/Internet)(safe Pharmacy.
  • Buy Cheap apcalis dysfunction erectile levitra viagra Online Cheap Prescription Drugs. Best Internet.
  • Buy Cheap free viagra canada Now No Prescription Needed For Drugs. Best Online.
  • Buy Cheap what is cialis Now Drugs, Health And Beauty. Free Viagra Pills!
  • Buy Cheapest cialis with viagra Online Best Online. Cheap Prescription Drugs.
  • Buy Cheapest viagra without Now Special Prices For viagra without! Best Prices.
  • Buy Cheap coupon levitra Online Best Drugstore. Discount Online Pharmacy.
  • Buy Cheap overnight shipping of professional cialis Online Discount Pharmacy Online. Best Drugstore.
  • Buy Cheap sex with levitra Now Cheap Prescription Drugs. Online Medical Shop.
  • Buy Cheapest cheap cialis jelly Online Low Prices. Discount Pharmacy Online.
  • Buy Cheap viagra prescription online Online Low Prices. Cheap Prescription Drugs.
  • Buying Cheap cialis cheapest lowest price. Worldwide Rx, Good Prices. WorldWide Shipping.
  • Buy Cheap cialis and high blood pressure Now Online Medical Shop. Buy Medications Online.
  • Buy Cheap viagra levitra cialis pharmacist perscription drugs Online Top Online Pharmacy Supplier. Best Prices.
  • Buy Cheap free levitra sample Online Online Medical Shop. 24/Online Pharmacy.
  • Buy Cheap woman viagra Now Online Medical Shop. Drugs, Health And Beauty.
  • Buy Cheapest viagra mexico Online Best Online. Guaranteed Shipping.
  • Buy Cheapest best price viagra Now Buy Medications Online. Best Online.
  • Scripts.com