3
votes

Context: I am building a site where a user uploads an image as part of the process of listing an item. I want to show them a preview of the image in the upload form, as it is both aesthetically pleasing, and will help the users in uploading and describing their items.

Problem: In order to show a preview of the image, I need to upload it to my server, due to the sandboxing of browsers (I cannot get the image data into JS except by submitting the file-input form). However, we store the images using identifiers based upon data that the user inputs, in order to keep them organized (in our case, we sanitize the name and use that as a file name, in a /useruploads/user_id folder).

While I can change the naming scheme of the files in order to remove the dependency of user input (i.e. sha1 the file), uploading to s3 and resizing the image is an expensive operation, and I do not want to tie up resources unnecessarily. If I upload the image without resizing and generating thumbnails, then after the form containing the information about the item is submitted, the application will have to stream the data from S3, resize and generate thumbnails, and then re-upload the image(s), which seems somewhat roundabout.

I feel this is a pretty common problem, and I wonder if anyone has any patterns or advice on how to handle image uploads, in this regard.

2
I know that with wordpress for example, you upload the image, then it goes into a "media library" of items for that particular blog post. You can then change the description etc and insert it into the post you are writing. Check out the source code and it might give you some ideas.Daniel Kinsman

2 Answers

1
votes

Using the FileReader API, you can actually read the binary blob from disk and then use that image purely client-side, before submitting the form!

See http://www.onlywebpro.com/2012/01/24/create-thumbnail-preview-of-images-using-html5-api/

0
votes

Most services would have a dedicated location to store the thumbnails automatically as you do the upload. In my service too I store the original image on s3 and a corresponding virtual path mapped to its thumbnail which I store in the user metadata of the thumbnail image.

Thus from the UI perspective we'd display just the thumbnails initially which would cost less to download comparatively. Once the user clicks on the image the original image location is received from the metadata of this image. With this we get the original image only when the user requests for it.

Also the resizing of the image can be done on the download when the original image is requested and to be rendered within a container.