vue+图片裁剪vue-cropper以及api

前言:

        因为项目需要,需要实现一个上传完图片,对图片进行二次处理的需求,就使用了vue-cropper来实现这个功能,总的来说还是感觉非常不错的软件,这里分享下我的使用方法,以及vue-cropperapi(见最下)

实现效果:

vue+图片裁剪vue-cropper以及api_第1张图片vue+图片裁剪vue-cropper以及api_第2张图片

实现步骤:

步骤一:项目中安装 vue-cropper

npm install vue-cropper
   
   
   
   

步骤二:在main.js中注册(经测试,单独页面注册可能会出不来)


   
   
   
   
  1. import VueCropper from 'vue-cropper'
  2. Vue.use(VueCropper)

步骤三:在页面使用,这里直接提供封装好的组件 vue-cropper.vue(源代码在下面)

1、template部分

vue+图片裁剪vue-cropper以及api_第3张图片

2、js部分

vue+图片裁剪vue-cropper以及api_第4张图片

vue+图片裁剪vue-cropper以及api_第5张图片

vue+图片裁剪vue-cropper以及api_第6张图片

源代码:


   
   
   
   
  1. <template>
  2. <div class="cropper-component">
  3. <div class="btn-box">
  4. <label class="btn" for="uploads">选择图片 label>
  5. <input type="file" id="uploads" :value="imgFile"
  6. style= "position:absolute; clip:rect(0 0 0 0);width: 1px;"
  7. accept= "image/png, image/jpeg, image/gif, image/jpg"
  8. @ change= "uploadImg($event,'blob', 1)">
  9. <Button type="primary" icon="ios-add" title="放大" @click="changeScale(1)"/>
  10. <Button type="primary" icon="ios-remove" title="缩小" @click="changeScale(-1)"/>
  11. <Button type="primary" title="左旋转" @click="rotateLeft"> <span style="font-weight: 500;">span> Button>
  12. <Button type="primary" title="右旋转" @click="rotateRight"> <span style="font-weight: 500;">span> Button>
  13. div>
  14. <div class="show_box">
  15. <div class="cropper">
  16. <vueCropper
  17. ref= "cropper"
  18. :img= "option.img"
  19. :outputSize= "option.size"
  20. :outputType= "option.outputType"
  21. :info= "option.info"
  22. :full= "option.full"
  23. :canMove= "option.canMove"
  24. :canMoveBox= "option.canMoveBox"
  25. :original= "option.original"
  26. :autoCrop= "option.autoCrop"
  27. :autoCropWidth= "option.autoCropWidth"
  28. :autoCropHeight= "option.autoCropHeight"
  29. :fixedBox= "option.fixedBox"
  30. @ realTime= "realTime"
  31. @ imgLoad= "imgLoad"
  32. > vueCropper>
  33. div>
  34. <div class="preview-box" v-if="previews.url">
  35. <div>预览: div>
  36. <div :style="previews.div" class="preview">
  37. <img :src="previews.url" :style="previews.img"/>
  38. div>
  39. div>
  40. div>
  41. div>
  42. template>
  43. <script>
  44. export default {
  45. name: 'cropper',
  46. data() {
  47. return {
  48. //剪切图片上传
  49. crap: false,
  50. previews: {},
  51. option: {
  52. img: '', // 裁剪图片的地址
  53. info: true, // 裁剪框的大小信息
  54. outputSize: 1, // 剪切后的图片质量(0.1-1)
  55. full: true, // 输出原图比例截图 props名full
  56. outputType: 'jpg', // 裁剪生成额图片的格式
  57. canMove: true, // 能否拖动图片
  58. original: false, // 上传图片是否显示原始宽高
  59. canMoveBox: true, // 能否拖动截图框
  60. autoCrop: true, // 是否默认生成截图框
  61. autoCropWidth: 150,
  62. autoCropHeight: 150,
  63. fixedBox: true // 截图框固定大小
  64. },
  65. fileName: '', // 本机文件地址
  66. downImg: '#',
  67. imgFile: '',
  68. uploadImgRelaPath: '', // 上传后的图片的地址(不带服务器域名)
  69. }
  70. },
  71. methods: {
  72. // 放大/缩小
  73. changeScale(num) {
  74. num = num || 1;
  75. this.$refs.cropper.changeScale(num);
  76. },
  77. // 坐旋转
  78. rotateLeft() {
  79. this.$refs.cropper.rotateLeft();
  80. },
  81. // 右旋转
  82. rotateRight() {
  83. this.$refs.cropper.rotateRight();
  84. },
  85. // 上传图片(点击上传按钮)
  86. finish(type) {
  87. let formData = new FormData();
  88. // 输出
  89. if (type === 'blob') {
  90. this.$refs.cropper.getCropBlob( (data) => {
  91. let img = window.URL.createObjectURL(data);
  92. formData.append( 'images', data);
  93. this.$emit( 'postFile',formData);
  94. })
  95. } else {
  96. this.$refs.cropper.getCropData( (data) => {
  97. formData.append( 'images', data);
  98. this.$emit( 'postFile',formData);
  99. })
  100. }
  101. },
  102. // 实时预览函数
  103. realTime(data) {
  104. this.previews = data;
  105. },
  106. // 下载图片
  107. down(type) {
  108. var aLink = document.createElement( 'a');
  109. aLink.download = 'author-img'; //文件名
  110. if (type === 'blob') {
  111. // 获取截图的blob数据
  112. this.$refs.cropper.getCropBlob( (data) => {
  113. this.downImg = window.URL.createObjectURL(data); //生成blob格式图片路径
  114. aLink.href = window.URL.createObjectURL(data);
  115. aLink.click();
  116. })
  117. } else {
  118. // 获取截图的base64 数据
  119. this.$refs.cropper.getCropData( (data) => {
  120. this.downImg = data;
  121. aLink.href = data;
  122. })
  123. }
  124. },
  125. // 选择本地图片
  126. uploadImg(e, type, num) { //num代表第几个
  127. // 上传图片
  128. var file = e.target.files[ 0];
  129. this.fileName = file.name;
  130. if (! /\.(jpg)$/.test(e.target.value)) {
  131. this.$Message.info( '证件照图片必须是jpg格式');
  132. return false;
  133. }
  134. let reader = new FileReader();
  135. reader.onload = (e) => {
  136. let data = ''; //生成图片地址
  137. if ( typeof e.target.result === 'object') {
  138. if(type == 'blob'){
  139. // 把Array Buffer转化为blob 如果是base64不需要
  140. data = window.URL.createObjectURL( new Blob([e.target.result]));
  141. }
  142. }
  143. else {
  144. data = e.target.result;
  145. }
  146. if (num === 1) {
  147. this.option.img = data;
  148. }
  149. }
  150. if(type == 'blob'){
  151. // 转化为blob
  152. reader.readAsArrayBuffer(file);
  153. } else{
  154. // 转化为base64
  155. reader.readAsDataURL(file);
  156. }
  157. },
  158. //图片加载的回调 imgLoad 返回结果success, error
  159. imgLoad (msg) {
  160. console.log( 'imgLoad')
  161. console.log(msg)
  162. },
  163. //刷新-清除截图-目前尚未用到
  164. refeshImg(type){
  165. if(type == 'start'){
  166. this.$refs.cropper.startCrop() //开始截图
  167. } else if(type == 'end'){
  168. this.$refs.cropper.stopCrop() //停止截图
  169. } else if(type == 'clear'){
  170. this.$refs.cropper.clearCrop() //清除截图
  171. }
  172. }
  173. }
  174. }
  175. script>
  176. <style lang="less">
  177. .cropper-component {
  178. width: 100%;
  179. margin: 0 auto;
  180. position: relative;
  181. //工具箱部分
  182. .btn-box {
  183. margin: 20px 0;
  184. .btn {
  185. padding:0 10px;
  186. height:32px;
  187. line-height:32px;
  188. text-align: center;
  189. border-radius: 4px;
  190. background-color: #0d8b8e ;
  191. color: #fff;
  192. cursor: pointer;
  193. display: inline-block;
  194. vertical-align: bottom;
  195. }
  196. }
  197. //展示内容部分
  198. .show_box{
  199. display: flex;
  200. justify-content: space-between;
  201. // 展示选中图片
  202. .cropper {
  203. width: 260px;
  204. height: 260px;
  205. }
  206. // 展示缩略图
  207. .preview-box {
  208. top: 60px;
  209. right: 10px;
  210. .preview {
  211. width: 150px;
  212. height: 150px;
  213. // border-radius: 50%;//这里预览是否需要变成圆的
  214. border: 1px solid #ccc;
  215. background-color: #ccc;
  216. margin: 5px;
  217. overflow: hidden;
  218. }
  219. }
  220. }
  221. }
  222. style>

