64x64 icon dark hosting
Choose a hosting plan here and get a free year's subscription to Tuts+ (worth $180).

How to Dynamically Create Thumbnails


Start a hosting plan from $3.92/mo and get a free year on Tuts+ (normally $180)

In this week's screencast, I'll show you how to upload files and then have PHP dynamically create a thumbnail. Whether you're building an ecommerce site, or just a simple gallery, these techniques will absolutely prove to be useful. If you're ready for your "spoonfed" screencast of the week, let's get going!

*Note - There have been a few slight changes to the code after some additional thinking and some great suggestions. Don't worry, very little has changed. Just some cleanup. You can review the changes below, and/or download the source code.

The Simple Config File

The first step is to create a simple config file where we can store a few variables. By placing these in their own file, we can easily make changes to our code without having to edit many lines.

  • $final_width_of_image - This variable will store the width of our thumbnail.
  • $path_to_image_directory - This stores the path to our full sized images folder
  • $path_to_thumbs_directory - This stores the path to our full thumbnails directory

Save this file as 'config.php' and place it in the root of your folder.



Next, create a new page called "index.php" and paste the following.

First, scroll down a bit to the body tag. To keep things as bare-bones as possible, I've created an extremely simple form. But it will get the job done just fine.

Any time that you're going to be working with the "file upload" input type, you need to add an "enctype" attribute to the form tag.

Rather than posting to a different page, we'll just write the code in our main document. To do that, we'll set the action attribute equal to this page.

Now, scroll back up to the PHP code at the top. We're requiring two files. The first is the config file that we just created. The second one is "functions.php" - which we'll create shortly.

Next, we're checking to see if the page has posted back. If it has, we'll then check to see if the file that the user chose was either a "jpg", "gif", or "png". We can do this by checking the file name against a regular expression.

To learn more about regular expressions, watch this screencast.

Moving along, we're creating a few variables.

  • $filename - Stores the name of the file that the user has chosen to upload.
  • $source - When the submit button is clicked, the file will be saved into a temporary directory. This variable will store that path.
  • $target - This will store the path of where the uploaded image should be saved.

Saving the File

The final step is to move the file from the temporary directory into our "images/fullsized" folder. We can do this by calling the move_uploaded_file() function. We'll pass in two parameters. The first one needs to know the path to the temporary folder. The second needs to know where to save the file. ($source and $target, respectively)

Creating the Thumbnail

Rather than store all of the code in our index.php page, let's create another page called "functions.php". Create and open this new file and write a new function called "createThumbnail()".

We'll start by requiring the "config.php" file once again. Next, we'll check to see whether the user selected a "jpg", "gif", or "png". We must do this because PHP requires a different function depending on the file: "imagecreatefromjpeg", "imagecreatefromgif", "imagecreatefrompng".


After that, we must store the width and height values of the image that the user chose to upload. We can do this by calling "imagesx", and "imagesy", respectively.

Next, we'll create two more variables that will store the width and height values for the thumbnail that will soon be created.

  • $nx - is equal to the value from our config file: 100
  • $ny. We'll need to run some simple math to find the correction proportionate height.



In our case, we're passing in the "$nx", and "$ny" variables that we just created.

Image Copy Resized


Saving the Thumbnail

The final steps require that we check to see if a "thumbnails" folder exists. If it doesn't, we'll create it by using "mkdir". Then, we'll output our new image to the thumbnails folder.


Finally, we need to echo out the thumbnail to show the user that his image was uploaded successfully.


Well done! This screencast was somewhat hastily done - because of time constraints. You might want to clean up the code a bit and a bit more error handling.

If you wish to take things further, see if you can figure out how to crop the images as well! As always, I'm more than open to refinement and suggestions!

  • Subscribe to the NETTUTS RSS Feed for more daily web development tuts and articles.