My website allows users to upload profile images, which are then represented in several (about 3 or 4) different sizes around the site.
As the site grows, there is always the possibility that the image sizes will need to be tweaked, or new image sizes will be needed later.
How does a site like Facebook or Twitter handle this? Do they process the image into different sizes right at the upload time, or do they store higher quality images and process them server-side when needed?
Is there a common way to handle this?
The different sized images would most likely be cached somewhere, rather than processed at access-time each time. When the upload occurs, you would create all the sizes you will need and store them (in files or your database). This method uses the most disk space to store all image sizes, but places all the processing load at the moment of upload, allowing for faster access later.
Alternatively, if the load isn’t expected to be heavy you could create different sizes at the first moment those sizes are accessed, and then store them for future use. This method therefore uses less disk space by only creating images that are actually used, but would inhibit access times the first time an image size is used. Future access times would be fast, accessing a cached image.
Addendum for larger loads
Consider performing the image processing on a separate worker server. Ideally, the front end and the worker servers would share the storage mount to which images are uploaded and stored, saving transfer bandwidth between them. At the moment of upload of the original, the main application places the image in a queue for processing by the worker. The images cannot be available for use until processed, but the processing load remains independent of the front end, so it does not have much direct impact on the end-user experience.
Depending on just how many uploads you expect per minute, the worker process could be as simple as a cron job running every minute to poll a table of pending upload tasks (registered by the main application), perform the conversions, and update the table when they have been completed. If one minute is too long to wait however, you would need a continuous running worker process to be polling for new tasks. Obviously this is more complex to implement.
No matter what you do though, do not regenerate the alternative image sizes each time you need them. Store them somewhere.