d97148febeedd7ea665c169ed3646fefee2191eb.svn-base 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. <template>
  2. <el-dialog append-to-body :visible.sync="imgVisible" @close="closeWindow" title="图片编辑" width="60%">
  3. <div class="container">
  4. <el-row>
  5. <el-col :xs="24" :md="24" :style="{height: '500px'}">
  6. <vue-cropper
  7. ref="cropper"
  8. :img="options.img"
  9. :info="true"
  10. :autoCrop="options.autoCrop"
  11. :autoCropWidth="options.autoCropWidth"
  12. :autoCropHeight="options.autoCropHeight"
  13. :fixedBox="options.fixedBox"
  14. :outputType="options.outputType"
  15. @realTime="realTime"
  16. />
  17. </el-col>
  18. <!-- <el-col :xs="24" :md="12" :style="{height: '600px'}">-->
  19. <!-- <div class="avatar-upload-preview">-->
  20. <!-- <img :src="previews.url" :style="previews.img" />-->
  21. <!-- </div>-->
  22. <!-- </el-col>-->
  23. </el-row>
  24. <br />
  25. <el-row>
  26. <el-col :lg="2" :sm="3" :xs="3">
  27. <el-upload action="#" :http-request="requestUpload" :show-file-list="false" :before-upload="beforeUpload">
  28. <el-button size="small">
  29. 选择
  30. <i class="el-icon-upload el-icon--right"></i>
  31. </el-button>
  32. </el-upload>
  33. </el-col>
  34. <el-col :lg="{span: 1, offset: 2}" :sm="1" :xs="1">
  35. <el-button size="small" @click="showCut()">裁剪</el-button>
  36. </el-col>
  37. <el-col :lg="{span: 1, offset: 2}" :sm="1" :xs="1">
  38. <el-button icon="el-icon-plus" size="small" @click="changeScale(1)"></el-button>
  39. </el-col>
  40. <el-col :lg="{span: 1, offset: 1}" :sm="1" :xs="1">
  41. <el-button icon="el-icon-minus" size="small" @click="changeScale(-1)"></el-button>
  42. </el-col>
  43. <el-col :lg="{span: 1, offset: 1}" :sm="1" :xs="1">
  44. <el-button icon="el-icon-refresh-left" size="small" @click="rotateLeft()"></el-button>
  45. </el-col>
  46. <el-col :lg="{span: 1, offset: 1}" :sm="1" :xs="1">
  47. <el-button icon="el-icon-refresh-right" size="small" @click="rotateRight()"></el-button>
  48. </el-col>
  49. <el-col :lg="{span: 2, offset: 6}" :sm="2" :xs="2" style="display: flex;align-items: center">
  50. <el-button type="primary" size="small" @click="uploadImg()">提 交</el-button>
  51. <el-button size="small" @click="refresh()">重置</el-button>
  52. </el-col>
  53. </el-row>
  54. </div>
  55. </el-dialog>
  56. </template>
  57. <script>
  58. import { VueCropper } from "vue-cropper";
  59. export default {
  60. name: 'CropImage',
  61. components:{VueCropper},
  62. props: {
  63. picUrl:{
  64. type: String,
  65. default: '',
  66. }
  67. },
  68. data () {
  69. return {
  70. imgVisible:true,
  71. options: {
  72. img: "", //裁剪图片的地址
  73. autoCrop: false, // 是否默认生成截图框
  74. //autoCropWidth: 200, // 默认生成截图框宽度
  75. //autoCropHeight: 200, // 默认生成截图框高度
  76. fixedBox: false, // 固定截图框大小 不允许改变
  77. outputType:"png" // 默认生成截图为PNG格式
  78. },
  79. previews: {},
  80. };
  81. },
  82. watch:{
  83. },
  84. mounted () {
  85. this.$nextTick(() => {
  86. this.options.img = this.picUrl;
  87. });
  88. },
  89. methods: {
  90. // 上传图片
  91. uploadImg() {
  92. this.$refs.cropper.getCropBlob(data => {
  93. const myFile = new File([data], 'zyz.png');
  94. let formData = new FormData();
  95. formData.append("file", myFile);
  96. this.$emit('editPicData',formData);
  97. });
  98. },
  99. cancelCut(){
  100. this.$nextTick(() => {
  101. this.options.autoCrop = false;
  102. })
  103. },
  104. showCut(){
  105. this.$nextTick(() => {
  106. this.options.autoCrop = true;
  107. })
  108. },
  109. // 刷新组件
  110. refresh() {
  111. this.$refs.cropper.refresh();
  112. },
  113. // 覆盖默认的上传行为
  114. requestUpload() {
  115. },
  116. // 向左旋转
  117. rotateLeft() {
  118. this.$refs.cropper.rotateLeft();
  119. },
  120. // 向右旋转
  121. rotateRight() {
  122. this.$refs.cropper.rotateRight();
  123. },
  124. // 图片缩放
  125. changeScale(num) {
  126. num = num || 1;
  127. this.$refs.cropper.changeScale(num);
  128. },
  129. realTime(data) {
  130. this.previews = data;
  131. },
  132. // 上传预处理
  133. beforeUpload(file) {
  134. if (file.type.indexOf("image/") == -1) {
  135. this.$message.warning("文件格式错误,请上传图片类型,如:JPG,PNG后缀的文件。");
  136. } else {
  137. const reader = new FileReader();
  138. reader.readAsDataURL(file);
  139. reader.onload = () => {
  140. this.options.img = reader.result;
  141. };
  142. }
  143. },
  144. // 打开弹出层结束时的回调
  145. closeWindow() {
  146. this.$emit("cancelHandler");
  147. }
  148. },
  149. };
  150. </script>
  151. <style lang="css" scoped>
  152. .container {
  153. height: 600px;
  154. text-align: center;
  155. }
  156. /deep/ .el-icon-refresh-right{
  157. font-size: 12px !important;
  158. }
  159. </style>