JavaScript:获取图片尺寸

JQu*_*eer 80 html javascript image

我只有一个图像的URL.我需要仅使用JavaScript来确定此图像的高度和宽度.页面上的用户无法看到图像.我怎样才能得到它的尺寸?

Shu*_*mii 182

var img = new Image();

img.onload = function(){
  var height = img.height;
  var width = img.width;

  // code here to use the dimensions
}

img.src = url;
Run Code Online (Sandbox Code Playgroud)

  • @AjayGaur因为`onload`是一个监听器,如果正确加载图像,它将被异步调用.如果在设置url后设置监听器,则可以在代码到达设置onload本身之前加载映像,从而导致监听器永远不会被调用.使用类比,如果你给自己一杯水,你先倒水,然后把玻璃杯装满吗?或者你先把玻璃杯然后倒入水中? (16认同)
  • img.naturalWidth 和 img.naturalHeight 是正确答案 (5认同)

nc3*_*c3b 40

做一个新的 Image

var img = new Image();
Run Code Online (Sandbox Code Playgroud)

设置 src

img.src = your_src
Run Code Online (Sandbox Code Playgroud)

得到widthheight

//img.width
//img.height
Run Code Online (Sandbox Code Playgroud)

  • 使用这个确切的代码,我的宽度和高度始终为0. (14认同)
  • 似乎图像必须加载,这是有道理的. (11认同)
  • 附加信息 - 虽然这对我来说第一次是间歇性的.我认为问题是因为图像尚未下载.我通过运行在onload事件处理程序中使用维度的代码来修复,如下所示:img.onload = function(){}; (3认同)
  • 只要图像在浏览器缓存中,此代码就可以工作.缓存无效或删除后.它不会起作用.所以不太可靠. (3认同)
  • 这是行不通的,因为图像尚未加载 - 图像加载是异步完成的,因此您必须等待加载事件。 (2认同)

Dav*_*ham 7

这使用该功能并等待它完成.

http://jsfiddle.net/SN2t6/118/

function getMeta(url){
    var r = $.Deferred();

  $('<img/>').attr('src', url).load(function(){
     var s = {w:this.width, h:this.height};
     r.resolve(s)
  });
  return r;
}

getMeta("http://www.google.hr/images/srpr/logo3w.png").done(function(test){
    alert(test.w + ' ' + test.h);
});
Run Code Online (Sandbox Code Playgroud)

  • “仅使用 JavaScript”,我猜这是 jQuery (3认同)

Bit*_*lue 7

自然宽度自然高度

var img = document.createElement("img");
img.onload = function (event)
{
    console.log("natural:", img.naturalWidth, img.naturalHeight);
    console.log("width,height:", img.width, img.height);
    console.log("offsetW,offsetH:", img.offsetWidth, img.offsetHeight);
}
img.src = "image.jpg";
document.body.appendChild(img);

// css for tests
img { width:50%;height:50%; }
Run Code Online (Sandbox Code Playgroud)


Que*_* S. 7

结合承诺和打字稿输入:

/**
 * Returns image dimensions for specified URL.
 */
export const getImageDimensions = (url: string): Promise<{width: number, height: number}> => {
  return new Promise((resolve, reject) => {
    const img = new Image();
    img.onload = () => resolve({
      width: img.width,
      height: img.height,
    });
    img.onerror = (error) => reject(error);
    img.src = url;
  });
};
Run Code Online (Sandbox Code Playgroud)

用法:

try {
  const {width, height} = await getImageDimensions(entry.NWS_IMAGE);
  console.log(`Image dimensions: ${width}px x ${height}px`);
} catch (e) {
  // Could not load image from specified URL
  console.error(e);
}
Run Code Online (Sandbox Code Playgroud)


小智 5

如果您有输入表单中的图像文件。你可以这样使用

let images = new Image();
images.onload = () => {
 console.log("Image Size", images.width, images.height)
}
images.onerror = () => result(true);

let fileReader = new FileReader();
fileReader.onload = () => images.src = fileReader.result;
fileReader.onerror = () => result(false);
if (fileTarget) {
   fileReader.readAsDataURL(fileTarget);
}
Run Code Online (Sandbox Code Playgroud)