I have javascript code that was running fine on the server for about 2 weeks. After an update of Chrome it stops working :(
function doUpload() {
var file = document.getElementById('fileToUpload').files[0];
EXIF.getData(file, function () {
var orientation = 0;
orientation = EXIF.getTag(this, "Orientation");
var dataUrl = "";
var img = document.createElement("img"); // Create an image
var reader = new FileReader(); // Create a file reader
reader.onload = function(e) { // Set the image once loaded into file reader
img.src = e.target.result;
var canvas = document.createElement("canvas");
var MAX_WIDTH = 1080; // Set Width and Height
var MAX_HEIGHT = 1080;
var width = img.width;
var height = img.height;
console.log(img.width);
if (width > height) {
if (width > MAX_WIDTH) {
height *= MAX_WIDTH / width;
width = MAX_WIDTH;
}
....
The problem is that img.width
and img.height
are both 0
. Because of that it is impossible to resize the image. If I add console.log(img)
I get <img src="data:image/jpeg:base 64, 9/j/ 4xg....>
so I guess the image is properly loaded into the file reader but than something prevents the code from working. I´m struggling with this from last week with no luck. Does anyone see the problem?
Thanks
edit
here is the working code:
function doUpload() {
var file = document.getElementById('fileToUpload').files[0];
EXIF.getData(file, function () {
var orientation = 0;
orientation = EXIF.getTag(this, "Orientation");
var dataUrl = "";
var img = document.createElement("img"); // Create an image
var reader = new FileReader(); // Create a file reader
reader.onload = function(e) { // Set the image once loaded into file reader
var canvas = document.createElement("canvas");
img.src = e.target.result;
img.onload = function(){
var MAX_WIDTH = 1080; // Set Width and Height
var MAX_HEIGHT = 1080;
var width = img.width;
var height = img.height;
console.log(img);