Skip to content

Instantly share code, notes, and snippets.

@Znarkus
Last active December 20, 2015 19:39
Show Gist options
  • Save Znarkus/6185091 to your computer and use it in GitHub Desktop.
Save Znarkus/6185091 to your computer and use it in GitHub Desktop.
Get natural img size.
imgNaturalSize($img, function (width, height) {
// $img is now loaded
});
function imgNaturalSize($img, callback) {
if ($img[0].complete || $img[0].width && $img[0].height) {
returnSize();
} else {
$img.load(function () {
returnSize();
});
}
function returnSize() {
var img;
if ('naturalWidth' in $img[0]) {
callback($img[0].naturalWidth, $img[0].naturalHeight);
} else {
img = new Image();
img.src = $img[0].src;
callback(img.width, img.height);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment