2 回答

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)絡往返。

TA貢獻1876條經(jīng)驗 獲得超6個贊
我無法回答你的問題,但我可以給你另一個有用的信息:
圖像是一種奇怪的動物,它的行為取決于瀏覽器、上下文和像素密度。為確保捕捉圖像的自然尺寸,請使用:
newImg.naturalHeight; newImg.naturalWidth;
而不是newImg.width
or newImg.clientWidth
。
更多信息:https ://developer.mozilla.org/en-US/docs/Web/API/HTMLImageElement/naturalHeight
玩得開心!
添加回答
舉報