微信小程序 压缩图片并上传
转自https://segmentfault.com/q/1010000012507519
wxml写入
<view bindtap='uploadImg'>上传</view>
<image bindtap='uploadImg' data-number="0" src="{{ uploadPic[0] }}" alt="" mode="widthFix"></image>
<canvas style="width: {{cw}}px; height: {{ch}}px;" canvas-id="firstCanvas"></canvas>
JS参考
uploadImg(e) {
let that = this;
console.log(e);
let index = e.currentTarget.dataset.number;
let uploadFile = ''; //最后处理完,图片上传的图片地址
wx.chooseImage({
success(res) {
console.log(res)
const tempFilePaths = res.tempFilePaths;
//获得原始图片大小
wx.getImageInfo({
src: res.tempFilePaths[0],
success(res) {
// console.log('获得原始图片大小',res.width)
//console.log(res.height)
var originWidth, originHeight;
originHeight = res.height;
originWidth = res.width;
console.log(originWidth);
//压缩比例
// 最大尺寸限制
var maxWidth = 1200,
maxHeight = 600;
// 目标尺寸
var targetWidth = originWidth,
targetHeight = originHeight;
//等比例压缩,如果宽度大于高度,则宽度优先,否则高度优先
if (originWidth > maxWidth || originHeight > maxHeight) {
if (originWidth / originHeight > maxWidth / maxHeight) {
// 要求宽度*(原生图片比例)=新图片尺寸
targetWidth = maxWidth;
targetHeight = Math.round(maxWidth * (originHeight / originWidth));
} else {
targetHeight = maxHeight;
targetWidth = Math.round(maxHeight * (originWidth / originHeight));
}
}
//尝试压缩文件,创建 canvas
var ctx = wx.createCanvasContext('firstCanvas');
ctx.clearRect(0, 0, targetWidth, targetHeight);
ctx.drawImage(tempFilePaths[0], 0, 0, targetWidth, targetHeight);
ctx.draw();
//更新canvas大小
that.setData({
cw: targetWidth,
ch: targetHeight
});
//保存图片
setTimeout(function() {
wx.canvasToTempFilePath({
canvasId: 'firstCanvas',
success: (res) => {
//写入图片数组
var uploadpic = "uploadPic[" + index + "]";
//
that.setData({
[uploadpic]: res.tempFilePath
});
uploadFile = res.tempFilePath;
//保存到相册
// wx.saveImageToPhotosAlbum({
// filePath: res.tempFilePath,
// success: (res) => {
// console.log(res)
// },
// fail: (err) => {
// console.error(err)
// }
// })
wx.uploadFile({
url: 'https://example.weixin.qq.com/upload', //仅为示例,非真实的接口地址
filePath: uploadFile,
name: 'file',
formData: {
'user': 'test'
},
success(res) {
const data = res.data
//do something
}
})
},
fail: (err) => {
console.error(err)
}
}, this)
}, 500);
}
})
}
})
}
微信小程序 压缩图片并上传的更多相关文章
- 微信小程序压缩图片并上传到服务器(拿去即用)
这里注意一下,图片压缩后的宽度是画布宽度的一半 canvasToTempFilePath 创建画布的时候会有一定的时间延迟容易失败,这里加setTimeout来缓冲一下 这是单张图片压缩,多张的压缩暂 ...
- 微信小程序实现图片是上传、预览功能
本文实例讲述了微信小程序实现图片上传.删除和预览功能的方法,分享给大家供大家参考,具体如下: 这里主要介绍一下微信小程序的图片上传图片删除和图片预览 1.可以调用相机也可以从本地相册选择 2.本地实现 ...
- 微信小程序实现图片裁剪上传(wepy)
参考https://github.com/we-plugin/we-cropper,在wepy中实现,参考的具体例子是we-cropper/example/cutInside/ 项目上传图片时2:3的 ...
- 微信小程序裁剪图片后上传
上传图片的时候调起裁剪页面,裁剪后再回调完成上传; 图片裁剪直接用we-cropper https://github.com/we-plugin/we-cropper we-cropper使用详细 ...
- 微信小程序:多张图片上传
最近在写小程序的相册,需要多张图片的上传.因为小程序不支持数组的多张图片同时上传,然后根据自己的需求+借鉴网上各位大神的案例,总算搞定.分享下,不足之处,多多指教哦 页面wxml: <form ...
- 微信小程序入门八头像上传
1. action-sheet 底部弹出可选菜单组件 2. wx.uploadFile 将本地资源上传到服务器 3. wx.chooseImage 从本地相册选择图片或使用相机拍照. 4. wx.pr ...
- 微信小程序--更换用户头像/上传用户头像/更新用户头像
changeAvatar:function (){ var that=this; wx.chooseImage({ count: 1, // 默认9 sizeType: ['original', 'c ...
- 微信小程序裁剪图片成圆形
代码地址如下:http://www.demodashi.com/demo/14453.html 前言 最近在开发小程序,产品经理提了一个需求,要求微信小程序换头像,用户剪裁图片必须是圆形,也在gith ...
- 使用canvas压缩图片 并上传
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
随机推荐
- python数据类型之字典(dict)和其常用方法
字典的特征: key-value结构key必须可hash,且必须为不可变数据类型.必须唯一. # hash值都是数字,可以用类似于2分法(但比2分法厉害的多的方法)找.可存放任意多个值.可修改.可以不 ...
- SQL防止重复提交和Filter
/class User package com.neuedu.bean; import java.io.Serializable; public class User implements Seria ...
- oracle如何保证读一致性 第一弹
oracle保证读一致性原理 1:undo segment的概念 当数据库进行修改的时候,需要把保存到以前的old的数据保存到一个地方,然后进行修改,用于保存o ...
- LeetCode(228) Summary Ranges
题目 Given a sorted integer array without duplicates, return the summary of its ranges. For example, g ...
- 在终端更改MAC的MySQL的root密码
- Python动态属性和特性(二)
内置的property经常用作装饰器,但它其实是一个类.在Python中,函数和类通常可以互换,因为二者都是可调用对象,而且没有实例化的new运算符,所以调用构造方法和调用工厂函数没有区别,只要能返回 ...
- jdk生成证书,网站请求变成https
生成证书的步骤 1.进入jdk的bin目录 keytool -genkey -alias tomcat -keyalg RSA 命名证书的名字叫tomcat 2.将证书拷贝至tomcat的bin目 ...
- python 学习分享-基础篇
1.python起手式 写下第一个代码,打印‘hello world’ print('hello world') 2.变量 变量是为了存储信息,在程序中被调用,标识数据名称或类型. 变量定义的规则: ...
- Python hash、xml、configparser、sheve、shutil模块讲解 以及 面向对象初识
今日内容: 1.hash模块2.xml模块3.configparser模块4.sheve 模块5.shutil模块 知识点一:hash什么是hash: hash是一种算法,该算法接受传入的的内容,经过 ...
- Java容器jdk1.6 Array
参考:https://www.cnblogs.com/tstd/p/5042087.html 1.定义 顶层接口collection public interface Collection<E& ...