album script – Hello :) https://blog.samyapp.com Wed, 20 Apr 2016 09:04:51 +0000 en-US hourly 1 https://wordpress.org/?v=4.8.7 102402223 Create thumbnails for all images in a directory https://blog.samyapp.com/create-thumbnails-for-all-images-in-a-directory/ https://blog.samyapp.com/create-thumbnails-for-all-images-in-a-directory/#comments Wed, 17 Sep 2008 16:24:53 +0000 https://blog.samyapp.com/?p=134 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 […]

The post Create thumbnails for all images in a directory appeared first on Hello :).

]]>
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 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

<?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>

The post Create thumbnails for all images in a directory appeared first on Hello :).

]]>
https://blog.samyapp.com/create-thumbnails-for-all-images-in-a-directory/feed/ 2 134