UploadImg.vue 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. <template>
  2. <div class="img-wrap">
  3. <div class="box">
  4. <ul class="upload-imgs">
  5. <li
  6. v-if="imgLen>=6 ? false : true"
  7. class="uploadBox"
  8. @click="handleUpload"
  9. >
  10. <input
  11. type="file"
  12. class="upload"
  13. @change="addImg"
  14. accept="image/*"
  15. ref="inp"
  16. style="display:none"
  17. />
  18. <img src="../images/upload.png">
  19. <p>点击上传</p>
  20. </li>
  21. <li v-for='(value, key) in imgs'>
  22. <p class="imgbox">
  23. <img
  24. :src="value"
  25. :preview="item.id"
  26. class="img"
  27. preview-text=""
  28. >
  29. </p>
  30. <a
  31. class="close"
  32. @click="delImg(key)"
  33. ></a>
  34. </li>
  35. </ul>
  36. </div>
  37. </div>
  38. </template>
  39. <script type="text/javascript">
  40. export default {
  41. name: 'UploadImg',
  42. data() {
  43. return {
  44. mag: "上传图片",
  45. imgs: {},
  46. imgLen: 0
  47. }
  48. },
  49. props: ['item', 'moduleType', 'imgList'],//moduleType-哪个模块下上传的图
  50. mounted() {
  51. this.imgs = this.imgList; //回读
  52. this.$previewRefresh();//预览刷新
  53. },
  54. methods: {
  55. handleUpload() {
  56. // navigator.getUserMedia({video: true,audio:true}, function onSuccess(stream) {
  57. /*navigator.getUserMedia({video: true}, function onSuccess(stream) {
  58. const inp = this.$refs.inp;
  59. inp.click();
  60. }, function onError(error) {
  61. alert("请开启权限设置")
  62. })*/
  63. const inp = this.$refs.inp;
  64. inp.click();
  65. },
  66. delImg(key) {
  67. let obj = this.imgs;
  68. delete (obj[key]);
  69. this.imgLen--;
  70. this.imgs = Object.assign({}, obj);
  71. this.$store.commit('deleImg', { key: key, type: this.moduleType });
  72. this.$store.commit('deleSrc', { key: key, type: this.moduleType });
  73. },
  74. addImg() {
  75. // 上传图片进行压缩,压缩后超过4M则不允许上传
  76. let fileTag = this.$refs.inp;
  77. // let img = this.$refs.img;
  78. let file = fileTag.files[0];
  79. const that = this;
  80. this.imgBase64(file, function (image, canvas) {
  81. console.log("图片宽:",image.naturalWidth,"图片高度:",image.naturalHeight)
  82. var maxSize = 4 * 1024; // 4M
  83. var fileSize = file.size / 1024; //kb 图片大小
  84. var uploadSrc;
  85. var uploadFile;
  86. if (fileSize > maxSize) { // 如果图片大小大于4m,进行压缩
  87. // console.log(maxSize,fileSize, maxSize/fileSize );
  88. uploadSrc = canvas.toDataURL(file.type, maxSize / fileSize);
  89. uploadFile = that.dataURLtoFile(uploadSrc, file.name.split('.')[0]); // 转成file文件
  90. // uploadFile = that.convertBase64UrlToBlob(uploadSrc); // 转成blob
  91. } else {
  92. uploadSrc = image.src; //canvas.toDataURL(file.type,0.5);
  93. uploadFile = file;
  94. }
  95. var compressedSize = uploadFile.size / 1024 / 1024;
  96. if (compressedSize.toFixed(2) > 4.00) {
  97. alert('上传图片不可超过4M');
  98. } else {
  99. let key = file.name + new Date().getTime();
  100. that.$set(that.imgs, key, uploadSrc);
  101. that.imgLen++;
  102. // 将图片信息存到store
  103. that.$store.commit('setImgFile', { type: that.moduleType, pId: that.item.id, key: key, file: uploadFile })
  104. that.$store.commit('setImgSrc', { key: key, src: uploadSrc, type: that.moduleType })
  105. that.$previewRefresh(); //异步获取的图片需要刷新下
  106. }
  107. // that.$refs.inp.value = '';
  108. fileTag.value = '';
  109. });
  110. },
  111. imgBase64(file, callback) {
  112. var self = this;
  113. // 看支持不支持FileReader
  114. if (!file || !window.FileReader) return;
  115. // 创建一个 Image 对象
  116. var image = new Image();
  117. // 绑定 load 事件处理器,加载完成后执行
  118. image.onload = function () {
  119. // 获取 canvas DOM 对象
  120. var canvas = document.createElement('canvas')
  121. // 返回一个用于在画布上绘图的环境, '2d' 指定了您想要在画布上绘制的类型
  122. var ctx = canvas.getContext('2d')
  123. // 如果高度超标 // 参数,最大高度
  124. var MAX_HEIGHT = 3000;
  125. if (image.height > MAX_HEIGHT) {
  126. // 宽度等比例缩放 *=
  127. image.width *= MAX_HEIGHT / image.height;
  128. image.height = MAX_HEIGHT;
  129. }
  130. // 获取 canvas的 2d 环境对象,
  131. // canvas清屏
  132. ctx.clearRect(0, 0, canvas.width, canvas.height);
  133. // 重置canvas宽高
  134. canvas.width = image.width;
  135. canvas.height = image.height;
  136. // 将图像绘制到canvas上
  137. ctx.drawImage(image, 0, 0, image.width, image.height);
  138. callback(image, canvas);
  139. };
  140. if (/^image/.test(file.type)) {
  141. var reader = new FileReader();
  142. // 将图片将转成 base64 格式
  143. reader.readAsDataURL(file);
  144. // 读取成功后的回调
  145. reader.onload = function () {
  146. // 设置src属性,浏览器会自动加载。
  147. // 记住必须先绑定事件,才能设置src属性,否则会出同步问题。
  148. image.src = this.result;
  149. }
  150. }
  151. },
  152. // 将以base64的图片url数据转换为Blob
  153. convertBase64UrlToBlob(urlData) {
  154. var bytes = window.atob(urlData.split(',')[1]); //去掉url的头,并转换为byte
  155. //处理异常,将ascii码小于0的转换为大于0
  156. var ab = new ArrayBuffer(bytes.length);
  157. var ia = new Uint8Array(ab);
  158. for (var i = 0; i < bytes.length; i++) {
  159. ia[i] = bytes.charCodeAt(i);
  160. }
  161. return new Blob([ab], { type: 'image/jpg' });
  162. },
  163. // 将以base64的图片url数据转换为file
  164. dataURLtoFile(dataurl, filename) { //将base64转换为文件
  165. var arr = dataurl.split(','),
  166. mime = arr[0].match(/:(.*?);/)[1],
  167. bstr = atob(arr[1]),
  168. n = bstr.length,
  169. u8arr = new Uint8Array(n);
  170. while (n--) {
  171. u8arr[n] = bstr.charCodeAt(n);
  172. }
  173. return new File([u8arr], filename, { type: mime });
  174. },
  175. }
  176. }
  177. </script>
  178. <style lang="less" scoped>
  179. .img-wrap {
  180. font-size: 0.3rem;
  181. .upload-imgs {
  182. margin-bottom: 0.2rem;
  183. .uploadBox {
  184. border: 1px solid #dfe0e4;
  185. box-sizing: border-box;
  186. text-align: center;
  187. img {
  188. width: 0.6rem;
  189. margin: 0.45rem 0 0.23rem 0;
  190. }
  191. }
  192. li {
  193. width: 1.86rem;
  194. height: 1.9rem;
  195. border: 1px solid #dfe0e4;
  196. display: inline-block;
  197. position: relative;
  198. vertical-align: top;
  199. border-radius: 0.08rem;
  200. margin: 0 0 0.3rem 0.3rem;
  201. .close {
  202. width: 0.54rem;
  203. height: 0.54rem;
  204. background: url(../images/del-pic.png) no-repeat;
  205. background-size: cover;
  206. position: absolute;
  207. top: -0.27rem;
  208. right: -0.27rem;
  209. z-index: 3;
  210. }
  211. }
  212. li:nth-child(3n + 1) {
  213. margin-left: 0;
  214. }
  215. .imgbox {
  216. width: 100%;
  217. height: 100%;
  218. overflow: hidden;
  219. border-radius: 0.08rem;
  220. position: relative;
  221. .img {
  222. width: 100%;
  223. position: absolute;
  224. left: 50%;
  225. top: 50%;
  226. transform: translate(-50%,-50%);
  227. }
  228. }
  229. }
  230. }
  231. </style>