简体   繁体   English

Firefox .naturalHeight属性

[英]Firefox .naturalHeight property

I have the following code: 我有以下代码:

 var checkImgDim=new Image();
 checkImgDim.src=e.target.result;

 if (input.files[0].size > MAX_SIZE) 
    setError("File too large - max 3MB");

 else if (checkImgDim.naturalHeight < MIN_H ||
          checkImgDim.naturalWidth < MIN_W)
    setError("Image dimensions too small");

It works fine under Chrome however with Firefox I'm getting inconsistent results because it seems that naturalHeight and naturalWidth return 0 sometimes and there's a delay before the values are set. 在Chrome上运行良好,但是在Firefox上却出现不一致的结果,因为似乎naturalHeight和naturalWidth有时会返回0,并且在设置值之前会有延迟。 I don't want to load the image into the DOM or use .onload() the whole purpose of the code is simply to check the image dimensions. 我不想将图像加载到DOM或使用.onload(),整个代码的目的仅仅是检查图像尺寸。 I don't think it would be good practice to add a time delay here. 我认为在此处添加时间延迟不是好习惯。 Can someone please tell me what the proper way to check the image dimensions? 有人可以告诉我检查图像尺寸的正确方法吗?

You cannot get image size before it is loaded. 加载图像之前无法获取图像大小。 You should use the load event of the image anyway. 无论如何,您应该使用图像的load事件。 Even if the image URL is actually a Data URI (e. g. if it comes from a FileReader ), it is “loaded” into the image object asynchronously . 即使图像URL实际上是数据URI(例如,它来自FileReader ),也将其异步地 “加载”到图像对象中。

It's not necessary to insert the image as an HTML element into DOM for this. 为此,不必将图像作为HTML元素插入DOM。 Just create a new Image object, add a load listener to it, and then set its src property. 只需创建一个新的Image对象,向其添加load侦听器,然后设置其src属性即可。

var image = new Image;

image.onload = function() {
    alert(this.naturalHeight);
};

image.src = 'example.png';

If the image has already been downloaded before, it will not be downloaded again — its cached copy will be used instead (as long as client-side caching itself is not disabled intentionally via corresponding server-response headers). 如果该映像以前已经下载过,则不会再次下载-将使用其缓存副本(只要没有通过相应的服务器响应标头有意禁用客户端缓存本身)。 If the image URL is actually a Data URI, there is no downloading at all. 如果图像URL实际上是数据URI,则根本没有下载。

Note that naturalWidth and naturalHeight properties are relatively new and some browsers may not support them. 请注意, naturalWidthnaturalHeight属性相对较新,某些浏览器可能不支持它们。 So using regular width and height properties is currently more reliable, and they have the same values as long as their values have not been changed explicitly before. 因此,使用常规的widthheight属性当前更为可靠,并且只要它们的值之前未明确更改即可,它们具有相同的值。

EDITED WITHOUT ONLOAD: 无需下载即可编辑:

I think you can get the image dimensions before it's loaded by using a 0 second interval - it will technically return the dimensions before onload is called but this solution does use a delay like you said you wanted to avoid however I cannot think of another way of doing it. 我认为您可以通过使用0秒的间隔来获取图像尺寸,然后再加载它-从技术上讲,它将在调用onload之前返回尺寸,但是此解决方案确实会像您所说的那样使用延迟,但是我无法想到另一种方法正在做。 This way is at least quicker than onload. 这样至少比加载更快。

var loaded = false;
var checkImgDim = new Image();

checkImgDim.onload = function () { loaded = true; };

var wait = setInterval(function () {
    if (loaded) {
        clearInterval(wait);
    } else {
        console.log(checkImgDim.naturalWidth, checkImgDim.naturalHeight);
    }
}, 0);

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

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