Create thumbnails of all the images in a directory

This example reads a directory and creats thumbnails of all the images. It can be modified to only read certain files, the files can or can not be ovewriten, the filenames can be changed and the ImageMagick code can be modified to add watermarks etc.

The way I would go about this now using glob

<?php
// Setup the maximium width and height for the finished image
$max_width "200";
$max_height "90";

// Directory containing the images
$dir 'photos'

// Sellect all jpg, png and gif images
foreach (glob($dir."{*.jpg,*.png,*.gif}",GLOB_BRACE ) as $filename) {

// Get the image size for use in the code
$size=GetImageSize$filename );

$new_name "thumb_".$filename;

exec("convert -size {$size[0]}x{$size[1]} $filename -thumbnail $max_widthx$max_height $new_name"); 
}

?>

Another way of doing this; you would have more ways to build in ways to ignore specific files etc. using this method

<?php

// Setup the maximium width and height for the finished image
$max_width "200";
$max_height "90";

// Directory containing the images - need the trailing slash / 
$read 'photos/'
  
// Open the directory and create an array of the images 
$myDirectory opendir$read ); 
  
while( 
$entryName readdir$myDirectory )) 

// This will only select jpg images from the folder 
if ( strtolowersubstr$entryName, -) ) == "jpg" 
$dirArray[]=$entryName

    
closedir$myDirectory ); 
  
// Count the number of images 
$indexCount count($dirArray); 
  
for ( 
$i=0$i<$indexCount$i++ ) 


$name $read.$dirArray[$i]; 

// Save resized images as folder/thumb_original name
$new_name $read."thumb_".$dirArray[$i];

// Get the image size for use in the code
$size=GetImageSize$name ); 
exec("convert -size {$size[0]}x{$size[1]} $name -thumbnail $max_widthx$max_height $new_name"); 



?>