Skip to content
Snippets Groups Projects
Commit c8d0c476 authored by Emmanuel ROHEE's avatar Emmanuel ROHEE
Browse files

Safari needs the img.onload event before actually working on the img

parent be2f948d
No related branches found
No related tags found
No related merge requests found
...@@ -82,6 +82,7 @@ angular.module('mFileUpload', ['matrixService', 'mUtilities']) ...@@ -82,6 +82,7 @@ angular.module('mFileUpload', ['matrixService', 'mUtilities'])
// First, get the image size // First, get the image size
mUtilities.getImageSize(imageFile).then( mUtilities.getImageSize(imageFile).then(
function(size) { function(size) {
console.log("image size: " + JSON.stringify(size));
// The final operation: send imageFile // The final operation: send imageFile
var uploadImage = function() { var uploadImage = function() {
......
...@@ -38,10 +38,15 @@ angular.module('mUtilities', []) ...@@ -38,10 +38,15 @@ angular.module('mUtilities', [])
img.src = e.target.result; img.src = e.target.result;
// Once ready, returns its size // Once ready, returns its size
deferred.resolve({ img.onload = function() {
width: img.width, deferred.resolve({
height: img.height width: img.width,
}); height: img.height
});
};
img.onerror = function(e) {
deferred.reject(e);
};
}; };
reader.onerror = function(e) { reader.onerror = function(e) {
deferred.reject(e); deferred.reject(e);
...@@ -71,33 +76,39 @@ angular.module('mUtilities', []) ...@@ -71,33 +76,39 @@ angular.module('mUtilities', [])
reader.onload = function(e) { reader.onload = function(e) {
img.src = e.target.result; 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"); var MAX_WIDTH = maxSize;
ctx.drawImage(img, 0, 0); var MAX_HEIGHT = maxSize;
var width = img.width;
var MAX_WIDTH = maxSize; var height = img.height;
var MAX_HEIGHT = maxSize;
var width = img.width;
var height = img.height;
if (width > height) { if (width > height) {
if (width > MAX_WIDTH) { if (width > MAX_WIDTH) {
height *= MAX_WIDTH / width; height *= MAX_WIDTH / width;
width = MAX_WIDTH; width = MAX_WIDTH;
} }
} else { } else {
if (height > MAX_HEIGHT) { if (height > MAX_HEIGHT) {
width *= MAX_HEIGHT / height; width *= MAX_HEIGHT / height;
height = MAX_HEIGHT; height = MAX_HEIGHT;
}
} }
} canvas.width = width;
canvas.width = width; canvas.height = height;
canvas.height = height; var ctx = canvas.getContext("2d");
var ctx = canvas.getContext("2d"); ctx.drawImage(img, 0, 0, width, height);
ctx.drawImage(img, 0, 0, width, height);
var dataUrl = canvas.toDataURL("image/jpeg", 0.7); var dataUrl = canvas.toDataURL("image/jpeg", 0.7);
deferred.resolve(self.dataURItoBlob(dataUrl)); deferred.resolve(self.dataURItoBlob(dataUrl));
};
img.onerror = function(e) {
deferred.reject(e);
};
}; };
reader.onerror = function(e) { reader.onerror = function(e) {
deferred.reject(e); deferred.reject(e);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment