微信小程序裁剪图片后上传
上传图片的时候调起裁剪页面,裁剪后再回调完成上传;
图片裁剪直接用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 ...
随机推荐
- 超低功耗Sub-1GHz性价比首选方案:CMT2300
关于超低功耗Sub-1GHz射频收发器,目前性价比方面CMT2300是一款大多客户的首选方案,不管是成本方面还是性能方面,都能大大的满足客户的需求.下面为大家讲解下CMT2300 这款Sub-1GHz ...
- 51Nod1049 最大子段和
我们来先看题: N个整数组成的序列a1,a2,a3,-,an,求该序列如ai+ai+1+-+aj的连续子段和的最大值.当所给的整数均为负数时和为0. 例如:-2,11,-4,13,-5,-2,和最大的 ...
- HBase从入门到精通系列:误删数据如何抢救?
云栖君导读:有时候我们操作数据库的时候不小心误删数据,这时候如何找回?mysql里有binlog可以帮助我们恢复数据,但是没有开binlog也没有备份就尴尬了.如果是HBase,你没有做备份误删了又如 ...
- Android自定义View——刮刮卡效果
想要红包的实现效果的可以关注我的博客,仿饿了么红包 下层图片:我们的红包的图片 上层图片:有两部分 一部分是灰色背景 一部分是拥有透明度为0,并且模式为交集的画笔 使用滑动监听,滑动时,用透明度为0的 ...
- python单链表的基本操作思路
单链表: 1.定义链表 class ListNode: # 定义节点 def __init__(self, x): self.val = x # 节点当前值 self.next = None # 指向 ...
- c++程序—变量
#include<iostream> using namespace std; int main() { int a = 10; cout << "a=" ...
- Python笔记_第五篇_Python数据分析基础教程_文件的读写
1. 读写文件(基本) savetxt.loadtxt i2 = np.eye(2) print(i2) np.savetxt(r"C:\Users\Thomas\Desktop\eye.t ...
- python进阶(三)~~~装饰器和闭包
一.闭包 满足条件: 1. 函数内嵌套一个函数: 2.外层函数的返回值是内层函数的函数名: 3.内层嵌套函数对外部作用域有一个非全局变量的引用: def func(): print("=== ...
- 使用Python绘制新型冠状肺炎全国增长趋势图
截至1月28日24时,国家卫生健康委收到31个省(区.市)累计报告确诊病例5974例,现有重症病例1239例,累计死亡病例132例,累计治愈出院103例.现有疑似病例9239例.目前累计追踪到密切接触 ...
- 委托、Action、Func使用
参考 using System; using System.Collections.Generic; using System.Linq; using System.Text; using Syste ...