19

On a page I have displayed 100 images, which have the width and height attribute changed. Image id is in a loop.

How do I get the original image size, inside this loop?

$(this).appendTo(".prod_image").attr('height',150).attr('width',150).attr('title','').attr('name','').attr('alt','').attr('id','id'+i);
DRosenfeld
  • 115
  • 1
  • 9
X10nD
  • 19,972
  • 44
  • 105
  • 150
  • 7
    just as a small note, you can use this : attr({ height:150, width:150, title: '', name: '', 'id': 'id'+i); and so on. – Ionuț Staicu Mar 07 '10 at 11:56
  • Possible duplicate of [Is there any way to read out the "naturalWidth" of an image with jquery?](http://stackoverflow.com/questions/1832907/is-there-any-way-to-read-out-the-naturalwidth-of-an-image-with-jquery) – Darvanen Mar 17 '16 at 05:27

5 Answers5

34

You can retrieve the original dimensions of an image by using the naturalWidth and naturalHeight properties of the dom object.

Eg.

var image = document.getElementById('someImage');

var originalWidth = image.naturalWidth; 
var originalHeight = image.naturalHeight;

With jQuery it would look like:

var image = $('#someImage');

var originalWidth = image[0].naturalWidth; 
var originalHeight = image[0].naturalHeight;
g.salakirov
  • 451
  • 4
  • 3
  • 7
    +1 with a strong side note that this isn't supported in IE 8 and lower (so additional fallbacks would be required for older browsers). – Andy E Feb 23 '12 at 11:46
  • 1
    This is the syntax that seems to work: image.prop("naturalWidth") (as seen in this [related question](http://stackoverflow.com/questions/11513313/getting-an-images-original-width-and-height-in-jquery). – DRosenfeld Jan 09 '17 at 15:58
21

HTML:

  <img id="myimg" src="http://azart-cash.ru/big_pics/kazino_AzartPlay.png"
  width="120" height="140"/>

JavaScript:

var img = new Image();
img.onload = function() {
  alert(this.width + 'x' + this.height);
}
img.src = $("#myimg").attr('src');

Or just check it out here http://jsfiddle.net/Increazon/A5QJM/1/

Yi Jiang
  • 46,385
  • 16
  • 133
  • 131
Vova Popov
  • 1,027
  • 11
  • 11
  • 1
    This is by far the simplest and most elegant solution. The options with higher votes that require rendering elements offscreen and custom CSS are slower and uglier. – EMI Jan 03 '12 at 02:39
  • 1
    I think this is ugly, not elegant. naturalWidth is a good solution if you target IE8+ – inanc Apr 02 '14 at 13:50
6

The only way to get the original size is to restore it to its original state. You can do this pretty easily by cloning the image, then removing the height and width attributes to get the real values. You also have to then insert the changed image into the page or else it wont have a calculated height/width. You can do this easily in an offscreen div. Here's an example:

http://jsbin.com/ocoto/edit

The JS:

// Copy the image, and insert it in an offscreen DIV
aimgcopy = $('#myimg').clone();
$('#store').append(aimgcopy);

// Remove the height and width attributes
aimgcopy.removeAttr('height');
aimgcopy.removeAttr('width');

// Now the image copy has its "original" height and width
alert('Height: '+aimgcopy.height()+' Width: '+aimgcopy.width());

The CSS:

  #store {  position: absolute;  left: -5000px; }

The HTML:

  <img id="myimg" src="http://sstatic.net/so/img/logo.png" width="300" height="120"/>
  <div id="store"></div>
Erik
  • 19,600
  • 7
  • 43
  • 75
1

I had to perform a similar task with a jQuery plugin I created. It keeps state for an inputs text value, but the principle could easily be used for your width and height.

I would create a custom object that stores the original object's ID, Width and Height. That object is then put into an array. When required to restore the value simply loop the array of custom objects to match ID and bingo, an object with the original width and height.

You can check out my plugin's code at www.wduffy.co.uk/jLabel to see how I implemented this. Depending on how many images you are changing the array could become a little on the heavy side though.

An example might look like

var states = new Array();

function state($obj) {

    // Public Method: equals
    this.equals = function($obj) {
        return $obj.attr('id') == this.id;
    };

    // Public Properties
    this.id = $obj.attr('id');
    this.width = $obj.attr('width');
    this.height = $obj.attr('height');

};

function getState($obj) {
    var state; 

    $.each(states, function() {
        if (this.equals($obj)) {
            state = this;
            return false; // Stop the jQuery loop running
        };
    });

    return state;
};
WDuffy
  • 7,498
  • 5
  • 35
  • 42
0

Try this one:

$("<img>")
            .attr("src", element.attr("src"))
            .load(function(){
                MG.originalSize[0] = this.width;
                MG.originalSize[1] = this.height;

});

Ryan Bañaria
  • 88
  • 1
  • 7