JavaScript获取img原始尺寸:微信小程序与浏览器方案对比
本文详解如何在JavaScript中获取img原始尺寸。针对微信小程序,介绍通过wx.getImageInfo或监听loadSuccess事件获取宽高,需注意业务域名配置;针对浏览器环境,解析利用img.naturalWidth和naturalHeight属性的方法,并提供完整HTML与JS代码示例,解决图片变形问题。
获取图片原始尺寸的必要性
在前端开发中,通常无需刻意获取图片的原始尺寸,因为未设置宽高时浏览器会按最佳比例渲染。然而,在微信小程序开发中,image标签默认存在高度,这可能导致图片被压缩或变形。因此,获取图片原始尺寸并据此设置image标签的宽高显得尤为必要。
微信小程序获取方案
微信小程序获取image原始尺寸的方法
<view style="width:100%;" >
<image src="https://sf3-ttcdn-tos.pstatp.com/img/mosaic-legacy/3796/2975850990~300x300.image" bindload="loadSuccess" style="width:{{imageWidth}}px; height:{{imageHeight}}px"></image>
</view>
//js
Page({
data: {
imageHeight: 0,
imageWidth: 0
},
loadSuccess(e){
const { detail: {width, height} } = e // // 这里获取到的就是图片原始尺寸
this.setData({
imageWidth: width,
imageHeight:height
})
}
})
方法一:使用wx.getImageInfo。根据微信官方文档,此方法需要添加业务域名并进行服务端接口验证,流程较为繁琐,一般不推荐优先使用。
方法二:在小程序中监听图片加载完成事件。通过bindload事件获取detail中的width和height,即为图片原始尺寸,并setData更新视图。
浏览器环境获取方案
方法三:在浏览器环境中获取图片尺寸。利用HTMLImageElement对象的naturalWidth和naturalHeight属性可直接获取原始宽高,无需额外网络请求,是浏览器环境下的推荐做法。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>img原始尺寸获取</title>
<style>
.image {
/* height: 20px; 这种写法没什么卵用 */
}
</style>
</head>
<body>
<img class="image" referrerpolicy="no-referrer" src="https://image-static.segmentfault.com/193/916/1939169050-641cff9f16cdc_fix732"
style="width: 300px;">
<script>
// 1. 获取DOM元素的渲染尺寸
const img = document.querySelector('.image');
console.log(img.style.width) // 300px 获取到字符串
console.log(img.style.height) // 如果在标签行内样式没有设置 无法获取到
// 2. 直接获取DOM元素的width和height属性
console.log(img.width) // 300 获取到的数字类型
console.log(img.height) // 533 可以获取到元素的渲染高度
// 3. naturalWidth / naturalHeight (适用于Firefox/IE9/Safari/Chrome/Opera浏览器)
console.log('naturalWidth:', img.naturalWidth) // naturalWidth: 412
console.log('naturalHeight:', img.naturalHeight) // naturalHeight: 732
// 4. 使用Image()对象异步获取图片原始尺寸
function getImageInfo(url) {
return new Promise((resolve, reject) => {
let image = new Image();
image.onload = () => {
resolve({
width: image.width,
height: image.height
})
}
image.onerror = () => {
reject(new Error('image load error'))
}
image.src = url;
})
}
(async () => {
let size = await getImageInfo('https://image-static.segmentfault.com/193/916/1939169050-641cff9f16cdc_fix732')
console.log(size) // {width: 412, height: 732}
})()
// 终极兼容写法 (首先检测浏览器是否支持img.naturalWidth,如果支持直接获取,不支持使用4.Image()对象获取)
async function getImageSize(img) {
if (img.naturalWidth) {
return {
width: img.naturalWidth,
height: img.naturalHeight
}
} else {
return await getImageInfo(img.src)
}
}
</script>
</body>
</html>至此,关于JavaScript实现获取img原始尺寸的方法详解介绍完毕。更多相关JavaScript获取img原始尺寸内容请搜索本站以前的文章或继续浏览下面的相关文章希望大家以后多多支持本站!
网页开发三者分工:HTML结构、CSS样式与JavaScript交互怎么协作
2026-11-05 12:51:22
JS解析URL参数时怎么处理中文乱码和特殊字符
2026-11-05 12:47:23
js-cookie 存储 JSON 对象:自动序列化与反序列化技巧
2026-11-05 12:36:26
marked.js 快速上手:安装与基础 Markdown 转 HTML 示例
2026-11-05 09:32:53
Phaser.js 与 PixiJS、Cocos Creator 的技术选型对比
2026-11-05 08:36:30
