Thumbnail Creation via PHP

I got a bit tired of numerous security holes and sheer bulkiness of other thumbnail generation scripts, so decided to build my own from scratch, for local images only. Simply upload, create a writable cache directory and parse the image through the PHP script like so:

<img src='thumb.php?src=./images/photo.jpg&size=400x300' />

It's super simple and only 155 lines in total. You can specify the width and height, or just the width so the height will get calculated automatically and vice versa. It also allows you to set a maximum width/height, which is best suited for photo thumbnails.


  • File based cache
  • Automatic deletion of expired cache files
  • Optional browser based cache via the IF_MODIFIED_SINCE HTTP header
  • Multiple size types
  • Options whether to crop, trim, zoom and set alignment
  • Set sharpness based on a percentage midpoint between two values, so you can specify the strength of the sharpness rather than just turning it on or off

View the code and usage instructions on Github

18-Jul-2012 at 1:00pm

Joel wrote:

Nice script, will this suffer from having a huge number of cache files in one directory if you wanted the expiry to be a bit more long term?

Jamie Bicknell

Jamie Bicknell wrote:

The maximum number of cache files would be relative to the number of images processed through the script, so if you have 1000 images then there'd most likely always be 1000 cache images at some point.

Changing the expiry time means that there's simply more time to reach the maximum number of cache images.

  • (optional)
  • Security Code