Last active
December 20, 2015 19:39
-
-
Save Znarkus/6185091 to your computer and use it in GitHub Desktop.
Get natural img size.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
imgNaturalSize($img, function (width, height) { | |
// $img is now loaded | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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