首页 > 编程语言 >How to get the original size of an image using JavaScript All In One

How to get the original size of an image using JavaScript All In One

时间:2023-09-27 09:56:39浏览次数:46  
标签:naturalHeight image JavaScript naturalWidth https get 256 size

How to get the original size of an image using JavaScript All In One

如何使用 JavaScript 获取一个图片像的原始大小

naturalWidth & naturalHeight

let output = document.querySelector(".output");
let image = document.querySelector("img");

window.addEventListener("load", (event) => {
  output.innerHTML += `Natural size: ${image.naturalWidth} x ` + `${image.naturalHeight} pixels<br>`;
  output.innerHTML += `Displayed size: ${image.width} x ` + `${image.height} pixels`;
});

https://developer.mozilla.org/en-US/docs/Web/API/HTMLImageElement/naturalWidth
https://developer.mozilla.org/en-US/docs/Web/API/HTMLImageElement/naturalHeight

Experimental ⚠️

https://developer.mozilla.org/en-US/docs/Web/API/PerformanceElementTiming/naturalWidth
https://developer.mozilla.org/en-US/docs/Web/API/PerformanceElementTiming/naturalHeight

WHATWG

The IDL attributes naturalWidth and naturalHeight must return the density-corrected natural width and height of the image, in CSS pixels, if the image has density-corrected natural width and height and is available, or else 0.

如果图像具有密度校正的自然宽度和高度并且可用,则 IDL 属性 naturalWidth 和 naturalHeight 必须返回图像的密度校正的自然宽度和高度(以 CSS 像素为单位),否则返回 0.

https://html.spec.whatwg.org/multipage/embedded-content.html#dom-img-naturalwidth
https://html.spec.whatwg.org/multipage/embedded-content.html#dom-img-naturalheight

image

https://caniuse.com/?search=naturalWidth %26 naturalHeight

demos

<img src="https://i.stack.imgur.com/deTzi.jpg?s=256&amp;g=1" alt="xgqfrms's user avatar" width="128" height="128" class="bar-sm bar-md d-block">
const img = document.querySelector(`img`);
img.naturalHeight;
// 256
img.naturalWidth;
// 256

image

aspect ratio / 纵横比

Rendered size:	128 × 128 px
Rendered aspect ratio:	1∶1
Intrinsic size:	256 × 256 px
Intrinsic aspect ratio:	1∶1
File size:	27.0 kB
Current source:	https://i.stack.imgur.com/deTzi.jpg?s=256&g=1
渲染尺寸:128 × 128 像素
渲染纵横比:1:1
内在尺寸:256 × 256 像素
固有长宽比:1:1
文件大小:27.0 kB
当前来源:https://i.stack.imgur.com/deTzi.jpg?s=256&g=1

https://stackoverflow.com/users/5934465/xgqfrms?tab=reputation

(

标签:naturalHeight,image,JavaScript,naturalWidth,https,get,256,size
From: https://www.cnblogs.com/xgqfrms/p/17731964.html

相关文章

  • JavaScript-Object 对象的相关方法
    Object.getPrototypeOf() Object.getPrototypeOf方法返回参数对象的原型。这是获取原型对象的标准方法。varF=function(){};varf=newF();Object.getPrototypeOf(f)===F.prototype//true上面代码中,实例对象f的原型是F.prototype。下面是几种特殊对象的原型。//空对......
  • 无涯教程-JavaScript - T.TEST函数
    描述T.TEST函数返回与学生t检验相关的概率。使用T.TEST来确定两个样本是否可能来自均值相同的两个相同基础人口。语法T.TEST(array1,array2,tails,type)争论Argument描述Required/OptionalArray1Thefirstdataset.RequiredArray2Theseconddataset.Required......
  • 无涯教程-JavaScript - T.DIST.2T函数
    描述T.INV.2T函数返回学生t分布的两尾逆。语法T.INV.2T(probability,deg_freedom)争论Argument描述Required/OptionalProbabilityTheprobabilityassociatedwiththeStudent'stdistribution.RequiredDeg_freedomAnintegerindicatingthenumberofdegrees......
  • IDEA中的java代码Getters和Setters报红问题解决办法【杭州多测师_王sir】
    今天在新的编辑器中导入新项目时,发现很多get、set、toString的相关方法全部报红,仔细排查发现,原来是bean中注解采用lombok来自动生成get、set、toStirng、equals等方法,而新的编辑器未安装lombok plugin,所以全部报红。Lombok简介项目中经常使用bean,entity等类,绝大部分数据类类中都......
  • 无涯教程-JavaScript - T.DIST函数
    描述T.DIST.2T函数返回两尾学生的t分布。学生的分布用于小样本数据集的假设检验。使用此函数代替t分布的临界值表。语法T.DIST(x,deg_freedom,cumulative)争论Argument描述Required/OptionalXThenumericvalueatwhichtoevaluatethedistribution.RequiredDeg......
  • Csproj 编译输出引用Nuget包内的资源文件
    组内有个组件,对外部Nuget包Microsoft.Web.WebView2封装。因为WebView2对自身有一些资源文件依赖,资源文件需要随编译输出到启动目录,WebView2直接加载启动目录下相应文件。 如果上层应用同时引用Microsoft.Web.WebView2,自然会输出对应的资源文件。但应用层很容易遗漏对Microsof......
  • 无涯教程-JavaScript - STDEVPA函数
    描述STDEVPA函数根据作为参数给出的总体(包括文本和逻辑值)计算标准偏差。语法STDEVPA(value1,[value2]...)争论Argument描述Required/OptionalValue11到255对应于总体的值。您也可以使用单个数组或对数组的引用,而不要使用以逗号分隔的参数。RequiredValue......
  • 无涯教程-JavaScript - STDEVA函数
    描述STDEVA函数根据样本估算标准偏差。标准偏差是对值与平均值(平均值)的分散程度的度量。语法STDEVA(value1,[value2]...)争论Argument描述Required/OptionalValue11至255个值,对应于总体样本。您也可以使用单个数组或对数组的引用,而不要使用以逗号分隔的参数......
  • JavaScript——判断0, NaN, false, null, underfined, 空字符串
    1.判断NaN//NaN出现的原因:(NotaNumber)//操作两个类型不一致的数、用NaN值计算最终得到NaN、不合法运算(如0/0)letnum=0/0;if(isNaN(num)){console.log("num为NaN!")}2.判断undefinedlettemp=undefined//方法一if(typeof(temp)=="undefined"......
  • GET和POST请求的区别
    HTTP是超文本传输协议,用来定义客户端与服务器数据传输的规范。HTTP服务端默认端口为80,HTTPS默认端口为443,客户端的端口是动态分配的。GET请求和POST请求都是HTTP请求八种(GET、POST、PUT、DELETE、PATCH、HEAD、OPTIONS)方法中的其中一种。1、GETGET请求是一个幂等的请......