So this is my code snippet (unnecessary code lines are removed):
let imgInput = document.getElementById('id_photo');
imgInput.addEventListener('change', function (e) {
if (e.target.files) {
for (let i = 0; i < e.target.files.length; i++) {
let imageFile = e.target.files[i];
var reader = new FileReader();
reader.onload = function (e) {
var img = document.getElementById(node.inputId+'-img');
img.onload = function() {
var shape = resizeImage(img) // resizing image for future canvas drawing, returns [width, height] (resized)
var canvas = document.createElement("canvas")
canvas.width = 1000
canvas.height = 1000
var ctx = canvas.getContext("2d")
ctx.drawImage(img, 0, 0, shape[0], shape[1])
}
img.src = e.target.result;
}
reader.readAsDataURL(imageFile);
}
}
});
I want to get the uploaded original image’s width and height so I can resize it. I can’t do it from imageFile as it returns undefined and can’t get it from the img variable as it originally depends on my css (in css the <img> tag is in a <div> tag which is not fullscreen, let’s say it is 400 pixels). So correspondingly, if I upload 5000×5000 image it will not be the original-sized (hope you got it). My problem is that because my css resizes that div and elements inside, I can’t get normal widht and height of my original image, so I can resize it then draw with canvas and then convert it to base64.
I try to add this code to img.onload:
var blobURL = URL.createObjectURL(imageFile)
Here I access my original image (which is imageFile), create blob for it and get that blob link. But I can’t access that blob link image’s width and height. Any suggestions?
Note: I think the problem is in img.onload itself. As in this line I pass that img:
ctx.drawImage(img, 0, 0, shape[0], shape[1])
and as a result I get this error:
Uncaught TypeError: Failed to execute 'drawImage' on 'CanvasRenderingContext2D': The provided value is not of type '(CSSImageValue or HTMLCanvasElement or HTMLImageElement or HTMLVideoElement or ImageBitmap or OffscreenCanvas or SVGImageElement or VideoFrame)'.
at HTMLImageElement.img.onload