Created
July 29, 2013 18:50
-
-
Save 1000k/6106673 to your computer and use it in GitHub Desktop.
Get actual dimension of image. Using jQuery.
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
/** | |
* 画像の実際のサイズを取得する | |
* | |
* @param string image img要素 | |
*/ | |
function getActualDimension(image) { | |
var run, mem, w, h, key = "actual"; | |
// for Firefox, Safari, Google Chrome | |
if ("naturalWidth" in image) { | |
return {width: image.naturalWidth, height: image.naturalHeight}; | |
} | |
if ("src" in image) { // HTMLImageElement | |
if (image[key] && image[key].src === image.src) {return image[key];} | |
if (document.uniqueID) { // for IE | |
w = $(image).css("width"); | |
h = $(image).css("height"); | |
} else { // for Opera and Other | |
mem = {w: image.width, h: image.height}; // keep current style | |
$(this).removeAttr("width").removeAttr("height").css({width:"", height:""}); // Remove attributes in case img-element has set width and height (for webkit browsers) | |
w = image.width; | |
h = image.height; | |
image.width = mem.w; // restore | |
image.height = mem.h; | |
} | |
return image[key] = {width: w, height: h, src: image.src}; // bond | |
} | |
// HTMLCanvasElement | |
return {width: image.width, height: image.height}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment