1.1.1 • Published 5 years ago

wepy-image-cropper v1.1.1

Weekly downloads
3
License
MIT
Repository
github
Last release
5 years ago

基于 image-cropper 开发的wepy版本微信小程序截图插件

说明

此插件基于image-cropper微信原生插件开发, 可能会增加部分功能,具体请查阅以下的插件文档

原生插件传送门: 微信原生插件 image-cropper 地址

使用到该插件的案例: 同游分账小程序

一款高性能的小程序图片裁剪插件,支持旋转、设置尺寸

1.功能强大,请看下面demo。
2.性能超高超流畅,大图毫无卡顿感。
3.可以设置导出图片尺寸。
4.组件化,使用非常简单。
5.点击中间窗口实时查看裁剪结果。

使用

安装组件

npm install wepy-image-cropper --save

引入组件

<template>
  <view wx:if="{{show_cropper}}" style="position: fixed; top: 0; left: 0; right: 0; bottom: 0; background: black;">
      <wepy-image-cropper id="image-cropper" :limit_move="limit_move" :disable_rotate="disable_rotate" :disable_ratio="disable_ratio" :borderColor="borderColor" :width="width" :height="height" @load.user="cropperload" @imageload.user="loadimage"></wepy-image-cropper>
      <view style="height: 100rpx; box-shadow:0px -2rpx 0px 0px white; display: flex; flex-direction: row; align-items: center; justify-content: space-around; position: absolute; width: 100%; bottom: 0; z-index: 9999;">
        <view @tap="hide_cropper" style="font-size: 36rpx; color: white;">取消</view>
        <view @tap="finish_cut" style="font-size: 36rpx; color: white;">完成</view>
      </view>
    </view>
</template>

<script>
  import wepy from 'wepy'
  import WepyImageCropper from 'wepy-image-cropper'

  export default class Test extends wepy.page {
    config = {
      navigationBarTitleText: '',
    }
    components = {
      'wepy-image-cropper': WepyImageCropper
    }

    mixins = []

	cropper = null 
    data = {
     	src: '',
	    width: 250,//宽度
	    height: 250,//高度
	    limit_move: true,
	    disable_rotate: true,
	    borderColor: 'white',
	    disable_ratio: true,
	    show_cropper: true,
    }
    onLoad(options) {
    }
    onReady() {
      //开始裁剪
      this.src = 'https://raw.githubusercontent.com/1977474741/image-cropper/dev/image/code.jpg'
      wx.showLoading({
        title: '加载中'
      })
    }

    computed = {
    }

    methods = {
          cropperload(e) {
            console.log("cropper初始化完成:" + e);
            this.cropper = e.cropper
            this.cropper.$wxpage.imgReset()
            this.$apply()
          },
          loadimage(e){
            console.log("图片加载完成",e);
          },
          // 上传收款码
          upload(type) {
            let that = this
            this.type = type
            this.cropper.$wxpage.setWidth(this.width)
            this.cropper.$wxpage.setHeight(this.height)
            this.cropper.$wxpage.imgReset()
            this.cropper.$wxpage.setCutCenter();
    
            wx.chooseImage({
              count: 1,
              sizeType: ['original', 'compressed'],
              sourceType: ['album', 'camera'],
              success: function success(res) {
                var tempFilePaths = res.tempFilePaths[0];
                that.cropper.$wxpage.pushImg(tempFilePaths);
                that.show_cropper = true;
                that.$apply();
                wx.showLoading({
                  title: '加载中...'
                });
              }
            });
          },
          async finish_cut() {
            this.cropper.$wxpage.getImg(async (e) => {
              try {
                // 获取选择的图片
                let image = e.url
    
               	
              } catch (err) {
                console.log(err)
                wepy.showModal({
                  title: '提示',
                  content: '服务器错误,请联系管理员'
                })
              }
    
              this.show_cropper = false
              this.$apply()
            })
          },
        
        }

    events = {
    }
  }
</script>

参数说明(高亮属性表示对于上个版本有修改,请注意)

属性类型缺省值取值描述必填
imgSrcString无限制图片地址(如果是网络图片需配置安全域名)
borderColorString'white'无限制裁剪框的颜色
disable_rotateBooleanfalsetrue/false禁止用户旋转(为false时建议同时设置limit_move为false)
limit_moveBooleanfalsetrue/false限制图片移动范围(裁剪框始终在图片内)(为true时建议同时设置disable_rotate为true)
widthNumber200超过屏幕宽度自动转为屏幕宽度裁剪框宽度
heightNumber200超过屏幕高度自动转为屏幕高度裁剪框高度
max_widthNumber300裁剪框最大宽度裁剪框最大宽度
max_heightNumber300裁剪框最大高度裁剪框最大高度
min_widthNumber100裁剪框最小宽度裁剪框最小宽度
min_heightNumber100裁剪框最小高度裁剪框最小高度
disable_widthBooleanfalsetrue/false锁定裁剪框宽度
disable_heightBooleanfalsetrue/false锁定裁剪框高度
disable_ratioBooleanfalsetrue/false锁定裁剪框比例
export_scaleNumber3无限制输出图片的比例(相对于裁剪框尺寸)
qualityNumber10-1生成的图片质量
cut_topNumber居中始终在屏幕内裁剪框上边距
cut_leftNumber居中始终在屏幕内裁剪框左边距
img_widthNumber宽高都不设置,最小边填满裁剪框支持%(不加单位为px)(只设置宽度,高度自适应)图片宽度
img_heightNumber宽高都不设置,最小边填满裁剪框支持%(不加单位为px)(只设置高度,宽度自适应)图片高度
scaleNumber1无限制图片的缩放比
angleNumber0(limit_move=true时angle=n*90)图片的旋转角度
min_scaleNumber0.5无限制图片的最小缩放比
max_scaleNumber2无限制图片的最大缩放比
bindloadFunctionnull函数名称cropper初始化完成
bindimageloadFunctionnull函数名称图片加载完成,返回值Object{width,height,path,type等}
bindtapcutFunctionnull函数名称点击中间裁剪框,返回值Object{src,width,height}

函数说明

函数名参数返回值描述参数必填
upload调起wx上传图片接口并开始剪裁
pushImgsrc开始裁剪图片
getImgFunction(回调函数)Object{url,width,height}获取裁剪之后的图片(图片尺寸 = 图片宽高 * export_scale)
setCutXYX、Y设置裁剪框位置
setCutSizewidth、height设置裁剪框大小
setCutCenter设置裁剪框居中
setScalescale设置图片缩放比例(不受min_scale、max_scale影响)
setAngledeg设置图片旋转角度(带过渡效果)
setTransform{x,y,angle,scale,cutX,cutY}图片在原有基础上的变化(scale受min_scale、max_scale影响)根据需要传参
imgReset重置图片的角度、缩放、位置(可以在onloadImage回调里使用)
setWidthwidth设置图片宽度
setHeightheight设置图片高度

996.icu LICENSE

联系我们

1.1.1

5 years ago

1.1.0

6 years ago

1.0.1

6 years ago

1.0.0

6 years ago