This tutorial will describe how to create thumbnail images on the fly using PHP. Furthermore you will learn how to process a whole folder of images and create their thumbnails. Since this requires the GD library, you will need an installation of PHP with at least GD 2.0.1 enabled.
Below we will create a PHP script that contains two functions. The first one scans a provided directory for any .JPG images and, for each one, creates a thumbnail in the specified folder using the GD image functions. The second function creates an HTML file in the same directory as the script, which contains all of the thumbnails with links to the original images. This could be the basis of advanced photo gallery software.
The code below creates a function named createThumbs that will get three parameters. The first and the second is correspondingly the path to the directory that contains original images and the path to the directory in which thumbnails will be placed. The third parameter is the width you want for the thumbnail images.
<?php function createThumbs( $pathToImages, $pathToThumbs, $thumbWidth ) {
$dir = opendir( $pathToImages );
while (false !== ($fname = readdir( $dir ))) {
$info = pathinfo($pathToImages . $fname);
if ( strtolower($info['extension']) == 'jpg' ) { echo "Creating thumbnail for {$fname} <br />";
$img = imagecreatefromjpeg( "{$pathToImages}{$fname}" ); $width = imagesx( $img ); $height = imagesy( $img );
$new_width = $thumbWidth; $new_height = floor( $height * ( $thumbWidth / $width ) );
$tmp_img = imagecreatetruecolor( $new_width, $new_height );
imagecopyresized( $tmp_img, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height );
imagejpeg( $tmp_img, "{$pathToThumbs}{$fname}" ); } }
closedir( $dir ); }
createThumbs("upload/","upload/thumbs/",100); ?>
At first we open the directory with images and iterate through it, looking for all .JPG files. Next we create thumbnails for each image in the directory. To create a thumbnail, we read in the file using the imagecreatefromjpeg() function and calculate the new thumbnail size. imagesx() and imagesy() functions return the width and height of the image respectively. Next we create a new image using the imagecreatetruecolor(). Finally, we copy and resize the original file with the imagecopyresized() function and save thumbnail with imagejpeg().
The second part of the code creates the function named createGallery which gets two parameters (the relative paths to the directories in which images and thumbnails are stored) and creates an HTML page which contains all of the thumbnails with links to the original images.
<?php function createGallery( $pathToImages, $pathToThumbs ) { echo "Creating gallery.html <br />";
$output = "<html>"; $output .= "<head><title>Thumbnails</title></head>"; $output .= "<body>"; $output .= "<table cellspacing=\"0\" cellpadding=\"2\" width=\"500\">"; $output .= "<tr>";
$dir = opendir( $pathToThumbs );
$counter = 0;
while (false !== ($fname = readdir($dir))) {
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>"; } } }
closedir( $dir );
$output .= "</tr>"; $output .= "</table>"; $output .= "</body>"; $output .= "</html>";
$fhandle = fopen( "gallery.html", "w" );
fwrite( $fhandle, $output );
fclose( $fhandle ); }
createGallery("upload/","upload/thumbs/"); ?>
First, we open the directory with thumbnails. Next we iterate through files in the directory and put the HTML into a string variable. The variable contents then written into a file using fopen(), fwrite(), and fclose() functions.
As you can see, adding on the fly generated thumbnails to your website with GD library and PHP is quite easy to accomplish.
Back to top
Related Articles
PHP: Looping Statements
Dynamic Image Generation using PHP
|