cropper.vue 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <template>
  2. <el-dialog :visible.sync="dialogVisible" width="600px" class="cropper-dialog" center title="图片剪裁">
  3. <div class="cropper-container">
  4. <div class="cropper">
  5. <vue-cropper ref="cropper"
  6. :img="cropperImg"
  7. :output-size="option.size"
  8. :output-type="option.outputType"
  9. :info="true"
  10. :full="option.full"
  11. :can-move="option.canMove"
  12. :can-move-box="option.canMoveBox"
  13. :fixed-box="option.fixedBox"
  14. :original="option.original"
  15. :auto-crop="option.autoCrop"
  16. :auto-crop-width="option.autoCropWidth"
  17. :auto-crop-height="option.autoCropHeight"
  18. :center-box="option.centerBox"
  19. :high="option.high"
  20. :info-true="option.infoTrue"
  21. @realTime="realTime"
  22. :enlarge="option.enlarge"
  23. :fixed="option.fixed"
  24. :fixed-number="option.fixedNumber"/>
  25. </div>
  26. <!-- 预览 -->
  27. <div class="preview-container">
  28. <div class="preview" :style="previews.div">
  29. <el-image v-if="previews.url" :src="previews.url" :style="previews.img" fit="cover"></el-image>
  30. </div>
  31. </div>
  32. </div>
  33. <div slot="footer">
  34. <el-button @click="onClose">取 消</el-button>
  35. <el-button type="primary" @click="saveImg" style="margin-left: 50px;">确 定</el-button>
  36. </div>
  37. </el-dialog>
  38. </template>
  39. <script>
  40. import {VueCropper} from 'vue-cropper';
  41. export default {
  42. name: 'Cropper',
  43. components: {
  44. VueCropper
  45. },
  46. props: {
  47. visible: {
  48. type: Boolean,
  49. default: false
  50. },
  51. imgType: {
  52. type: String,
  53. default: 'blob'
  54. },
  55. cropperImg: {
  56. type: String,
  57. default: ''
  58. },
  59. options: {
  60. type: Object,
  61. default: ()=>{
  62. return {}
  63. }
  64. }
  65. },
  66. computed: {
  67. dialogVisible: {
  68. get() {
  69. return this.visible
  70. },
  71. set(val) {
  72. this.$emit('update:visible', val)
  73. }
  74. },
  75. option() {
  76. return {...this.defaultOption, ...this.options};
  77. }
  78. },
  79. data() {
  80. return {
  81. previews: {},
  82. defaultOption: {
  83. img: '', // 裁剪图片的地址
  84. size: 1, // 裁剪生成图片的质量
  85. full: false, // 是否输出原图比例的截图 默认false
  86. outputType: 'png', // 裁剪生成图片的格式 默认jpg
  87. canMove: false, // 上传图片是否可以移动
  88. fixedBox: false, // 固定截图框大小 不允许改变
  89. original: false, // 上传图片按照原始比例渲染
  90. canMoveBox: true, // 截图框能否拖动
  91. autoCrop: true, // 是否默认生成截图框
  92. // 只有自动截图开启 宽度高度才生效
  93. autoCropWidth: 200, // 默认生成截图框宽度
  94. autoCropHeight: 200, // 默认生成截图框高度
  95. centerBox: true, // 截图框是否被限制在图片里面
  96. high: false, // 是否按照设备的dpr 输出等比例图片
  97. enlarge: 1, // 图片根据截图框输出比例倍数
  98. mode: 'contain', // 图片默认渲染方式
  99. maxImgSize: 2000, // 限制图片最大宽度和高度
  100. limitMinSize: [100, 100], // 更新裁剪框最小属性
  101. infoTrue: false, // true 为展示真实输出图片宽高 false 展示看到的截图框宽高
  102. fixed: true, // 是否开启截图框宽高固定比例 (默认:true)
  103. fixedNumber: [1, 1] // 截图框的宽高比例
  104. }
  105. };
  106. },
  107. methods: {
  108. // 裁剪时触发的方法,用于实时预览
  109. realTime(data) {
  110. this.previews = data;
  111. },
  112. // 取消关闭弹框
  113. onClose() {
  114. this.$set(this, "dialogVisible", false);
  115. },
  116. // 获取裁剪之后的图片,默认blob,也可以获取base64的图片
  117. saveImg() {
  118. if (this.imgType === 'blob') {
  119. this.$refs.cropper.getCropBlob(blob => {
  120. this.$emit('crop', blob);
  121. });
  122. } else {
  123. this.$refs.cropper.getCropData(data => {
  124. this.$emit('crop', data);
  125. });
  126. }
  127. }
  128. }
  129. };
  130. </script>
  131. <style lang="scss" scoped>
  132. .cropper-dialog {
  133. .cropper-container {
  134. display: flex;
  135. justify-content: space-between;
  136. .cropper {
  137. height: 240px;
  138. width: 240px;
  139. }
  140. .preview-container {
  141. width: 240px;
  142. height: 240px;
  143. display: flex;
  144. align-items: center;
  145. justify-content: center;
  146. background: #ededed;
  147. .preview {
  148. overflow: hidden;
  149. }
  150. .el-button {
  151. margin-top: 20px;
  152. }
  153. }
  154. }
  155. }
  156. </style>