How to Resize and Save Images with phpThumb

If you are not using WordPress, and therefore don’t have access to its built in image resizing and saving tools, then phpThumb thumbnail creator is your next best option for image resizing, rotating, and a bunch of other cool image manipulation functions.

Here is a script I was using on one of my sites to resize and save user uploaded images. It utilizes the phpThumb resizing class and either GD or ImageMagick.

Download the image upload and resize demo.
Download the phpThumb project files.

The image submission form:

Notice that there is no submit button. We are going to handle the submit using jQuery because we can and it’s cooler that way.

The multipart/form-data enctype value is important also. Don’t remove it or the photo will not submit. The “uploading” div will not be shown until an image is selected for upload by the user. Stick whatever you want to be show when the image is being uploaded into that div. I have an “uploading” message along with the usual accompanying spinner gif and css to make it pretty.

Here is the jQuery for our page header (make sure you have the jQuery library loaded!):

The first line hides the #uploading div on page load. The next line is the start of our function that is started whenever the users browses his computer and selects an image to be uploaded. Once he has made his image selection, the browse button is hidden, the #uploading div is shown, and the form is submitted.

Next is our php upload script with phpThumb image resizing class:

Everything you need to know is commented into the script above. This script is making three passes to save the images to multiple dimensions. You can add additional image passes and parameters as needed for your project.

Download this the image resize demo below and give it a try. The image and storage paths can be tricky. phpThumb treats paths as if they are relative to the phpThumb script directory. Using absolute paths works best. You can set absolute paths when using the script on your server so it’s cleaner. This image resize demo download below should work on most any server as-as. Note that the demo does not include the phpThumb files. There is a “phpthumb” folder in the demo package. Download the project files and drop them directly into this folder and you will be good to go.

If you run into trouble with phpThumb not working, use the following to figure out what’s going on:

I would also advise downloading the full phpThumb package and using the included demo check file to diagnose problems.

Download the image upload and resize demo.
Download the phpThumb project files.

2 thoughts on “How to Resize and Save Images with phpThumb

  1. Hey, nice job with the script! I was wondering if it’s possible to save to server thumbnails generated from images stored on remote servers, like pictures from different news articles. I am actually working on a script based on your work to accomplish this but I think I am stuck 🙁 I would appreciate your reply. Best, P.

Leave a Reply

Your email address will not be published. Required fields are marked *