简体   繁体   English

获取真实的图像宽度和高度

[英]Get real image width and height

When I use image.width it returns the image's width which appears on the screen, and I use max-width property on the image and that image appears in some div . 当我使用image.width它返回出现在屏幕上的图像max-width ,并且我在图像上使用max-width属性,并且该图像出现在div Real image's size is much bigger. 实际图像的尺寸要大得多。 Is it possible to get a real image's width? 是否可以获得真实图像的宽度?

You can get the real dimensions of an image by creating a new image with the same source and get the dimensions of that 您可以通过使用相同的来源创建一个新图像来获取图像的实际尺寸,并获取该图像的尺寸

var img = new Image();

img.onload = function() {
    var w = this.width,
        h = this.height;
}

img.src = $('img')[0].src; // or whatever

FIDDLE 小提琴

First, know that images load asynchronously, meaning you'll have to write your code so you get the results in a callback function. 首先,要知道图像是异步加载的,这意味着您必须编写代码,以便在回调函数中获得结果。 Then, use a new image object with no explicit styling (so it can resize to fit native image dimensions) to trigger the callback. 然后,使用没有显式样式的新图像对象(以便可以调整大小以适合本机图像尺寸)以触发回调。 For example: 例如:

function whatWidth(url, onSize) {
  var img = new Image();
  img.src = url;
  img.onload = function() {
    onSize(img.width, img.height);
  }
}

// Now get the dimensions of an image ...
whatWidth('http://icons.iconarchive.com/icons/deleket/keriyo-emoticons/256/Smiley-rofl-icon.png',
  function(w, h) {
    console.log('width x height: ', w, h);
  }
);

// => Outputs "width x height: 256 256"

It's worth noting that, while this may appear to require a request to the server to fetch the image (independent from whatever image object you're actually showing on your web page), as long as the url is the same as the image on your page the browser cache will [typically] insure only one request is made. 值得注意的是,尽管这似乎需要向服务器发送请求以获取图像(与您在网页上实际显示的图像对象无关),但前提是url与您网页上的图像相同浏览器缓存页面通常将确保仅发出一个请求。

Dart example with caching in case you need it (I know original question mentioned JS, but the logic stays the same): 在需要时使用缓存的Dart示例(我知道原始问题提到JS,但是逻辑保持不变):

class _IconSize {
  final int width;
  final int height;

  _IconSize(this.width, this.height);
}


class IconLoader {
  static final IconLoader _this = new IconLoader._init();
  factory IconLoader() => _this;

  // Url -> dimensions.
  final Map<String, _IconSize> _cache = {};

  IconLoader._init() {}

  void withSize(String url, void onLoaded(int width, int height)) {
    if (_cache.containsKey(url)) {
      onLoaded(_cache[url].width, _cache[url].height);

    } else {
      final ImageElement img = new ImageElement();
      img.onLoad.listen((_) {
        var iconSize = new _IconSize(img.width, img.height);
        _cache[url] = iconSize;
        withSize(url, onLoaded);
      });
      img.src = url;
    }
  }
}

Turns out there's a naturalWidth property. 原来有一个naturalWidth属性。 Thanks w3schools ! 谢谢w3schools

You need to access it on the image's onload event 您需要在图片的onload事件中访问它

JS JS

  var testImg = document.getElementById('testImg'), 
    iWidth=document.getElementById('width'),
    iHeight =    document.getElementById('height');

    testImg.onload = function(){
        console.log(this);
        iWidth.value=this.width;
        iHeight.value=this.height;
    };

Here is the jsfiddle for it: http://jsfiddle.net/mac1175/evgP8/ 这是它的jsfiddle: http : //jsfiddle.net/mac1175/evgP8/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM