Last active
August 29, 2015 14:02
-
-
Save colelawrence/f1006509e704ceb61e63 to your computer and use it in GitHub Desktop.
Image aspect ratio from url
This file contains hidden or 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
@img = null | |
# Using an Image Object | |
getAspectRatio = (url, callback) -> | |
@img = img = new Image() | |
img.onload = -> | |
callback null, @width/@height, @width, @height | |
img.onerror = -> | |
callback "Failed loading #{url}" | |
img.src = url | |
turl = 'http://cdn.sstatic.net/stackoverflow/img/sprites.png?v=3c6263c3453b' | |
getAspectRatio turl, (err, ratio) -> | |
Say(ratio or err) |
This file contains hidden or 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 () { | |
var getAspectRatio = function(url, callback) { | |
var img = new Image(); | |
img.onload = function() { | |
return callback(null, this.width / this.height, this.width, this.height); | |
}; | |
img.onerror = function() { | |
return callback("Failed loading " + url); | |
}; | |
return img.src = url; | |
}; | |
if (typeof module !== "undefined") | |
module.exports = getAspectRatio; | |
else | |
this.getAspectRatio = getAspectRatio; | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment