Cropper.vue 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. <template>
  2. <div>
  3. <div>
  4. <button
  5. id="fileSelect"
  6. class="btn btn-primary"
  7. :readonly="!readonly"
  8. @click="selectFile"
  9. >
  10. {{ $t("lang.Cropper.selectPicture") }}
  11. </button>
  12. <input
  13. :id="'file1' + uuid"
  14. ref="fileInput1"
  15. autocomplete="off"
  16. type="file"
  17. class="form-control file-input"
  18. @change="onFileChanges"
  19. />
  20. <label for="attachment">
  21. <a
  22. role="button"
  23. class="btn btn-primary btn-sm"
  24. style="height: 34px"
  25. @click="clickButton"
  26. >
  27. <i class="fa fa-upload" />
  28. &nbsp; {{ $t("lang.Cropper.selectPictureUploadDirectly") }}
  29. </a>
  30. </label>
  31. <button
  32. id="button2"
  33. class="btn btn-success"
  34. type="button"
  35. @click="cropImage"
  36. >
  37. {{ $t("lang.Cropper.upload") }}
  38. </button>
  39. <input
  40. v-show="false"
  41. :id="'file' + uuid"
  42. ref="fileInput"
  43. autocomplete="off"
  44. type="file"
  45. class="form-control"
  46. :readonly="!readonly"
  47. @change="previewAfterUpload($event)"
  48. />
  49. </div>
  50. <div style="height: 5px" />
  51. <div :id="'modal-contain' + uuid">
  52. <div :id="'preview-image-div' + uuid">
  53. <img :id="'crop-image' + uuid" width="100%" />
  54. </div>
  55. </div>
  56. </div>
  57. </template>
  58. <script>
  59. import Common from '../common/Common.js';
  60. import { Notify, Uuid } from 'pc-component-v3';
  61. export default {
  62. components: {
  63. },
  64. props: {
  65. uuid: {
  66. type: String,
  67. default: null,
  68. },
  69. readonly: {
  70. type: Boolean,
  71. default: null,
  72. },
  73. },
  74. emits: ['fileUpload', 'uploadImage'],
  75. data: function () {
  76. return {
  77. croppable: false,
  78. croppedCanvas: '',
  79. roundedCanvas: '',
  80. dataURL: '',
  81. errorMessage: '',
  82. };
  83. },
  84. watch: {},
  85. mounted: function () {
  86. //判断浏览器是否支持FileReader接口
  87. if (typeof FileReader == 'undefined') {
  88. Notify.error('提示', '当前浏览器不支持FileReader接口', false);
  89. }
  90. },
  91. methods: {
  92. /**
  93. * 点击上传按钮事件
  94. * @return {[type]} [description]
  95. */
  96. clickButton: function (e) {
  97. var fileElem = document.getElementById('file1' + this.uuid);
  98. if (fileElem) {
  99. fileElem.click();
  100. }
  101. e.preventDefault();
  102. },
  103. onFileChanges: function (e) {
  104. var files = e.target.files || e.dataTransfer.files;
  105. if (!files.length) return;
  106. this.$emit('fileUpload', files[0]);
  107. },
  108. getRoundedCanvas: function (sourceCanvas) {
  109. var canvas = document.createElement('canvas');
  110. var context = canvas.getContext('2d');
  111. var width = sourceCanvas.width;
  112. var height = sourceCanvas.height;
  113. canvas.width = width;
  114. canvas.height = height;
  115. context.beginPath();
  116. context.arc(
  117. width / 2,
  118. height / 2,
  119. Math.min(width, height) / 2,
  120. 0,
  121. 2 * Math.PI,
  122. );
  123. context.strokeStyle = 'rgba(0,0,0,0)';
  124. context.stroke();
  125. context.clip();
  126. context.drawImage(sourceCanvas, 0, 0, width, height);
  127. return canvas;
  128. },
  129. /**
  130. * 圆形截图的方法,目前未调用,保留
  131. */
  132. roundImage: function () {
  133. if (this.croppable == false) {
  134. return;
  135. }
  136. this.croppedCanvas = this.image1.cropper('getCroppedCanvas');
  137. this.roundedCanvas = this.getRoundedCanvas(this.croppedCanvas);
  138. this.dataURL = this.roundedCanvas.toDataURL();
  139. },
  140. /**
  141. * 裁剪图片
  142. */
  143. cropImage: function () {
  144. if (this.croppable == false) {
  145. return;
  146. }
  147. this.croppedCanvas = this.image1.cropper('getCroppedCanvas');
  148. this.roundedCanvas = this.getRoundedCanvas(this.croppedCanvas);
  149. this.dataURL = this.croppedCanvas.toDataURL();
  150. this.$emit('uploadImage', this.dataURL);
  151. },
  152. /**
  153. * 创建图片裁剪控件
  154. */
  155. createCropper: function () {
  156. var _self = this;
  157. this.image1 = $('#crop-image' + _self.uuid);
  158. this.image1.cropper({
  159. viewMode: 1,
  160. ready: function () {
  161. _self.croppable = true;
  162. },
  163. });
  164. },
  165. /**
  166. * 选择了文件
  167. */
  168. selectFile: function (e) {
  169. var fileElem = document.getElementById('file' + this.uuid);
  170. if (fileElem) {
  171. fileElem.click();
  172. }
  173. e.preventDefault(); // prevent navigation to "#"
  174. },
  175. /**
  176. * 选择图片后,马上预览
  177. */
  178. previewAfterUpload: function (e) {
  179. var _self = this;
  180. var elementStr =
  181. '<div id=\'preview-image-div' +
  182. _self.uuid +
  183. '\'><img id=\'crop-image' +
  184. _self.uuid +
  185. '\'/></div>';
  186. $('#preview-image-div' + _self.uuid).remove();
  187. $('#modal-contain' + _self.uuid).append(elementStr);
  188. var files = e.target.files || e.dataTransfer.files;
  189. if (!files.length) {
  190. return;
  191. }
  192. var file = files[files.length - 1];
  193. console.log('file.size = ' + file.size); //file.size 单位为byte
  194. if (file.size > 5000 * 1024) {
  195. Notify.error('错误', '图片大小超过限制,最大5M', true);
  196. e.target.value = '';
  197. return;
  198. }
  199. var reader = new FileReader();
  200. //读取文件过程方法
  201. reader.onloadstart = function (e) {
  202. console.log('开始读取....');
  203. };
  204. reader.onprogress = function (e) {
  205. console.log('正在读取中....');
  206. };
  207. reader.onabort = function (e) {
  208. console.log('中断读取....');
  209. };
  210. reader.onerror = function (e) {
  211. console.log('读取异常....');
  212. };
  213. reader.onload = function (e) {
  214. console.log('成功读取....');
  215. var img = document.getElementById('crop-image' + _self.uuid);
  216. img.src = e.target.result;
  217. _self.createCropper();
  218. _self.$refs.fileInput.value = '';
  219. };
  220. reader.readAsDataURL(file);
  221. },
  222. },
  223. };
  224. </script>
  225. <style scoped>
  226. .input-group {
  227. width: 100%;
  228. }
  229. img {
  230. max-width: 100%;
  231. }
  232. /* Override Cropper's styles */
  233. .cropper-view-box,
  234. .cropper-face {
  235. border-radius: 50%;
  236. }
  237. .finish_room {
  238. width: 400px;
  239. height: auto;
  240. }
  241. .finish_room2 {
  242. width: 100%;
  243. height: auto;
  244. padding-top: 15px;
  245. padding-bottom: 15px;
  246. display: flex;
  247. align-items: center;
  248. }
  249. .finish_room2 .room_img {
  250. width: 80px;
  251. height: 80px;
  252. margin-right: 10px;
  253. position: relative;
  254. overflow: hidden;
  255. }
  256. .finish_room2 .room_img img {
  257. width: 100%;
  258. height: 100%;
  259. }
  260. .finish_room2 > .room_img span {
  261. position: absolute;
  262. width: auto;
  263. height: auto;
  264. right: 5px;
  265. bottom: 3px;
  266. }
  267. .room_add_img {
  268. width: 148px;
  269. height: 98px;
  270. border: 1px solid #e1e1e1;
  271. display: flex;
  272. flex-direction: column;
  273. align-items: center;
  274. justify-content: space-between;
  275. position: relative;
  276. }
  277. .room_add_img > span:nth-child(1) {
  278. margin-top: 20px;
  279. width: 40px;
  280. height: 40px;
  281. overflow: hidden;
  282. }
  283. .room_add_img > span:nth-child(2) {
  284. margin-bottom: 10px;
  285. }
  286. .room_add_img input {
  287. position: absolute;
  288. top: 0px;
  289. left: 0px;
  290. width: 100%;
  291. height: 100%;
  292. z-index: 99999;
  293. opacity: 0;
  294. }
  295. .file-input {
  296. width: 0.1px;
  297. height: 0.1px;
  298. opacity: 0;
  299. overflow: hidden;
  300. position: absolute;
  301. z-index: -1;
  302. }
  303. </style>