PHP Image Uploading-Resizing
Thursday, October 18th, 2007The other day I was on the look out for a solution to speed up the process of uploading images on the server.
We had the code in place already, and everything was running smooth (resizing, thumbnail creation), but there was just one problem when the user will upload something over 1024 x 800.
It was taking too long and the user will either get a server error, or just run out of patients, cancelling the upload or just close the browser.
Dealing with users that have no knowledge of adjusting the digital camera to take o lower quality / size picture, or just they don’t want to or…, made me think.
I needed a solution that will not break apart in the middle of doing its job, will be fast and doesn’t require too much of my time.
So looking at what I have.
1. 1 x products table
2. 1 x product images table, related to the products table by product id
3. 1 x folder that stores the images
So the solution I came up with was:
1. Duplicate product images table and add an extra field (status, 1=done, 0 = to be processed)
2. Create a new folder to store the temporary images
3. When the user uploads an image, we store the details into the duplicated table, and upload the image into the temporary folder without doing any work on them.
4. Create a new page that will look into the duplicated table, gets all the records where the status=0, then using while… statement, the following work was carried out:
a. get the record details, store them into variables
b. get the image path, check to see if the image exist in the temp folder
c. check the size of the current image (we need to resize it to 800px, then again create thumbnail of 100px and ad small_ to the image name)
d. save the images into the right folder this time
e. add the record into the product images table
f. delete the original image from the temporary folder, we don’t need it anymore
g. update the old record into the database as 1=done
h. send email of how many images has been processed.
Now everything looked good. Run few tests and it was all OK.
The problem:
1. the page had to be called manually, by clicking on a link or typing the URL into the address bar in IE or FF
2. call the page using an include statement into the home page (not really a solution)
The solution:
1. I was so tired, just didn’t came to me at all, so i dropped everything and went to get myself a strong cup of coffee, when I clicked – CRON JOB
so by setting up a cron job to run every 5min. the job becomes automated.
I get an email if anything has been processed so I could if I want to, check it out. If no images are processed, no email arrives so I don’t give myself too much work, deleting emails – what’s the point of doing all that work for?
Hope this helps others as much as it helped me.














