当前位置: 首页 > news >正文

【uniapp-小程序-分享图5/4】

utils.js

//裁剪分享的图片为5:4
const makeCanvas = (imgUrl) => {console.log("imgUrl",imgUrl);return new Promise((resolve, reject) => {// 获取图片信息,小程序下获取网络图片信息需先配置download域名白名单才能生效uni.getImageInfo({src: imgUrl,success: (imgInfo) => {let ctx = uni.createCanvasContext('canvas')let canvasW = 0let canvasH = imgInfo.height// 把比例设置为 宽比高 5:4canvasW = (imgInfo.height * 5) / 4// 为画框设置背景色,注意要放在画图前,图会覆盖在背景色上ctx.fillStyle = "#fff";if (imgInfo.width > 400 || imgInfo.height > 320) {canvasW = 400;canvasH = 320;ctx.fillRect(0, 0, canvasW, canvasH);let dWidth = canvasW / imgInfo.width; // canvas与图片的宽度比例let dHeight = canvasH / imgInfo.height; // canvas与图片的高度比例let dWH = imgInfo.width / imgInfo.height; //宽高比if (imgInfo.width > canvasW && imgInfo.height > canvasH) {if (dWH > 1 && dWH < 1.5) {ctx.drawImage(imgInfo.path, (canvasW - imgInfo.width * dHeight) / 2,0, imgInfo.width * dHeight, imgInfo.height *dHeight)} else {if (imgInfo.width > imgInfo.height) {ctx.drawImage(imgInfo.path, 0, (canvasH - imgInfo.height *dWidth) / 2, imgInfo.width * dWidth,imgInfo.height *dWidth)}if (imgInfo.width == imgInfo.height) {ctx.drawImage(imgInfo.path, (canvasW - imgInfo.width *dHeight) / 2, 0, imgInfo.width * dHeight,imgInfo.height * dHeight)}if (imgInfo.width < imgInfo.height) {ctx.drawImage(imgInfo.path, (canvasW - imgInfo.width *dHeight) / 2, 0, imgInfo.width * dHeight,imgInfo.height * dHeight)}}} else {if (imgInfo.width > imgInfo.height) {ctx.drawImage(imgInfo.path, 0, (canvasH - imgInfo.height) / 2,imgInfo.width * dWidth, imgInfo.height)}if (imgInfo.width == imgInfo.height) {ctx.drawImage(imgInfo.path, (canvasW - imgInfo.width * dHeight) / 2,0, imgInfo.width * dHeight, imgInfo.height *dHeight)}if (imgInfo.width < imgInfo.height) {ctx.drawImage(imgInfo.path, (canvasW - imgInfo.width * dHeight) / 2,0, imgInfo.width * dHeight, imgInfo.height *dHeight)}}} else {ctx.fillRect(0, 0, canvasW, canvasH)ctx.drawImage(imgInfo.path,0,0,canvasW,canvasH,(canvasW - imgInfo.width) / 2, // 宽度从中间向两边填充0,canvasW,canvasH)}ctx.draw(false, () => {uni.canvasToTempFilePath({width: canvasW,height: canvasH,destWidth: 750, // 标准的iphone6尺寸的两倍,生成高清图destHeight: 600,canvasId: "canvas",fileType: "jpg", // 注意jpg默认背景为透明success: (res) => {console.log("res.tempFilePath",res.tempFilePath);resolve(res.tempFilePath)},fail: (err) => {console.log("err",err);reject(err)}},this)})},fail: (err) => {reject(err)}})})
}
module.exports = {makeCanvas
}

用的页面

import util from '@/common/js/util.js';
//分享到聊天onShareAppMessage() {return new Promise((resolve, reject) => {let shareMessage = {title: this.liveInfo.wx_title,path: '/subPages/livePages/liveCourse/live_course_info?courseid=' +this.courseid,imageUrl: this.liveInfo.wx_thumb || this.liveInfo.thumb};util.makeCanvas(shareMessage.imageUrl).then(imgPath => {console.log(imgPathm,'imgPath')// uni.hideLoading();resolve({title: shareMessage.title,path: shareMessage.path,imageUrl: imgPath});}).catch(err => {// uni.hideLoading();resolve(shareMessage);});})},
http://www.lryc.cn/news/280048.html

相关文章:

  • 【响应式编程】前置知识和相关技术的总结
  • K8S--安装MySQL8(单机)
  • CMake+QT+大漠插件的桌面应用开发
  • OpenCV-24双边滤波
  • AI智能分析网关V4:太阳能+4G智慧水库远程可视化智能监管方案
  • 第8章-第8节-Java中的文件类File的简单介绍
  • CTF-PWN-沙箱逃脱-【seccomp和prtcl-2】
  • 【Docker篇】使用Docker操作镜像
  • css宽度适应内容
  • 粒子物理和原子核物理的理论在模拟和分析电路中的粒子束和辐射效应中的应用
  • Opentsdb官方优化文档 - 翻译
  • JavaScript深拷贝与浅拷贝的全面解析
  • ESU毅速丨制造企业需不需要建设增材制造中心?
  • Linux shell编程学习笔记39:df命令
  • 简单高效 LaTeX 科学排版 第004集 命令与环境
  • 初识XSS漏洞
  • 白嫖aws创建Joplin server服务器
  • metartc5_jz源码阅读-p2p通过stun服务器进行通信
  • 总结:Java程序员读书清单顺序
  • ubuntu通过virtualbox安装win虚拟机
  • 云流量回溯的工作原理及关键功能
  • DCP文件传输的重要性与应用
  • JAVA面试部分——后端-线程后篇
  • C语言辨析——深入理解字符常量与表达式
  • Springboot + websocket 实现 一对一 单人聊天
  • GEE机器学习——利用最短距离方法进行土地分类和精度评定
  • 数据结构时间复杂度与空间复杂度
  • 【计算机网络】内容整理
  • 【K12】Python写分类电阻问题的求解思路解析
  • 数据库面经---10则