微信小程序裁剪图片后上传
上传图片的时候调起裁剪页面,裁剪后再回调完成上传;
图片裁剪直接用we-cropper https://github.com/we-plugin/we-cropper
we-cropper使用详细方法参考 https://we-plugin.github.io/we-cropper/#/
chooseImage: function(e){
var _this = this;
wx.chooseImage({
count: 1,
sizeType: ['original', 'compressed'],
sourceType: ['album', 'camera'],
success: function (res) {
var tempFilePaths = res.tempFilePaths[0];
wx.navigateTo({
url: '/pages/new/imgcorp?img='+tempFilePaths,
});
}
})
},
uploadImage(path){
var _this = this;
wx.showLoading({
title: '正在上传..',
});
wx.uploadFile({
url: app.globalData.domain + 'user/uploadimage',
filePath: path,
name: 'file',
formData: {
'uid': app.globalData.userId
},
success: function (res) {
var data = JSON.parse(res.data);
if (data.status == 0) {
wx.showToast({
title: data.err,
duration: 2000
});
return false;
}
wx.hideLoading();
_this.setData({
imageurls: 'Uploads/' + data.urls,
postimage: path
});
}
})
},
imgcorp.wxml
<!--pages/new/imgcorp.wxml-->
<template name="we-cropper">
<canvas
class="cropper {{cutImage}}"
disable-scroll="true"
bindtouchstart="touchStart"
bindtouchmove="touchMove"
bindtouchend="touchEnd"
style="width:{{width}}px;height:{{height}}px;"
canvas-id="{{id}}">
</canvas>
</template> <view class="cropper-wrapper {{cutImage}}">
<template is="we-cropper" data="{{...cropperOpt}}"/>
</view>
<view class="operbtns">
<button class="button" type="primary" bindtap="getCropperImage">完成</button>
</view>
imgcorp.js
//pages /new /imgcorp.js
import WeCropper from '../../utils/we-cropper.js'
const device = wx.getSystemInfoSync()
const width = device.windowWidth
const height = device.windowHeight - 100; Page({ data: {
cropperOpt: {
id: 'cropper',
width,
height,
scale: 2.5,
zoom: 8,
cut: {
x: (width - 150) / 2,
y: (height - 150) / 2,
width: 150,
height: 150
}
} }, onLoad: function (options) {
this.data.cropperOpt.src = options.img;
const { cropperOpt } = this.data
new WeCropper(cropperOpt)
.on('beforeImageLoad', (ctx) => {
wx.showToast({
title: '上传中',
icon: 'loading',
duration: 20000
})
})
.on('imageLoad', (ctx) => {
wx.hideToast()
})
.updateCanvas();
},
touchStart(e) {
this.wecropper.touchStart(e)
},
touchMove(e) {
this.wecropper.touchMove(e)
},
touchEnd(e) {
this.wecropper.touchEnd(e)
},
getCropperImage() {
var that = this;
that.wecropper.getCropperImage((src) => {
if (src) {
var pages = getCurrentPages();
var currPage = pages[pages.length - 1]; //当前页面
var prevPage = pages[pages.length - 2]; //上一个页面
prevPage.uploadImage(src);
wx.navigateBack();
}
})
},
})
微信小程序裁剪图片后上传的更多相关文章
- 微信小程序实现图片是上传、预览功能
本文实例讲述了微信小程序实现图片上传.删除和预览功能的方法,分享给大家供大家参考,具体如下: 这里主要介绍一下微信小程序的图片上传图片删除和图片预览 1.可以调用相机也可以从本地相册选择 2.本地实现 ...
- 微信小程序 压缩图片并上传
转自https://segmentfault.com/q/1010000012507519 wxml写入 <view bindtap='uploadImg'>上传</view> ...
- 微信小程序压缩图片并上传到服务器(拿去即用)
这里注意一下,图片压缩后的宽度是画布宽度的一半 canvasToTempFilePath 创建画布的时候会有一定的时间延迟容易失败,这里加setTimeout来缓冲一下 这是单张图片压缩,多张的压缩暂 ...
- 微信小程序裁剪图片成圆形
代码地址如下:http://www.demodashi.com/demo/14453.html 前言 最近在开发小程序,产品经理提了一个需求,要求微信小程序换头像,用户剪裁图片必须是圆形,也在gith ...
- 小程序踩坑记录-上传图片及canvas裁剪图片后上传至服务器
最近在写微信小程序的上传图片功能,趟过了一些坑记录一下. 想要满足的需求是,从手机端上传图片至服务器,为了避免图片过大影响传输效率,需要把图片裁剪至适当大小后再传输 主要思路是,通过wx.choose ...
- 微信小程序:多张图片上传
最近在写小程序的相册,需要多张图片的上传.因为小程序不支持数组的多张图片同时上传,然后根据自己的需求+借鉴网上各位大神的案例,总算搞定.分享下,不足之处,多多指教哦 页面wxml: <form ...
- jquery photoClip支持手机端,PC端 本地裁剪图片后上传插件
支持手机,PC最好的是jquery photoClip插件,下载地址&示例:https://github.com/topoadmin/photoClip demo.html 代码: <! ...
- 微信小程序入门八头像上传
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 ...
随机推荐
- Ubantu学习笔记1
重启后按e键进行编辑,在文档倒数第二行r0处修改为rw init=/bin/bash 然后F10操作,输入passwd zichua =>修改此用户名的密码,重新输入两次密码(这里密码是看不到的 ...
- IBGP(内部BGP)的对等体组(命令解析)
IBGP(内部BGP)对等体组配置解析: ①:创建对等体组. ②:定义对等体组策略,指定邻居路由器及所在的AS. ③:定义,更新源. ④:(若边界)定义自己下一跳. ⑤:加入对等体组. IBGP(内部 ...
- LeJOS下eclipse通过USB连接EV3
适用情况:在eclipse安装好LeJOS插件后,再通过USB连接EV3. 第一:更新驱动 在<设备管理器-网络适配器>下更新RNDIS/Ethernet Gadget,电脑需要先连接EV ...
- python np array转json
np array转json import numpy as np import codecs, json a = np.arange().reshape(,) # a by array b = a.t ...
- jQuery实现轮播图--入门
jQuery是一个前台的框架. 主要函数: setInterval 语法:setInterval(code,millisec[,"lang"]) cdoe:需要执行的代码或者要调用 ...
- html_位置偏移属性position
定位属性 位置属性position:static.relative.absolute.fixed 偏移属性:top.bottom.left.right 浮动定位属性:float/clear 1.浮动定 ...
- 小白学习之pytorch框架(7)之实战Kaggle比赛:房价预测(K折交叉验证、*args、**kwargs)
本篇博客代码来自于<动手学深度学习>pytorch版,也是代码较多,解释较少的一篇.不过好多方法在我以前的博客都有提,所以这次没提.还有一个原因是,这篇博客的代码,只要好好看看肯定能看懂( ...
- Javascript object.constructor属性与面向对象编程(oop)
定义和用法 在 JavaScript 中, constructor 属性返回对象的构造函数. 返回值是函数的引用,不是函数名: JavaScript 数组 constructor 属性返回 funct ...
- dp--P1439 最长公共子序列(LCS)
题目描述 给出1-n的两个排列P1和P2,求它们的最长公共子序列. 输入格式 第一行是一个数n, 接下来两行,每行为n个数,为自然数1-n的一个排列. 输出格式 一个数,即最长公共子序列的长度 找出两 ...
- FPGA CRC-16/XMODEM x16+x12+x5+1
module crc_16( input clk, input [47:0]mac, input rst, input hash_enable,//哈希控制器使能位 output reg hash_c ...