I have a base64 img encoded that you can find here. How can I get the height and the width of it?
-
1Link rot has set in for this question – Luke Jan 10 '22 at 12:35
5 Answers
var i = new Image();
i.onload = function(){
alert( i.width+", "+i.height );
};
i.src = imageData;
- 7,751
- 3
- 38
- 38
-
1just awesome , it takes half time to check size , thank you so much! – bombastic Jul 21 '13 at 20:11
-
14
-
2
-
1
-
14I would also add that the order here is very important. If you do this the other way around (src before onload) you may miss the event. See: http://stackoverflow.com/a/2342181/4826740 – maxshuty Apr 04 '17 at 17:58
-
@CoenDamen why should the whole script block just to get information on the image size? – João Pimentel Ferreira Nov 25 '18 at 21:17
-
1
-
string or url, the image has to be loaded for client to get the dimension. do you have something else in mind? – gp. Nov 21 '19 at 12:41
-
`imageData` should be in the format `data:image/png;base64,` then the base 64 image data. – Luke Jan 10 '22 at 13:13
For synchronous use just wrap it into a promise like this:
function getImageDimensions(file) {
return new Promise (function (resolved, rejected) {
var i = new Image()
i.onload = function(){
resolved({w: i.width, h: i.height})
};
i.src = file
})
}
then you can use await to get the data in synchronous coding style:
var dimensions = await getImageDimensions(file)
- 2,723
- 1
- 30
- 32
-
5
-
1'naturalWidth' and 'naturalHeight` are the better choices no? https://stackoverflow.com/questions/28167137/whats-the-difference-between-width-naturalwidth-and-clientwidth – Crashalot Dec 27 '19 at 23:28
I found that using .naturalWidth and .naturalHeight had the best results.
const img = new Image();
img.src = 'https://via.placeholder.com/350x150';
img.onload = function() {
const imgWidth = img.naturalWidth;
const imgHeight = img.naturalHeight;
console.log('imgWidth: ', imgWidth);
console.log('imgHeight: ', imgHeight);
};
Docs:
- https://developer.mozilla.org/en-US/docs/Web/API/HTMLImageElement/naturalWidth
- https://developer.mozilla.org/en-US/docs/Web/API/HTMLImageElement/naturalHeight
This is only supported in modern browsers. http://www.jacklmoore.com/notes/naturalwidth-and-naturalheight-in-ie/
- 33,459
- 6
- 59
- 67
- 723
- 5
- 21
-
Exactly, it is the original size of image before being resized by ```object-fit``` – Cao Mạnh Quang Feb 15 '21 at 04:09
Create a hidden <img> with that image and then use jquery .width() and . height()
$("body").append("<img id='hiddenImage' src='"+imageData+"' />");
var width = $('#hiddenImage').width();
var height = $('#hiddenImage').height();
$('#hiddenImage').remove();
alert("width:"+width+" height:"+height);
Test here: FIDDLE
Image is not initially created hidden. it gets created, then you get width and height and then remove it. This may cause a very short visibility in large images in this case you have to wrap the image in another container and make that container hidden not the image itself.
Another Fiddle that does not add to dom as per gp.'s anser : HERE
- 346
- 1
- 9
-
1var i = new Image(); i.src = imageData; setTimeout(function(){ alert ( "width:"+ i.width+" height:" + i.height ); },100); – gp. Jul 21 '13 at 17:59
-
this is only a fix needed because of the nature of the jsfiddle, you can change onLoad to onDomReady instead to fix that initial 0 width and height problem. Code assumes you call this function somewhere in your work where document is already loaded. – Desu Jul 21 '13 at 18:04
-
1Guess your point being was, that you do not need to add anything to dom to get width and height. Will modify answer to reflect your suggestions. – Desu Jul 21 '13 at 18:14
A more modern solution is to use HTMLImageElement.decode() instead of the onload event. decode() returns a promise, thus can be used synchronously with await.
Asynchronous use:
let img = new Image();
img.src = "myImage.png";
img.decode().then(() => {
let width = img.width;
let height = img.height;
// Do something with dimensions
});
Synchronous use (inside async function):
let img = new Image();
img.src = "myImage.png";
await img.decode();
let width = img.width;
let height = img.height;
// Do something with dimensions