Download sourcethumbnail

217
-1

Published on

Published in: Technology, Art & Photos
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
217
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
1
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Download sourcethumbnail

  1. 1. Download SourceThis tutorial will describe how to create thumbnail images on the fly using PHP. Furthermore you will learprocess a whole folder of images and create their thumbnails. Since this requires the GD library, you willinstallation 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 forimages and, for each one, creates a thumbnail in the specified folder using the GD image functions. Thfunction creates an HTML file in the same directory as the script, which contains all of the thumbnails witthe 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 thecorrespondingly the path to the directory that contains original images and the path to the directorythumbnails will be placed. The third parameter is the width you want for the thumbnail images.<?phpfunction createThumbs( $pathToImages, $pathToThumbs, $thumbWidth ){ // open the directory $dir = opendir( $pathToImages ); // loop through it, looking for any/all JPG files: while (false !== ($fname = readdir( $dir ))) { // parse path for the extension $info = pathinfo($pathToImages . $fname); // continue only if this is a JPEG image if ( strtolower($info[extension]) == jpg ) { echo "Creating thumbnail for {$fname} <br />"; // load image and get image size $img = imagecreatefromjpeg( "{$pathToImages}{$fname}" ); $width = imagesx( $img ); $height = imagesy( $img ); // calculate thumbnail size $new_width = $thumbWidth; $new_height = floor( $height * ( $thumbWidth / $width ) ); // create a new temporary image $tmp_img = imagecreatetruecolor( $new_width, $new_height ); // copy and resize old image into new image imagecopyresized( $tmp_img, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height ); // save thumbnail into a file imagejpeg( $tmp_img, "{$pathToThumbs}{$fname}" ); } } // close the directory closedir( $dir );}// call createThumb function and pass to it as parameters the path// to the directory that contains images, the path to the directory
  2. 2. // in which thumbnails will be placed and the thumbnails width.// We are assuming that the path will be a relative path working// both in the filesystem, and through the web for linkscreateThumbs("upload/","upload/thumbs/",100);?>At first we open the directory with images and iterate through it, looking for all .JPG files. Next wthumbnails for each image in the directory. To create a thumbnail, we read in the fitheimagecreatefromjpeg() function and calculate the new thumbnail size. imagesx() and imagesy() functiothe width and height of the image respectively. Next we create a new image using theimagecreatetruFinally, we copy and resize the original file with the imagecopyresized() function and save twith imagejpeg().The second part of the code creates the function named createGallery which gets two parameters (thepaths to the directories in which images and thumbnails are stored) and creates an HTML page which contathe thumbnails with links to the original images.<?phpfunction 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>"; // open the directory $dir = opendir( $pathToThumbs ); $counter = 0; // loop through the directory while (false !== ($fname = readdir($dir))) { // strip the . and .. entries out if ($fname != . && $fname != ..) { $output .= "<td valign="middle" align="center"><a href="{$pathToImages}{$fname}">"; $output .= "<imgsrc="{$pathToThumbs}{$fname}" border="0" />"; $output .= "</a></td>"; $counter += 1; if ( $counter % 4 == 0 ) { $output .= "</tr><tr>"; } } } // close the directory closedir( $dir ); $output .= "</tr>"; $output .= "</table>"; $output .= "</body>"; $output .= "</html>"; // open the file $fhandle = fopen( "gallery.html", "w" ); // write the contents of the $output variable to the file fwrite( $fhandle, $output ); // close the file fclose( $fhandle );}
  3. 3. // call createGallery function and pass to it as parameters the path// to the directory that contains images and the path to the directory// in which thumbnails will be placed. We are assuming that// the path will be a relative path working// both in the filesystem, and through the web for linkscreateGallery("upload/","upload/thumbs/");?>First, we open the directory with thumbnails. Next we iterate through files in the directory and put the HTstring 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 quiteaccomplish.

×