My website uses many images. On a weak day users will upload hundreds of new images.
I'm trying to figure out what is the best-practice for manipulating sizes of images.
This project uses Node.js with gm module for manipulating images, but I don't think this question is node or gm specific.
I came up with several strategies, but I can't make a decision as to which is the best, and I am not sure if I am missing an obvious best-practice strategy.
Please enlighten me with your thoughts and experience.
Option 1: Resize the file with gm on every client request.
Option 1 pros:
Option 1 cons:
- If I run gm function every time I serve a file, I can control the size, quality, compression, filters and so on whenever I need it.
- On the server I only save 1, full quality - full size version of the file and save storage space.
- gm is very resource intensive, and that means that I will be abusing my RAM for every single image server to every single client.
- It means I will be always working from a big file, which makes things even worse.
- I will always have to fetch the file from my storage (in my case S3) to the server, then manipulate it, then serve it. It seems like it would create redundant bandwidth issues.
resize the file on first upload and keep multiple sizes of the file on the server.
Option 2 pros:
Option 2 cons:
- I will only have to use gm on uploads.
- Serving the files will require almost no resources.
- I will use more storage because I will be saving multiple versions of the same file (i.e full, large, medium, small, x-small) instead of only one version.
- I will be limited to using only the sizes that were created when the user uploaded their image.
- Not flexible - If in the future I decide I need an additional size version (x-x-small for instance) I will have to run a script that processes every image in my storage to create the new version of the image.
Use option 2 to only process files on upload, but retain a resize module when serving file sizes that don't have a stored version in my storage.
Option 3 pros:
Option 3 cons:
- I will be able to reduce resource usage significantly when serving files in a selection of set sizes.
- I would still take more storage as in option 2 vs option 1.
- I will still have to process files when I serve them in cases where I don't have the file size I want
I do not create multiple versions of files on upload. I do resize the images when I serve them, BUT
when ever an image size was requested, this version of the file will be saved in my storage and for future requests I will not have to process the image again.
Option 4 pros:
Option 4 cons:
- I will only use storage for the versions I use.
- I could add a new file size when ever I need, it will be automatically created on a need-basis if it doesn't already exists.
- Will use a lot of resources only once per file
- Files that are only accessed once will be both resource intensive AND storage intensive. Because I will access the file, see that the size version I need doesn't exist, create the new file version, use the resources needed, and save it to my storage wasting storage space for a file that will only be used once (note, I can't know how many times files will be used)
- I will have to check if the file already exists for every request.
- Which would you choose? Why?
- Is there a better way than the ways I suggested?