elementui结合vue-cropper实现裁剪和上传

旧城等待, 2022-11-24 11:21 439阅读 0赞

功能描述:上传图片支持裁剪图片

1:先安装vue-cropper

npm install vue-cropper —save

2:在main.js里加入:

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

3:cropper组件

  1. <!-- vueCropper 剪裁图片实现-->
  2. <el-dialog title="图片剪裁" :visible.sync="cropperVisible" width="400px" >
  3. <div class="cropper-content">
  4. <div style="width:100%;height:300px">
  5. <vue-cropper
  6. ref="cropper"
  7. :img="option.img"
  8. :output-size="option.size"
  9. :output-type="option.outputType"
  10. :info="true"
  11. :full="option.full"
  12. :fixed="option.fixed"
  13. :fixed-number="option.fixedNumber"
  14. :can-move="option.canMove"
  15. :can-move-box="option.canMoveBox"
  16. :fixed-box="option.fixedBox"
  17. :original="option.original"
  18. :auto-crop="option.autoCrop"
  19. :auto-crop-width="option.autoCropWidth"
  20. :auto-crop-height="option.autoCropHeight"
  21. :center-box="option.centerBox"
  22. @real-time="realTime"
  23. :high="option.high"
  24. @img-load="imgLoad"
  25. mode="cover"
  26. :max-img-size="option.max"
  27. @crop-moving="cropMoving"
  28. ></vue-cropper>
  29. </div>
  30. </div>
  31. <div slot="footer" class="dialog-footer">
  32. <el-button @click="cropperVisible = false">取 消</el-button>
  33. <el-button type="primary" @click="cropperFinish" :loading="loading">确认
  34. </el-button>
  35. </div>
  36. </el-dialog>
  37. <el-dialog title="申请售后"
  38. :visible.sync="saleAddVisible"
  39. width="30%"
  40. @close="saleAfterDialogClosed">
  41. <el-form
  42. ref="addsaleAfterFormRef"
  43. :model="saleAfterForm"
  44. label-width="120px"
  45. :rules="saleAfterFormRules">
  46. <el-form-item label="故障图片 : " prop="img">
  47. <el-upload
  48. class="avatar-uploader"
  49. action
  50. :auto-upload="false"
  51. :show-file-list="false"
  52. :on-change="changeUpload">
  53. <img v-if="saleAfterForm.imageUrl" :src="saleAfterForm.imageUrl"
  54. class="avatar" />
  55. <i v-else class="el-icon-plus avatar-uploader-icon"></i>
  56. </el-upload>
  57. </el-form-item>
  58. </el-form>
  59. <span slot="footer" class="dialog-footer">
  60. <el-button @click="saleAddVisible= false">取 消</el-button>
  61. <el-button type="primary" @click="addSaleAfterSubm">确认</el-button>
  62. </span>
  63. </el-dialog>
  64. //上传图片
  65. changeUpload(file, fileList) {
  66. if (!/\.(gif|jpg|jpeg|png|bmp|GIF|JPG|PNG)$/.test(file.raw.name)) {
  67. alert('图片类型必须是.gif,jpeg,jpg,png,bmp中的一种')
  68. return false
  69. }
  70. const isLt5M = file.size / 1024 / 1024 < 5
  71. if (!isLt5M) {
  72. this.$message.error('上传文件大小不能超过 5MB!')
  73. return false
  74. }
  75. //URL.createObjectURL的参数只能是blob或者file类型
  76. //第一种方法用FileReader,URL.createObjectURL接收blob类型
  77. let reader = new FileReader()
  78. reader.onload = (e) => {
  79. let data
  80. if (typeof e.target.result === 'object') {
  81. // 把Array Buffer转化为blob 如果是base64不需要
  82. data = window.URL.createObjectURL(new Blob([e.target.result]))
  83. } else {
  84. data = e.target.result
  85. }
  86. this.option.img = data
  87. }
  88. // 转化为base64
  89. this.cropperVisible = true
  90. reader.readAsArrayBuffer(file.raw)
  91. //第二种方法,URL.createObjectURL接收file类型
  92. this.$nextTick(() => {
  93. this.option.img = URL.createObjectURL(file.raw)
  94. this.cropperVisible = true
  95. })
  96. },
  97. //点击剪裁弹框的确定按钮
  98. cropperFinish() {
  99. // 获取截图的base64 数据
  100. this.$refs.cropper.getCropBlob((data) => {
  101. let form = new FormData()
  102. let file = this.blobToFile(data, 'filename.jpg')
  103. form.append('img_file', file)
  104. this.$axios({
  105. method: 'POST',
  106. url: '/api/api_gateway?method=base.bases.base_photo',
  107. data: form
  108. }).then((res) => {})
  109. this.saleAfterForm.imageUrl = data
  110. this.cropperVisible = false
  111. })
  112. },
  113. //转成blob
  114. blobToFile(Blob, fileName) { //兼容IE
  115. Blob.lastModifiedDate = new Date()
  116. Blob.name = fileName
  117. return Blob
  118. },
  119. imgLoad(msg) {
  120. console.log('imgLoad')
  121. console.log(msg)
  122. },
  123. realTime(data) {
  124. console.log(data)
  125. },
  126. cropMoving(data) {
  127. console.log(99899)
  128. console.log(data, '截图框当前坐标')
  129. },
  1. 实现效果:

剪裁 :watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3UwMTEyMDA1NjI_size_16_color_FFFFFF_t_70 剪裁后:20200806100410464.png

注意:我在用vue-cropper的时候遇到个坑,就是vue-cropper不能和mock插件一起使用,不然后报错,后来把mock插件从项目中去掉,就不报错了,报错信息为“Failed to execute ‘readAsArrayBuffer’ on ‘FileReader’: parameter 1 is not of type ‘Blob’.”

希望大家能避免~~~

发表评论

表情:
评论列表 (有 0 条评论,439人围观)

还没有评论,来说两句吧...

相关阅读