API文档:

vue+图片裁剪vue-cropper以及api_第7张图片

vue+图片裁剪vue-cropper以及api_第8张图片

vue+图片裁剪vue-cropper以及api_第9张图片

这里内置方法的话,我是可以提供可供拷贝的代码:


   
   
   
   
  1. 内置方法 通过 this.$refs.cropper 调用
  2. this.$refs.cropper.startCrop() 开始截图
  3. this.$refs.cropper.stopCrop() 停止截图
  4. this.$refs.cropper.clearCrop() 清除截图
  5. this.$refs.cropper.changeScale() 修改图片大小 正数为变大 负数变小
  6. this.$refs.cropper.getImgAxis() 获取图片基于容器的坐标点
  7. this.$refs.cropper.getCropAxis() 获取截图框基于容器的坐标点
  8. this.$refs.cropper.goAutoCrop 自动生成截图框函数
  9. this.$refs.cropper.rotateRight() 向右边旋转 90
  10. this.$refs.cropper.rotateLeft() 向左边旋转 90
  11. 图片加载的回调 imgLoad 返回结果success, error
  12. 获取截图信息
  13. this.$refs.cropper.cropW 截图框宽度
  14. this.$refs.cropper.cropH 截图框高度

完整api入口

到这里就结束了,有兴趣的朋友可以留言一起交流哈

发布了132 篇原创文章 · 获赞 52 · 访问量 14万+

你可能感兴趣的:(vue)