During the development process of small programs and page layout, we often encounter some image processing problems. For example, if the image is not of fixed height and height, but the image is set to a fixed height and width, then the original If the image is not proportional to the fixed height and width set by the image, the image will be deformed and unclear. At this time, you can use the following proportional scaling method to scale the image so that the image does not deform. Or dynamically obtain the height and width of the image through the bindload method of the image, and dynamically set the height and width of the image so that the height and width of the image layout are equal to the height and width of the original image.
Image proportional scaling tool
//Util.js class Util{ /*** * 按照显示图片的宽等比例缩放得到显示图片的高 * @params originalWidth 原始图片的宽 * @params originalHeight 原始图片的高 * @params imageWidth 显示图片的宽,如果不传就使用屏幕的宽 * 返回图片的宽高对象 ***/ static imageZoomHeightUtil(originalWidth,originalHeight,imageWidth){ let imageSize = {}; if(imageWidth){ imageSize.imageWidth = imageWidth; imageSize.imageHeight = (imageWidth * originalHeight) / originalWidth; }else{//如果没有传imageWidth,使用屏幕的宽 wx.getSystemInfo({ success: function (res) { imageWidth = res.windowWidth; imageSize.imageWidth = imageWidth; imageSize.imageHeight = (imageWidth * originalHeight) / originalWidth; } }); } return imageSize; } /*** * 按照显示图片的高等比例缩放得到显示图片的宽 * @params originalWidth 原始图片的宽 * @params originalHeight 原始图片的高 * @params imageHeight 显示图片的高,如果不传就使用屏幕的高 * 返回图片的宽高对象 ***/ static imageZoomWidthUtil(originalWidth,originalHeight,imageHeight){ let imageSize = {}; if(imageHeight){ imageSize.imageWidth = (imageHeight *originalWidth) / originalHeight; imageSize.imageHeight = imageHeight; }else{//如果没有传imageHeight,使用屏幕的高 wx.getSystemInfo({ success: function (res) { imageHeight = res.windowHeight; imageSize.imageWidth = (imageHeight *originalWidth) / originalHeight; imageSize.imageHeight = imageHeight; } }); } return imageSize; } } export default Util;
Use the image component to load images and obtain them dynamically through bindload The height and width of the image, dynamically set the height and width of the image
<image bindload="imageLoad" style="width:{{imageWidth}}px;;height:{{imageHeight}}px;" src="../pro.png"/>
index.js code is as follows
import Util from '../common/Util'; Page({ data:{ imageWidth:0, imageHeight:0 }, imageLoad: function (e) { //获取图片的原始宽度和高度 let originalWidth = e.detail.width; let originalHeight = e.detail.height; //let imageSize = Util.imageZoomHeightUtil(originalWidth,originalHeight); //let imageSize = Util.imageZoomHeightUtil(originalWidth,originalHeight,375); let imageSize = Util.imageZoomWidthUtil(originalWidth,originalHeight,145); this.setData({imageWidth:imageSize.imageWidth,imageHeight:imageSize.imageHeight}); } })
The above is the detailed content of Summary of problems encountered in processing images in WeChat mini programs. For more information, please follow other related articles on the PHP Chinese website!