How to get the width and height of the image in WeChat applet

How to get the width and height of the image in WeChat applet

origin

Recently, I am working on requirement A, in which there is a small function point described as follows: return a picture from the configuration end, and expect the width to remain unchanged (750) and the height to adapt to the picture.

I thought

// For ease of explanation, bind css as style attribute [not recommended] && hard-code the value of src [can be changed to the value returned by the interface later]
<view style="width:100%;">
 <image src="{{src}}"></image>
</view>

What I thought of at first was: setting the width of the content area: 100% will automatically fill the screen width, and the height will be adaptive.

Actual effect: The space occupied by the image is: screen width x 0

Solution

Core: Solve how to get the height of the image

Primary Plan

Key point: Get the corresponding image information after the image is loaded.

After checking the applet development documentation, I found that there is a callback for successful loading, as follows:

The demo is as follows:

// wxml
<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
 })
 }
})

Let’s take a look at the effect first:

Think about this: If I have 100 images that need to be adapted, will there be a lot of cumbersome setData() calls, which will also cause performance problems.

Advanced Solution

After a friend reminded me, I found that the mini program image has a property called mode, which can be used to set the image cropping and scaling.

The possible values ​​for the mode attribute are as follows:

Without further ado, let's see what the actual effect is:

// 750x110 image <view style="width:100%;" >
 <image src="https://p3-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/ba1f75f0d29c40759b43ef910dacb4e7~tplv-k3u1fbpfcp-watermark.image" mode="widthFix"></image>
</view>

// 750x480 image <view style="width:100%;" >
 <image src="https://p3-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/ba1f75f0d29c40759b43ef910dacb4e7~tplv-k3u1fbpfcp-watermark.image" mode="widthFix"></image>
</view>

Take a look at the 750x110 rendering:

Let’s take a look at the 750x480 effect picture:

At this point, you only need to change the value of src to the value returned by the interface, and the requirements of fixed width and adaptive height will be met.

at last

This property is mainly to achieve image adaptability. In other words, it is mainly to ensure that the image is not distorted.

This is the end of this article about how to obtain the width and height of an image in WeChat Mini Program. For more information about how to obtain the width and height of an image in WeChat Mini Program, please search for previous articles on 123WORDPRESS.COM or continue to browse the related articles below. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • WeChat applet dynamically sets the height and width of the picture with detailed explanation and example code
  • WeChat applet picture width and height adaptation detailed explanation
  • WeChat applet implements the method of displaying the image component image in adaptive width ratio
  • WeChat applet implements adaptive width of rich text pictures
  • WeChat applet images are displayed at 100% width without distortion
  • Implementation of WeChat applet picture width adaptation

<<:  Detailed explanation of Mysql 5.7.18 installation method and the process of starting MySQL service

>>:  How to create https using nginx and Tencent Cloud free certificate

Recommend

How to uninstall MySQL 5.7 on CentOS7

Check what is installed in mysql rpm -qa | grep -...

Pure CSS to modify the browser scrollbar style example

Use CSS to modify the browser scroll bar style ::...

How to use the yum command

1. Introduction to yum Yum (full name Yellow dogU...

How to locate MySQL slow queries

Preface I believe that everyone has had experienc...

Steps to build the vite+vue3+element-plus project

Use vite to build a vue3 project You can quickly ...

How to submit a pure HTML page, pass parameters, and verify identity

Since the project requires a questionnaire, but th...

Summary of commonly used SQL in MySQL operation tables

1. View the types of fields in the table describe...

Detailed explanation of three methods of JS interception string

JS provides three methods for intercepting string...

Summary of Linux commands commonly used in work

Use more open source tools such as docker and kub...

Detailed explanation of client configuration for vue3+electron12+dll development

Table of contents Modify the repository source st...

Implementation of Nginx domain name forwarding

Introduction to Nginx Nginx ("engine x"...

CSS uses the autoflow attribute to achieve seat selection effect

1. Autoflow attribute, if the length and width of...

Vue implements tab navigation bar and supports left and right sliding function

This article mainly introduces: using Vue to impl...