第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Javascript:在一個請求中獲取圖像的尺寸和大小

Javascript:在一個請求中獲取圖像的尺寸和大小

LEATH 2022-07-08 15:46:27
基于此處的其他帖子,我寫了這個來獲取文件的尺寸和大小    const newImg = new Image();    newImg.onload = () => {      console.log('height ' + newImg.height);      console.log('width ' + newImg.width);      const xhr = new XMLHttpRequest();      xhr.open('GET', 'https://www.google.com/myImage.png', true);      xhr.responseType = 'arraybuffer';      xhr.onreadystatechange = function() {        if (this.readyState === this.DONE) {          console.log('size ' + this.response.byteLength);        }      };      xhr.send(null);   }   newImg.src = 'https://www.google.com/myImage.png';雖然它有效,但我想知道我是否可以將兩者合二為一,這意味著只做the或Image onLoadrequestxhr嗎?
查看完整描述

2 回答

?
MM們

TA貢獻1886條經(jīng)驗 獲得超2個贊

您可以通過使用Blob和數(shù)據(jù) URI來避免第二次網(wǎng)絡往返:


fetch('https://cdn.glitch.com/2eddb7d4-12e2-45ae-8d27-738f13fb514a%2FGOPR1017_1586689900523.JPG?v=1587457335788')

.then(r => r.arrayBuffer())

.then(buffer => {

  console.log('Size: ' + buffer.byteLength)

  const blob = new Blob([buffer], {type: 'image/jpeg'})

  const img = new Image()

  img.src = URL.createObjectURL(blob)

  img.onload = function() {

    console.log(img.width, img.height)    

  }

})

請注意,您仍然需要onload回調(diào),因為瀏覽器需要一些時間來解析圖像,但在這種情況下,它不會導致網(wǎng)絡往返。


查看完整回答
反對 回復 2022-07-08
?
HUX布斯

TA貢獻1876條經(jīng)驗 獲得超6個贊

我無法回答你的問題,但我可以給你另一個有用的信息:

圖像是一種奇怪的動物,它的行為取決于瀏覽器、上下文和像素密度。為確保捕捉圖像的自然尺寸,請使用:

newImg.naturalHeight;
newImg.naturalWidth;

而不是newImg.widthor newImg.clientWidth。

更多信息:https ://developer.mozilla.org/en-US/docs/Web/API/HTMLImageElement/naturalHeight

玩得開心!


查看完整回答
反對 回復 2022-07-08
  • 2 回答
  • 0 關(guān)注
  • 153 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號