Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't get height and width of image in javascript

Tags:

javascript

I am trying to retreive the actual height and width of an image through javascript. The image source being used in my case is a BLOB which gets generated as soon as a user inserts a file.

Heres my code:

    var img = document.createElement('img');
    var blob = URL.createObjectURL(e.target.files[0]);
    img.src = blob;
    console.log(img);
    var w = img.width;
    var h = img.height;
    console.log("NEW IMAGE width", w);
    console.log("NEW IMAGE height: ", h);

Here are the logs:

<img src=​"blob:​http%3A/​/​localhost%3A3000/​af3e5a35-8c1c-40b5-ad5a-379c3ab1ec1d">​
NEW IMAGE width 0
NEW IMAGE height:  0

the blob is correct since I can view the image in my browser.

When I create another image in my console with the same blob and try to retreive the height and width everything works just fine.

But when I try to run it like that in my onChange event form the input i just get 0 for height and width.

like image 656
noa-dev Avatar asked Jan 19 '16 09:01

noa-dev


People also ask

How do you get the image width and height using JS?

You can get the original width and height of an image using the HTML5 image naturalWidth and naturalHeight properties, which are supported in almost all major browsers. To get the current width and height, you can use the JavaScript clientWidth and clientHeight properties.

How do you find the width and height of an image?

Open the page with your feed in Chrome. Right-click the image whose size you want to know and select Inspect. View your image's width and height displayed in the Chrome DevTools. (Note, the first number is always the width).

How do you find the width and height of an image in HTML?

You can easily find the original or intrinsic width and heigh of an image using the HTML5 image naturalWidth and naturalHeight properties. These properties are supported in all major web browsers such as Chrome, Firefox, Safari, Opera, Internet Explorer 9 and above.

How do I change the size of an image in JavaScript?

Answer: Use the JavaScript width and height property You can use either width or height JavaScript property to proportionally increase and decrease the dimension of an image like zoom-in and zoom-out feature.


2 Answers

you should get size after image loaded:

    img.onload = getSize;//measure after loading or you will get 0

example and result

like image 113
jilykate Avatar answered Oct 22 '22 12:10

jilykate


How to get height and width of the image in blob objeact? example:

$(function(){
	$("input[name=file_name]").on('change',function(){
		var url = window.URL || window.webkitURL || window.mozURL;
		var src = url.createObjectURL($(this)[0].files[0]);
		$("#box").html('<img src="'+src+'">');
		$("#box img").attr('width','100');
		$('img')[0].onload = function() {
		    console.log($(this).height()); //get image height by jQuery
		    console.log($(this)[0].height)// get image height by javascript
		    console.log($(this)[0].naturalHeight);//get real height by javascript
		    /**
		     * if you want to get the image's width,you can use following code :
		     * $(this).width();
		     * $(this)[0].width;
		     * $(this)[0].naturalWidth;
		     */
		};
	})
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<form method="post" enctype="multipart/form-data">
  <input type="file" name="file_name">
</form>

<div id="box"></div>
like image 43
GlaryJoker Avatar answered Oct 22 '22 13:10

GlaryJoker