create_thumbs
Differences
This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
create_thumbs [2024/05/01 19:34] – created admin | create_thumbs [2024/05/01 19:45] (current) – admin | ||
---|---|---|---|
Line 1: | Line 1: | ||
==== CreateThumbs Script ==== | ==== CreateThumbs Script ==== | ||
- | Created as a ' | + | Created as a ' |
- | + | ||
- | Pointing your browser at the script will execute it and there will a short delay (or a longer delay depending on how many photo' | + | |
There is no actual output during the running of the script, only upon completion. | 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? | 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 === | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | </ | ||
+ | < | ||
+ | <?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=> | ||
+ | $oldName = $name; | ||
+ | $newName = strtolower($name); | ||
+ | rename(" | ||
+ | } | ||
+ | |||
+ | // Define directories | ||
+ | $imageDir = ''; | ||
+ | $thumbsDir = ' | ||
+ | // Check if thumbnails are already created? | ||
+ | if (!is_dir($thumbsDir)) | ||
+ | { | ||
+ | @mkdir($thumbsDir); | ||
+ | // Create thumbs directory then read in images | ||
+ | | ||
+ | | ||
+ | | ||
+ | |||
+ | // Echo short message to user - only outputs at end? | ||
+ | echo nl2br(" | ||
+ | |||
+ | // Load image and get image size | ||
+ | $image = imagecreatefromjpeg( " | ||
+ | $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, | ||
+ | |||
+ | // Append _th to image name to mark as thumbnail | ||
+ | $imageName = substr_replace($imageName , ' | ||
+ | | ||
+ | // Write out thumbail to thumbs directory with quality 100 | ||
+ | imagejpeg( $tmp_img, " | ||
+ | } | ||
+ | } | ||
+ | else { | ||
+ | // Echo short message to user | ||
+ | echo (" | ||
+ | } | ||
+ | ?> | ||
+ | </ | ||
+ | </ | ||
+ | </ | ||
+ | === Installation === | ||
+ | |||
+ | Copy / paste the above into a file called // | ||
+ | |||
+ | ### | ||
+ | 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:// | ||
+ | ### | ||
+ | |||
+ | 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.1714592085.txt.gz · Last modified: 2024/05/01 19:34 by admin