Add ImageResizer#loadFile
authorTim Düsterhus <duesterhus@woltlab.com>
Fri, 29 Mar 2019 14:38:20 +0000 (15:38 +0100)
committerTim Düsterhus <duesterhus@woltlab.com>
Fri, 29 Mar 2019 14:38:20 +0000 (15:38 +0100)
wcfsetup/install/files/js/WoltLabSuite/Core/ImageResizer.js

index 6a914e6d0bc5e140525d101ad85889d0345c2643..d572ea29ed8872f44f7a22578861daf28e0da080 100644 (file)
@@ -105,6 +105,47 @@ define([
                                });
                },
                
+               /**
+                * Loads the given file into an image object and parses Exif information.
+                * 
+                * @param   {File}    file the file to load
+                * @returns {Promise} resulting image data
+                */
+               loadFile: function (file) {
+                       var exif = undefined;
+                       if (file.type === 'image/jpeg') {
+                               // Extract EXIF data
+                               exif = ExifUtil.getExifBytesFromJpeg(file);
+                       }
+                       
+                       var loader = new Promise(function (resolve, reject) {
+                               var reader = new FileReader();
+                               var image = new Image();
+                               
+                               reader.addEventListener('load', function () {
+                                       image.src = reader.result;
+                               });
+                               
+                               reader.addEventListener('error', function () {
+                                       reader.abort();
+                                       reject(reader.error);
+                               });
+                               
+                               image.addEventListener('error', reject);
+                               
+                               image.addEventListener('load', function () {
+                                       resolve(image);
+                               });
+                               
+                               reader.readAsDataURL(file);
+                       });
+                       
+                       return Promise.all([ exif, loader ])
+                               .then(function (result) {
+                                       return { exif: result[0], image: result[1] };
+                               });
+               },
+               
                /**
                 * Downscales an image given as File object.
                 *