Skip to content

Instantly share code, notes, and snippets.

@johan
Created March 26, 2012 21:40
Show Gist options
  • Select an option

  • Save johan/2209957 to your computer and use it in GitHub Desktop.

Select an option

Save johan/2209957 to your computer and use it in GitHub Desktop.

Revisions

  1. johan created this gist Mar 26, 2012.
    19 changes: 19 additions & 0 deletions jquery.naturalWidth.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,19 @@
    // jQuery.naturalWidth / jQuery.naturalHeight plugin for (already-loaded) images

    // Triple-licensed: Public Domain, MIT and WTFPL license - share and enjoy!

    (function($) {
    function img(url) {
    var i = new Image;
    i.src = url;
    return i;
    }

    if ('naturalWidth' in (new Image)) {
    $.fn.naturalWidth = function() { return this[0].naturalWidth; };
    $.fn.naturalHeight = function() { return this[0].naturalHeight; };
    return;
    }
    $.fn.naturalWidth = function() { return img(this.src).width; };
    $.fn.naturalHeight = function() { return img(this.src).height; };
    })(jQuery);