1.0.1 • Published 3 years ago

vue-pic-clip-rotate v1.0.1

Weekly downloads
-
License
-
Repository
-
Last release
3 years ago

vue-pic-clip-rotate

效果图

Project setup

yarn install

Compiles and hot-reloads for development

yarn serve

Compiles and minifies for production

yarn build

Lints and fixes files

yarn lint

配置参数

名称功能默认值可选值
img默认图片地址url地址base64blob
accept上传图片类型'image/png, image/jpeg, image/jpg, image/gif'jpegpnggif等
autoClip是否生成截图框falseturefalse
autoClipWidth截图框的宽度容器宽度80%0~容器宽度
autoClipHeight截图框的高度与宽度相等0~容器宽度
canMove图片能否拖动truetruefasle
canMoveBox截图框能否拖动tureturefalse
dataUrlType输出图片数据类型blobbase64blob
fixed截图框是否开启固定宽高比falsetruefalse(若设置的宽高比例与宽高比不匹配,则按照宽高比计算高度)
fixedNumber截图框宽高比1,1宽度,高度
fixedBox固定截图框大小falsetruefalse
isOriginalImg是否上传原图falsetruefalse(启用裁剪时无效)
maxWidth生成图片的最大宽度6000~max(启用裁剪或上传原图时最大宽度无效)
maxHeight生成图片的最大高度6000~max(同上)
outputSize输出图片压缩比10.1-1
outputType生成图片的格式jpegjpegpngwebp
theme样式风格rectrectcircle
finish完成操作事件回调函数

<template>
  <div class="upload">
    <div class="main">
      <div class="avatar">
        <pic-clip
          :dataUrlType="option.dataUrlType"
          :accept="option.accept"
          :autoClip="option.autoClip"
          :autoClipWidth="option.autoClipWidth"
          :autoClipHeight="option.autoClipHeight"
          :fixed="option.fixed"
          :outputSize="option.outputSize"
          :theme="option.theme"
          @finish="finish"
        >上传头像</pic-clip>
      </div>
    </div>
  </div>
</template>
<script>
export default {
  name: 'app',
  data () {
    return {
      option: {
        accept: 'image/png, image/jpeg, image/jpg, image/gif',
        dataUrlType: 'blob', // 数据类型,可选值: blob base64
        outputSize: 0.7,
        autoClip: true,
        autoClipWidth: 300,
        autoClipHeight: 300,
        canMoveBox: false,
        fixed: false,
        fixedNumber: [1, 1],
        theme: 'rect'
      }
    }
  },
  methods: {
    finish (filename, url) {
      console.log(filename, url)
      // console.log(this.dataURLtoFile(url, filename)) // base64
      // console.log(this.dataBlobtoFile(url, filename))  // blob
    },
    //将base64转换为文件
    dataURLtoFile(dataurl, filename) {
      var arr = dataurl.split(','), mime = arr[0].match(/:(.*?);/)[1],
        bstr = atob(arr[1]), n = bstr.length, u8arr = new Uint8Array(n);
      while (n--) {
        u8arr[n] = bstr.charCodeAt(n);
      }
      return new File([u8arr], filename, { type: mime });
    },
    dataBlobtoFile(dataurl, filename) {
      let files = new window.File(
        [dataurl],
        filename,
        { type: `image/${filename.split('.')[1]}` }
      );
      return files
    }
  }
}
</script>
<style lang="css">
  i {
  margin: 0;
  padding: 0;
  border: 0;
  font-weight: normal;
  list-style: none;
  font-style: normal;
  font-size: 14px;
  text-decoration: none;
  -webkit-box-sizing: border-box;
  box-sizing: border-box;
  -webkit-tap-highlight-color: transparent;
  -webkit-font-smoothing: antialiased;
}

.avatar {
  cursor: pointer;
  width: 160px;
  height: 160px;
}
</style>