简体   繁体   English

JavaScript如何从XMLHttpRequest获取图像的宽度和高度

[英]JavaScript How to get an image width and height from an XMLHttpRequest

I'm switching an implementation of a cross-site request for an image converted to base 64 from Canvas to XMLHttpRequest and FileReader, so that it can be used inside of web workers. 我正在将跨站点请求的实现切换为从Canvas转换为base 64的图像到XMLHttpRequest和FileReader,以便可以在Web Worker中使用。 And I want to know if there's a way to get the image width and height from the get-go. 我想知道是否有一种方法可以从一开始就获得图像的宽度和高度。

The new function 新功能

var convertFileToDataURLviaFileReader = function (url, callback) {
    var xhr = new XMLHttpRequest();
    xhr.responseType = 'blob';
    xhr.onload = function () {
        var reader = new FileReader();
        reader.onloadend = function () {
            callback(reader.result);
        }
        reader.readAsDataURL(xhr.response);
    };

    // Our workaround for the Amazon CORS issue
    // Replace HTTPs URL with HTTP
    xhr.open('GET', url.replace(/^https:\/\//i, 'http://'));
    xhr.send();
}

Our old Function 我们的旧功能

var convertImgToDataURLviaCanvas = function(url, callback, outputFormat) {
    var img = new Image();
    img.crossOrigin = 'Anonymous';
    img.onload = function () {
        var canvas = document.createElement('CANVAS');
        var ctx = canvas.getContext('2d');
        var dataURL;
        canvas.height = this.height;
        canvas.width = this.width;
        ctx.drawImage(this, 0, 0);
        dataURL = canvas.toDataURL(outputFormat);

        var allInfo = {
            data: dataURL,
            width: this.width,
            height: this.height
        }

        // Add height and width to callback
        callback(allInfo);
        canvas = null;
    };

    // Our workaround for the Amazon CORS issue
    // Replace HTTPs URL with HTTP
    img.src = url.replace(/^https:\/\//i, 'http://');
}

In the old function I can just get the height and width with canvas, and I do so inside of the var allInfo . 在旧函数中,我只能使用canvas获得高度和宽度,而这是在var allInfo内部实现的。 Is there an equivalent for FileReader or some way to get the width and height inside of the new function? 是否有FileReader的等效项或某种获取新函数内部宽度和高度的方法?

To clarify, I am switching to the XMLHttpRequest because Web Workers don't have access to the DOM, so Canvas can't be used. 为了澄清,我切换到XMLHttpRequest因为Web Workers无法访问DOM,因此不能使用Canvas。

Have a look at this code, it's just a quick rework of this example https://developer.mozilla.org/en-US/docs/Web/API/FileReader/readAsDataURL 看一下这段代码,这只是该示例的快速重做https://developer.mozilla.org/en-US/docs/Web/API/FileReader/readAsDataURL

Is your reader.result a string in base 64 encoding ? 您的reader.result是以base 64编码的字符串吗?

  <form>
    <input type="file" onchange="previewFile()"><br>
    <img src="" alt="Image preview..."><!-- height and width are not set -->
  </form>
  <script>
    var previewFile = () => {

      "use strict";

      let file = document.querySelector("input[type=file]").files[0];
      let fileReader = new FileReader();
      let preview = document.querySelector("img")

      fileReader.addEventListener("load", () => {
        preview.src = fileReader.result;
        // here preview.height and preview.width are accessible
        // if height and width are not set in the img element in the html
      });

      if (file) {
        fileReader.readAsDataURL(file);
      }
    };
  </script>

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

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