javascript 获取图片原始尺寸
function getImgInfo(url){
var img = new Image(), loaded = false;
var info = {};
//对于预加载的情况,即没有把图片插入DOM树
img.onload = function(){
img.onload = null;
loaded = true; //标志当前已加载过,防止complete再运行
info['width'] = img.width;
info['height'] = img.height;
}
img.src = url;
var orignInfo;
//已经加载过的图片
if(img.complete && !loaded){
//safari,firefox,chrome,与opera10+中,有两个便捷的属性
if(img.naturalWidth && img.naturalHeight){
info['width'] = img.naturalWidth;
info['height'] = img.naturalHeight;
}else if(img.runtimeStyle){
//IE runtimeStyle不用同步style就能重绘原来的元素
orignInfo = {'width':img.width,'height':img.height}; //保存原来的尺寸
img.width = img.height = "auto";//重写
info['width'] = img.width;//获取原始尺寸
info['height'] = img.height;
img.width = orignInfo.width; //还原尺寸
img.height = orignInfo.height;
}else{
info['width'] = img.width;
info['height'] = img.height;
}
}
return info;
}
参考:http://www.cnblogs.com/rubylouvre/archive/2011/05/05/2037115.html