vue2.0 使用vue-cropper裁剪图片并上传服务器

插件地址 : https://github.com/xyxiao001/vue-cropper

项目截图:

1.npm install vue-cropper 安装

2.main.js使用

import VueCropper from 'vue-cropper' 

Vue.use(VueCropper)

3.选择图片

//选择图片
  	selectImage(){
	   	if (!window.plus) return;
	    let _this = this;
        //使用h5plus选择图片
	    plus.gallery.pick(
	        function (file) {
	        	_this.$store.commit('setCropperResult',file); //传值给裁剪页面
                _this.$router.push('/cropper');
	        } , function ( e ) {
    				console.log( "取消选择图片" );
    			}, {filter:"image"}
    	);
  	},

4.cropper页面裁剪 (使用了keep-alive页面缓存的记得在"watch"中监听路由变化并改变option.img的值)

<template>
    <div>
    	<div class="rj-header">
			<mu-appbar :z-depth="0" title="修剪图片">
		      <mu-button flat @click="cancle" color="primary" slot="left">
		         	取消
		      </mu-button>
			  <mu-button flat  @click="confirm" color="primary" slot="right" >确定</mu-button>
		    </mu-appbar>
		</div>
        <div class="cropper-content">
            <div class="cropper" :style="scrollHeight">
                <vueCropper
                    ref="cropper"
                    :img="option.img"
                    :outputSize="option.size"
                    :outputType="option.outputType"
                    :info="true"
                    :full="option.full"
                    :canScale="true"
                    :centerBox="true"
                    :canMove="option.canMove"
                    :canMoveBox="option.canMoveBox"
                    :autoCrop="option.autoCrop"
                    :autoCropWidth="option.autoCropWidth"
                    :autoCropHeight="option.autoCropHeight"
                    :fixedBox="option.fixedBox">
                </vueCropper>
            </div>
        </div>
    </div>
</template>
<script>
    export default {
        data() {
            return {
                crap: false,
                previews: {},
                option: {
                img: require('../assets/img/ALD.png'),
                size: 1,
                full: false,
                outputType: 'png',
                canMove: false,
                canMoveBox: true,
                autoCrop: true,
                autoCropWidth: 180,
                autoCropHeight: 180,
                downImg: '#'
            }
        }
    },
    mounted(){
    	this.option.img = this.$store.state.cropperResult;
    },
    computed:{
    	scrollHeight(){
    		return "height:" + ((window.screen.height)) + "px";
    	}
    },
    methods: {
        cancle () {
           this.$router.replace('/my')
        },
        confirm () {
			let _this = this;
            // 获取截图的blob数据
			this.$refs.cropper.getCropBlob((data) => {
			  // do something
				let param = new FormData();
				param.append('file', data, 'image.png');
				let ajaxLoading = _this.$layer.open({
					type: 2
				})
				_this.$http.post(_this.$lib.url + 'space/file', param).then(res => {
					console.log(res)
					if(res.status == 0) {
							let data = {
								avatar:res.data.url
							}
							_this.$http.post(_this.$lib.url + 'user/info', _this.$qs.stringify(data)).then(res => {
								_this.$layer.close(ajaxLoading);
								if(res.status == 0){
									_this.$layer.open({
										content: '上传成功',
										skin: 'msg',
										time: 2
									});
									_this.$router.replace('/my');
								}
							}).catch(err => {})
					} else {
						_this.$layer.open({
							content: res.msg,
							skin: 'msg',
							time: 2 //2秒后自动关闭
						});
					}
				}).catch(err => {})
			})
        },
    },
}
</script>
<style lang="css">
.cropper-content{
  position: fixed;
  z-index: 199;
  width: 100%;
 height: 100%;
}
 .cropper{
    width: 100%;
  }
</style>

 

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值