简体   繁体   English

从 url 获取远程图像的宽度高度

[英]Get width height of remote image from url

So the alert gives undefined values for the width and height.所以警报给出了未定义的宽度和高度值。 I think the w and h values of the image from the img.onload calculation is not being passed to the values to return, or it may be returning w and h before the onload calculates them:我认为来自 img.onload 计算的图像的 w 和 h 值没有传递给要返回的值,或者它可能在 onload 计算它们之前返回 w 和 h:

function getMeta(url){
 var w; var h;
 var img=new Image;
 img.src=url;
 img.onload=function(){w=this.width; h=this.height;};
 return {w:w,h:h}    
}

// "http://snook.ca/files/mootools_83_snookca.png" //1024x678
// "http://shijitht.files.wordpress.com/2010/08/github.png" //128x128

var end = getMeta("http://shijitht.files.wordpress.com/2010/08/github.png");
var w = end.w;
var h = end.h;
alert(w+'width'+h+'height');

How can I have the alert show the correct width and height?如何让警报显示正确的宽度和高度?

http://jsfiddle.net/YtqXk/ http://jsfiddle.net/YtqXk/

So the alert gives undefined values for the width and height.所以警报给出了宽度和高度的未定义值。 I think the w and h values of the image from the img.onload calculation is not being passed to the values to return, or it may be returning w and h before the onload calculates them:我认为来自 img.onload 计算的图像的 w 和 h 值没有传递给要返回的值,或者它可能在 onload 计算它们之前返回 w 和 h:

function getMeta(url){
 var w; var h;
 var img=new Image;
 img.src=url;
 img.onload=function(){w=this.width; h=this.height;};
 return {w:w,h:h}    
}

// "http://snook.ca/files/mootools_83_snookca.png" //1024x678
// "http://shijitht.files.wordpress.com/2010/08/github.png" //128x128

var end = getMeta("http://shijitht.files.wordpress.com/2010/08/github.png");
var w = end.w;
var h = end.h;
alert(w+'width'+h+'height');

How can I have the alert show the correct width and height?如何让警报显示正确的宽度和高度?

http://jsfiddle.net/YtqXk/ http://jsfiddle.net/YtqXk/

So the alert gives undefined values for the width and height.所以警报给出了宽度和高度的未定义值。 I think the w and h values of the image from the img.onload calculation is not being passed to the values to return, or it may be returning w and h before the onload calculates them:我认为来自 img.onload 计算的图像的 w 和 h 值没有传递给要返回的值,或者它可能在 onload 计算它们之前返回 w 和 h:

function getMeta(url){
 var w; var h;
 var img=new Image;
 img.src=url;
 img.onload=function(){w=this.width; h=this.height;};
 return {w:w,h:h}    
}

// "http://snook.ca/files/mootools_83_snookca.png" //1024x678
// "http://shijitht.files.wordpress.com/2010/08/github.png" //128x128

var end = getMeta("http://shijitht.files.wordpress.com/2010/08/github.png");
var w = end.w;
var h = end.h;
alert(w+'width'+h+'height');

How can I have the alert show the correct width and height?如何让警报显示正确的宽度和高度?

http://jsfiddle.net/YtqXk/ http://jsfiddle.net/YtqXk/

So the alert gives undefined values for the width and height.所以警报给出了宽度和高度的未定义值。 I think the w and h values of the image from the img.onload calculation is not being passed to the values to return, or it may be returning w and h before the onload calculates them:我认为来自 img.onload 计算的图像的 w 和 h 值没有传递给要返回的值,或者它可能在 onload 计算它们之前返回 w 和 h:

function getMeta(url){
 var w; var h;
 var img=new Image;
 img.src=url;
 img.onload=function(){w=this.width; h=this.height;};
 return {w:w,h:h}    
}

// "http://snook.ca/files/mootools_83_snookca.png" //1024x678
// "http://shijitht.files.wordpress.com/2010/08/github.png" //128x128

var end = getMeta("http://shijitht.files.wordpress.com/2010/08/github.png");
var w = end.w;
var h = end.h;
alert(w+'width'+h+'height');

How can I have the alert show the correct width and height?如何让警报显示正确的宽度和高度?

http://jsfiddle.net/YtqXk/ http://jsfiddle.net/YtqXk/

So the alert gives undefined values for the width and height.所以警报给出了宽度和高度的未定义值。 I think the w and h values of the image from the img.onload calculation is not being passed to the values to return, or it may be returning w and h before the onload calculates them:我认为来自 img.onload 计算的图像的 w 和 h 值没有传递给要返回的值,或者它可能在 onload 计算它们之前返回 w 和 h:

function getMeta(url){
 var w; var h;
 var img=new Image;
 img.src=url;
 img.onload=function(){w=this.width; h=this.height;};
 return {w:w,h:h}    
}

// "http://snook.ca/files/mootools_83_snookca.png" //1024x678
// "http://shijitht.files.wordpress.com/2010/08/github.png" //128x128

var end = getMeta("http://shijitht.files.wordpress.com/2010/08/github.png");
var w = end.w;
var h = end.h;
alert(w+'width'+h+'height');

How can I have the alert show the correct width and height?如何让警报显示正确的宽度和高度?

http://jsfiddle.net/YtqXk/ http://jsfiddle.net/YtqXk/

So the alert gives undefined values for the width and height.所以警报给出了宽度和高度的未定义值。 I think the w and h values of the image from the img.onload calculation is not being passed to the values to return, or it may be returning w and h before the onload calculates them:我认为来自 img.onload 计算的图像的 w 和 h 值没有传递给要返回的值,或者它可能在 onload 计算它们之前返回 w 和 h:

function getMeta(url){
 var w; var h;
 var img=new Image;
 img.src=url;
 img.onload=function(){w=this.width; h=this.height;};
 return {w:w,h:h}    
}

// "http://snook.ca/files/mootools_83_snookca.png" //1024x678
// "http://shijitht.files.wordpress.com/2010/08/github.png" //128x128

var end = getMeta("http://shijitht.files.wordpress.com/2010/08/github.png");
var w = end.w;
var h = end.h;
alert(w+'width'+h+'height');

How can I have the alert show the correct width and height?如何让警报显示正确的宽度和高度?

http://jsfiddle.net/YtqXk/ http://jsfiddle.net/YtqXk/

You will can try package .您可以尝试package

import getImageSize from 'image-size-from-url';

const {width, height} = await getImageSize('URL');

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

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