Skip to content

Instantly share code, notes, and snippets.

@colelawrence
Last active August 29, 2015 14:02
Show Gist options
  • Save colelawrence/f1006509e704ceb61e63 to your computer and use it in GitHub Desktop.
Save colelawrence/f1006509e704ceb61e63 to your computer and use it in GitHub Desktop.
Image aspect ratio from url
@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)
(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