create_thumbs
This is an old revision of the document!
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>
create_thumbs.1714592358.txt.gz · Last modified: 2024/05/01 19:39 by admin