1

how can I get an image's width and height in cm by JavaScript? I wrote a simple script, but when images have different resolutions so it couldn't show correct w and h. Please help me. (sorry for my English)

Mhe Mrg
  • 11
  • 2
  • 5
  • How about adding your code example so that people can suggest improvements ? – the-noob Apr 30 '16 at 09:49
  • In short: you can’t get the real dimension because images are built in pixels and different devices have a different pixel per inch ratio. – KittMedia Apr 30 '16 at 22:45

4 Answers4

0
$('img').width();
$('img').height();

or

$('img').clientWidth;
$('img').clientHeight;
hichris123
  • 10,145
  • 15
  • 56
  • 70
jamal zareie
  • 19
  • 1
  • 5
0

Small sample on pure JavaScript with pixel to cm converter (size depends on screen's DPI):

var
  getSizeInCM,
  ready;

/* Convert px to cm */
getSizeInCM = function(sizeInPX) {
  
   return sizeInPX * 2.54 / (96 * window.devicePixelRatio)
};

ready = function() {
  
  var 
    img = document.getElementById('img'),
    width = getSizeInCM(img.clientWidth),
    height = getSizeInCM(img.clientHeight);

  alert('width: ' + width.toFixed(2) + ' cm, height: ' + height.toFixed(2) + ' cm');
};
<body onload="ready()">
  <img id="img" src="http://placehold.it/250x150">
</body>

Result (MacBook Pro late 2011, Google Chrome):

enter image description here

Result (Apple Iphone 6, Safari):

enter image description here

Ali Mamedov
  • 5,116
  • 3
  • 33
  • 47
0

If it is sufficient to assume 96 pixels per inch, the formula is to calculate cm is:

centimeters = pixels * 2.54 / 96

function getImgSize() {
  var img = document.getElementById('img');
  var width = img.clientWidth * 2.54 / 96;
  var height = img.clientHeight * 2.54 / 96;
  document.getElementById('demo').innerHTML = 'width: ' + width + ', height: ' + height;
};
<body onload="getImgSize()">
  <img id="img" src="http://placehold.it/250x150">
  <p id="demo"></p>
</body>
Munawir
  • 3,346
  • 9
  • 33
  • 51
0

Some experiments with HTML5, to see which values actually get returned.

First of all I used a program called Dash to get an overview of the image API. It states that 'height' and 'width' are the rendered height/width of the image and that 'naturalHeight' and 'naturalWidth' are the intrinsic height/width of the image (and are HTML5 only).

I used an image of a beautiful butterfly, from a file with height 300 and width 400. And this Javascript:

var img = document.getElementById("img1");

console.log(img.height,           img.width);
console.log(img.naturalHeight,    img.naturalWidth);
console.log($("#img1").height(),  $("#img1").width());

Then I used this HTML, with inline CSS for the height and width.

<img style="height:120px;width:150px;" id="img1" src="img/Butterfly.jpg" />

Results:

/*Image Element*/ height == 300         width == 400
       naturalHeight == 300  naturalWidth == 400
/*Jquery*/      height() == 120       width() == 150

/*Actual Rendered size*/    120                  150

I then changed the HTML to the following:

<img height="90" width="115" id="img1" src="img/Butterfly.jpg" />

i.e. using height and width attributes rather than inline styles

Results:

/*Image Element*/ height ==  90         width == 115
       naturalHeight == 300  naturalWidth == 400
/*Jquery*/      height() ==  90       width() == 115

/*Actual Rendered size*/     90                  115

I then changed the HTML to the following:

<img height="90" width="115" style="height:120px;width:150px;" id="img1" src="img/Butterfly.jpg" />
i.e. using both attributes and CSS, to see which takes precedence.

Results:

/*Image Element*/ height ==  90         width == 115
       naturalHeight == 300  naturalWidth == 400
/*Jquery*/      height() == 120       width() == 150

/*Actual Rendered size*/    120                  150

NOTE : I have copied this answer from Height and Width. Its really worth to know these differences.

Hope this helps you.

Community
  • 1
  • 1
Nitin Garg
  • 888
  • 6
  • 7