30
Building Photo Uploader with HTML5 by Hieupv2 @Lifetime Technologies Co.,ltd Duy Tan Geeks #2 Jan 15th 2014

Modern photo uploader

Embed Size (px)

Citation preview

Page 1: Modern photo uploader

Building Photo Uploaderwith HTML5

by Hieupv2@Lifetime Technologies Co.,ltd

Duy Tan Geeks #2 Jan 15th 2014

Page 2: Modern photo uploader

@mrhieu @mrhieu

[email protected]

PHP developerFront-end developerRuby On Rails beginner

Contact

About Me

Page 3: Modern photo uploader

About Lifetime Technologies Co.,ltd100% foreign-ownedIT industry61 membersWhat we do:

Web applicationMobile application

Contact9F, Viet A building, Cau Giay district, Hanoi, Vietnamwww.lifetimetech.vn

Page 4: Modern photo uploader

START

Page 5: Modern photo uploader

Ancient photo uploader

Page 6: Modern photo uploader

and “MODERN” one

Page 7: Modern photo uploader

Challenges

● Drag ‘n’ drop

● Limit of 10 files, 10MB each

● Multiple upload

● Instant photo thumbnail, file info

● Extract EXIF data at CLIENT-side

● Extract GPS data and display (on a map)

Page 8: Modern photo uploader

Challenges (cont)

● Form data for each photo

● Add, remove photos to upload

● Display upload progress

Page 9: Modern photo uploader

Drag and drop

- Javascript API- Event-based- Listening for Dragging Events: dragstart, dragenter, drop, dragenddomElement.addEventListener('dragdrop', handleDropStart, false);function handleDrop(e) { var files = e.dataTransfer.files;

Reference:www.html5rocks.com/en/tutorials/dnd/basics/Demo: http://html5demos.com/dnd-upload

Page 10: Modern photo uploader

Limit of 10 files, 10MB each

- Count- File reader APIvar files = e.dataTransfer.files; // FileList object. // files is a FileList of File objects. List some properties. for (var i = 0, f; f = files[i]; i++) { console.log(f.name, f.type, f.size, f.lastModifiedDate)}

Reference:www.html5rocks.com/en/tutorials/file/dndfiles/

Page 11: Modern photo uploader

Multiple Upload

Implementation of XHR2 object- Append form data “on the fly”- Cross-origin requests sharing (CORS)- Uploading progress events- Chunk uploading/downloading binary data

Reference:www.html5rocks.com/en/tutorials/file/xhr2/#toc-send-formdata

Page 12: Modern photo uploader

Instant thumbnail, file info

- File reader API (again)- Asynchronousvar reader = new FileReader();// Closure to capture the file information.reader.onload = (function(theFile) { return function(e) { // Render thumbnail. console.log(e.target.result);

}})(file);reader.readAsDataURL(f);

Page 13: Modern photo uploader

Instant thumbnail, file info (cont)

imageData:data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAMCAgMCAgMDAwMEAwMEBQgFBQQEBQoHBwYIDAoMDAsKCwsNDhIQDQ4RDgsLEBYQERMUFRUVDA8XGBYUGBIUFRT/2wBDAQMEBAUEBQkFBQkUDQsNFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBT/wAARCAL4BHQDASIAAhEBAxEB/8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL/8QAtRAAAgEDAwIEAwUFBAQAAAF9AQIDAAQRBRIhMUEGE1FhByJxFDKBkaEII0KxwRVS0fAkM2JyggkKFhcYGRol………………………..

Page 14: Modern photo uploader

Extract EXIF data

- File reader API (agaiiin)- At the first 128kb of the image- Standardized data structure

@.@

Reference: http://code.flickr.net/2012/06/01/parsing-exif-client-side-using-javascript-2/

Page 15: Modern photo uploader

Extract EXIF data (cont)

Page 16: Modern photo uploader

Extract GPS data and display (on a map)

- Extract from Exif Data- Properties: .GPSLatitude

.GPSLongitude- Googlemaps Javascript API v3

Page 17: Modern photo uploader

Add, remove specifics photo

- Tricky- Add- Remove

Page 18: Modern photo uploader

Upload progress

- XHR2xhr.upload.onprogress = function(e) { if (e.lengthComputable) { progressBar.value = (e.loaded / e.total) * 100; progressBar.textContent = progressBar.value; // Fallback for unsupported browsers. } }

Reference:www.html5rocks.com/en/tutorials/file/xhr2/#toc-send-blob

Page 19: Modern photo uploader

Put them all together

JqueryJquery File Upload github.com/blueimp/jQuery-File-Upload

Canvasresize, Exif github.com/gokercebeci/canvasResize

Javascript Template Enginegithub.com/blueimp/JavaScript-Templates

BootstrapDatepicker, TimepickerOthers.

Page 21: Modern photo uploader

Wait a minute...

Page 22: Modern photo uploader

One more thing

Display INCOMPLETED

uploaded photos

Page 23: Modern photo uploader

Upload Processing...

Page 24: Modern photo uploader

One more thing

● It takes (long) time for server’s manipulation- Re-sizing- Indexing- Generating location (place) info...● No extra thumbnail photo stored on server

side● Automatically fetch “real” photo once

processes have been finished.

Page 25: Modern photo uploader

HTML5 Web storage

- 5MB- Key-value- Session Storage / Local Storageif (window.sessionStorage) { //.. sessionStorage.setItem('photo_' + data.files[0].name.substring(0, 20), imageData); }sessionStorage.getItem(<key>);

Reference: http://www.html5rocks.com/en/tutorials/file/xhr2/#toc-send-blob

Page 26: Modern photo uploader

Clever Collaboration

Server- API: whether photos are all finished- (Push technology)

Client- Continuous request (per 5s) / Push technology- Retrieve processed photos- Remove web storage records

Page 27: Modern photo uploader
Page 28: Modern photo uploader

So what ?

It just works !

mrhieu.github.io/500pxupload

Page 29: Modern photo uploader

want more

?

Page 30: Modern photo uploader

ありがと

See this online: http://goo.gl/nQ0Fid