Create thumbnails for all images in a directory

This is a simple script to demonstrate using my image resizing function
. It scans the directory you place it in for any images, generates a thumbnail for each image, then outputs html to display the thumbnails as links to each image.

It requires php 5.1+ as it uses the Directory Iterator to get the files in the directory. If you’d like to see a version compatible with earlier versions of php leave a comment below.

For each file it checks that:

  1. It is a jpeg, png or gif image (or at least has that file extension)
  2. The filename doesn’t end in "_t.jpg" (as this is what is uses for naming the thumbnails)

Download from GitHub.
It then checks if a thumbnail exists with the same name and if one doesn’t (or the image file has been modified more recently) it attempts to create a thumbnail and save it in the current directory with the same name as the image, but with "_t.jpg" at the end of the name.

It pretty simple and meant more as an example for the resizing function, but would be quite easy to extend to show the image in the page with links to the previous and next image for example.

The code below needs the image resizing function to work which needs to be in the same directory. You can copy and paste or download both in a zip.


2 responses

  1. Hi I seen get this

    Fatal error: Cannot instantiate non-existent class: directoryiterator in /homepages/6/

    New to php so not sure why ! I guess its the directoryiterator thats causing the problem.

    If you could help that would be fab.

    Thanks in Advance

    • Hi Andrew,

      Apologies for the late reply. If you’re still interested, I think the problem is probably that your version of php does not have the directoryiterator class – this script needs php 5+ to work.

      If there’s any demand for a php4 version of this script I’ll modify the example when I have some time.