summary refs log tree commit diff
path: root/webclient/components/utilities/utilities-service.js
diff options
context:
space:
mode:
authorEmmanuel ROHEE <erohee@amdocs.com>2014-08-22 10:15:15 +0200
committerEmmanuel ROHEE <erohee@amdocs.com>2014-08-22 10:15:15 +0200
commitc8d0c4762da432aafa4372928aa70ef55646134b (patch)
treebf61f6e2881dd9085c114acdd4528ddf1f2eb2d3 /webclient/components/utilities/utilities-service.js
parenthomeserver runs webclient by default now (diff)
downloadsynapse-c8d0c4762da432aafa4372928aa70ef55646134b.tar.xz
Safari needs the img.onload event before actually working on the img
Diffstat (limited to '')
-rw-r--r--webclient/components/utilities/utilities-service.js65
1 files changed, 38 insertions, 27 deletions
diff --git a/webclient/components/utilities/utilities-service.js b/webclient/components/utilities/utilities-service.js
index 9cf858ef39..5e9f707221 100644
--- a/webclient/components/utilities/utilities-service.js
+++ b/webclient/components/utilities/utilities-service.js
@@ -38,10 +38,15 @@ angular.module('mUtilities', [])
             img.src = e.target.result;
             
             // Once ready, returns its size
-            deferred.resolve({
-                width: img.width,
-                height: img.height
-            });
+            img.onload = function() {
+                deferred.resolve({
+                    width: img.width,
+                    height: img.height
+                });
+            };
+            img.onerror = function(e) {
+                deferred.reject(e);
+            };
         };
         reader.onerror = function(e) {
             deferred.reject(e);
@@ -71,33 +76,39 @@ angular.module('mUtilities', [])
         reader.onload = function(e) {
 
             img.src = e.target.result;
+            
+            // Once ready, returns its size
+            img.onload = function() {
+                var ctx = canvas.getContext("2d");
+                ctx.drawImage(img, 0, 0);
 
-            var ctx = canvas.getContext("2d");
-            ctx.drawImage(img, 0, 0);
-
-            var MAX_WIDTH = maxSize;
-            var MAX_HEIGHT = maxSize;
-            var width = img.width;
-            var height = img.height;
+                var MAX_WIDTH = maxSize;
+                var MAX_HEIGHT = maxSize;
+                var width = img.width;
+                var height = img.height;
 
-            if (width > height) {
-                if (width > MAX_WIDTH) {
-                    height *= MAX_WIDTH / width;
-                    width = MAX_WIDTH;
-                }
-            } else {
-                if (height > MAX_HEIGHT) {
-                    width *= MAX_HEIGHT / height;
-                    height = MAX_HEIGHT;
+                if (width > height) {
+                    if (width > MAX_WIDTH) {
+                        height *= MAX_WIDTH / width;
+                        width = MAX_WIDTH;
+                    }
+                } else {
+                    if (height > MAX_HEIGHT) {
+                        width *= MAX_HEIGHT / height;
+                        height = MAX_HEIGHT;
+                    }
                 }
-            }
-            canvas.width = width;
-            canvas.height = height;
-            var ctx = canvas.getContext("2d");
-            ctx.drawImage(img, 0, 0, width, height);
+                canvas.width = width;
+                canvas.height = height;
+                var ctx = canvas.getContext("2d");
+                ctx.drawImage(img, 0, 0, width, height);
 
-            var dataUrl = canvas.toDataURL("image/jpeg", 0.7); 
-            deferred.resolve(self.dataURItoBlob(dataUrl));
+                var dataUrl = canvas.toDataURL("image/jpeg", 0.7); 
+                deferred.resolve(self.dataURItoBlob(dataUrl));
+            };
+            img.onerror = function(e) {
+                deferred.reject(e);
+            };
         };
         reader.onerror = function(e) {
             deferred.reject(e);