User Tools

Site Tools


create_thumbs

CreateThumbs Script

Created as a 'companion script' to go with my One File PHP Photo Gallery this script can be dropped in a web folder with your main photos and of course the One File PHP Photo Gallery script! Pointing your browser at the script will execute it and there will a short delay (or a longer delay depending on how many photo's you have) prior to a summary of the the thumbnails that have been created!

There is no actual output during the running of the script, only upon completion.

Again you are warned that I am no PHP expert and there may be a lot better ways of doing this, and possibly more secure too?

The Code

<!DOCTYPE html>
<html>
 <head>
  <title>Create Thumbs</title>
 </head>
<body>
<?php
// Give script enough time to run through many pics
set_time_limit(0);

// Change all filenames to lowercase
$directory=dirname(__FILE__);
$files = scandir($directory);
foreach($files as $key=>$name){
	$oldName = $name;
	$newName = strtolower($name);
	rename("$directory/$oldName","$directory/$newName");
        }

// Define directories
$imageDir = '';
$thumbsDir = 'thumbs/';
// Check if thumbnails are already created?
if (!is_dir($thumbsDir))
{ 
  @mkdir($thumbsDir);
  //  Create thumbs directory then read in images
   foreach(glob("$imageDir{*.jpg}", GLOB_BRACE) as $images) {
     $imageName = explode("/", $images);   
     $imageName = end($imageName);

     // Echo short message to user - only outputs at end?
      echo nl2br("Thumbnail created from image file: " .$imageName. "\n");

      // Load image and get image size
      $image = imagecreatefromjpeg( "{$imageDir}{$imageName}" );
      $width = imagesx( $image );
      $height = imagesy( $image );

      // Test for landscape or portrait image and set thumb width
      if ($width > $height) {
          $thumbWidth = 100;
          }
          else {
          $thumbWidth = 56;
      }    
      
      // Calculate size of thumbnail
      $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 
      imagecopyresampled( $tmp_img, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height );

      // Append _th to image name to mark as thumbnail
      $imageName = substr_replace($imageName , '_th', -4, 0);
      
      // Write out thumbail to thumbs directory with quality 100
      imagejpeg( $tmp_img, "{$thumbsDir}{$imageName}", 100);
      }
}
 else {
      // Echo short message to user
      echo ("Thumbnails already created for this folder!");
      }
?>
</body>
</html>

Installation

Copy / paste the above into a file called CreateThumbs.php and save. Any time you want to add new photos to the One File PHP Photo Gallery also copy this script for a one time run to create the necessary thumbnails.

Note: Smartphones like the iPhone will always take landscape images but set the EXIF Orientation Flag so that images taken in portrait are displayed on the device as portrait. It took me a while to figure out what was going on especially when Windows 10 exhibited the same behaviour. You could use a tool like http://www.exifdataviewer.com to zero out the orientation flag and just rotate the image to portrait mode using your photo viewer or graphics tool - have fun!

Note2: On Linux the folder you run the scripts in must have the same ownership permissions as the http server apache, lighttpd etc

create_thumbs.txt · Last modified: 2024/05/01 19:45 by admin