上传压缩方法

  1. import {api} from '../../api/api.js';
  2. import axios from 'axios';
  3. export function imgPreview (that, file, type) {
  4. let self = that;
  5. let Orientation;
  6. if (!file || !window.FileReader) return;
  7. if (/^image/.test(file.type)) {
  8. // 创建一个reader
  9. let reader = new FileReader();
  10. // 将图片2将转成 base64 格式
  11. reader.readAsDataURL(file);
  12. // 读取成功后的回调
  13. reader.onloadend = function () {
  14. let result = this.result;
  15. let img = new Image();
  16. img.src = result;
  17. //判断图片是否大于100K,是就直接上传,反之压缩图片
  18. if (this.result.length <= (100 * 1024)) {
  19. if(type == 'imageFront'){
  20. upImgFront(self, this.result);
  21. }
  22. }else {
  23. img.onload = function () {
  24. let src = compress(img,Orientation);
  25. if(type == 'imageFront'){
  26. upImgFront(self, src);
  27. }
  28. }
  29. }
  30. }
  31. }
  32. }
  33. function compress(img,Orientation) {
  34. let canvas = document.createElement("canvas");
  35. let ctx = canvas.getContext('2d');
  36. //瓦片canvas
  37. let tCanvas = document.createElement("canvas");
  38. let tctx = tCanvas.getContext("2d");
  39. let initSize = img.src.length;
  40. let width = img.width;
  41. let height = img.height;
  42. //如果图片大于四百万像素,计算压缩比并将大小压至400万以下
  43. let ratio;
  44. if ((ratio = width * height / 4000000) > 1) {
  45. console.log("大于400万像素")
  46. ratio = Math.sqrt(ratio);
  47. width /= ratio;
  48. height /= ratio;
  49. } else {
  50. ratio = 1;
  51. }
  52. canvas.width = width;
  53. canvas.height = height;
  54. // 铺底色
  55. ctx.fillStyle = "#fff";
  56. ctx.fillRect(0, 0, canvas.width, canvas.height);
  57. //如果图片像素大于100万则使用瓦片绘制
  58. let count;
  59. if ((count = width * height / 1000000) > 1) {
  60. console.log("超过100W像素");
  61. count = ~~(Math.sqrt(count) + 1); //计算要分成多少块瓦片
  62. // 计算每块瓦片的宽和高
  63. let nw = ~~(width / count);
  64. let nh = ~~(height / count);
  65. tCanvas.width = nw;
  66. tCanvas.height = nh;
  67. for (let i = 0; i < count; i++) {
  68. for (let j = 0; j < count; j++) {
  69. tctx.drawImage(img, i * nw * ratio, j * nh * ratio, nw * ratio, nh * ratio, 0, 0, nw, nh);
  70. ctx.drawImage(tCanvas, i * nw, j * nh, nw, nh);
  71. }
  72. }
  73. } else {
  74. ctx.drawImage(img, 0, 0, width, height);
  75. }
  76. //进行最小压缩
  77. let ndata = canvas.toDataURL('image/jpeg', 0.3);
  78. tCanvas.width = tCanvas.height = canvas.width = canvas.height = 0;
  79. return ndata;
  80. }
  81. // 身份证正面
  82. function upImgFront(that, src) {
  83. that.isLoadingShow = true;
  84. that.loadingTit = '图片上传中...';
  85. axios({
  86. method: 'post',
  87. url: api + '/upload/image/base64',
  88. data: {
  89. fileBase64: src,
  90. authType: '1'
  91. },
  92. transformRequest: [function (data) {
  93. let ret = ''
  94. for (let it in data) {
  95. ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&'
  96. }
  97. return ret
  98. }],
  99. headers: {
  100. 'Content-Type': 'application/x-www-form-urlencoded'
  101. }
  102. })
  103. .then((res) => {
  104. that.isLoadingShow = false;
  105. if(res.data.code == 0){
  106. that.imageFrontPath = res.data.data.path;
  107. if(res.data.data.info){
  108. if(res.data.data.info.address){
  109. that.idCardFrontDialogAddress = res.data.data.info.address;
  110. }
  111. if(res.data.data.info.name){
  112. that.idCardFrontDialogName = res.data.data.info.name;
  113. }
  114. if(res.data.data.info.number){
  115. that.idCardFrontDialogId = res.data.data.info.number;
  116. }
  117. }
  118. that.idCardFrontDialog = true;
  119. that.isFrontSuccess = true;
  120. that.imageFront = src;
  121. }else{
  122. setTimeout(() => {
  123. that.isDialogShow = true;
  124. },100);
  125. that.dialogTit = '图片上传失败';
  126. }
  127. })
  128. .catch((error) => {
  129. // that.isLoadingShow = false;
  130. // setTimeout(() => {
  131. // that.isDialogShow = true;
  132. // },100);
  133. // that.dialogTit = '服务器错误';
  134. // console.log('错误了'+ error);
  135. });
  136. }

html

  1. <li class="input-img">
  2. <span class="item"><b>&lowast;</b>身份证正面照</span>
  3. <span class="item-value">
  4. <a href="javascript:" class="input-img-btn" v-on:click="addPicFront">+</a>
  5. <input type="file" @change="onFileFrontChange" style="display: none;" ref="onFileFrontChange" accept="image/*">
  6. <span class="img-wrapper" v-if="imageFront">
  7. <img :src="data:imageFront" alt="" >
  8. <b class="delete" @click="onDelete('imageFront')"></b>
  9. </span>
  10. <span class="img-wrapper" v-else>
  11. <img src="./u111.png" alt="" >
  12. <i>示例照片</i>
  13. </span>
  14. </span>
  15. </li>

javascript

  1. addPicFront(e){
  2. e.preventDefault();
  3. this.$refs.onFileFrontChange.click();
  4. return false;
  5. },
  6.  
  7. onFileFrontChange(e) {
  8. var files = e.target.files || e.dataTransfer.files;
  9. if (!files.length) return;
  10. imgPreview(this, files[0], 'imageFront');
  11. this.$refs.onFileFrontChange.value = '';
  12. this.idCardFrontDialogName = '';
  13. this.idCardFrontDialogId = '';
  14. this.idCardFrontDialogAddress = '';
  15. },

vue移动端图片上传压缩的更多相关文章

  1. 移动端图片上传解决方案localResizeIMG先压缩后ajax无刷新上传

    现在科技太发达,移动设备像素越来越高,随便一张照片2M+,但是要做移动端图片上传和pc上略有不同,移动端你不能去限制图片大小,让用户先处理图片再上传,这样不现实.所以理解的解决方案就是在上传先进行图片 ...

  2. angularJS+Ionic移动端图片上传的解决办法

    前端开发中经常会碰到图片上传的问题,网上的解决办法很多,可是有些图片上传的插件会有一些附属的插件,因此因为一个图片上传的问题可能额需要引入其他插件到项目中,久而久之项目会不伦不类,有时候插件之间也会有 ...

  3. vue+axios实现移动端图片上传

    在利用vue做一些H5页面时,或多或少会遇到有图片上传的操作,主要是运用html5里面的input[type=file]来实现,传递到后端的数据是以二进制的格式传递,所以上传图片的请求与普通的请求稍微 ...

  4. HTML5移动端图片上传模块

    上传图片的功能是极为常用的,之前做过一个移动端上传文件的功能(基于jquery的),总结梳理一下. html <div class="uploadPic clearBox"& ...

  5. 用Vue来实现图片上传多种方式

    没有业务场景的功能都是耍流氓,那么我们先来模拟一个需要实现的业务场景.假设我们要做一个后台系统添加商品的页面,有一些商品名称.信息等字段,还有需要上传商品轮播图的需求. 我们就以Vue.Element ...

  6. 用html5文件api实现移动端图片上传&预览效果

    想要用h5在移动端实现图片上传&预览效果,首先要了解html5的文件api相关知识(所有api只列举本功能所需): 1.Blob对象  Blob表示原始二进制数据,Html5的file对象就继 ...

  7. 百度ueditor vue项目应用 -- 图片上传源码修改

    本文目的有两个,一.废掉单图上传,二.改造多图上传 大家都知道百度ueditor不是针对vue项目开发的,官方文档提供的源码包里有需要后端配置的接口,but到vue项目就不太好办了,网上有些文章也介绍 ...

  8. Js 之移动端图片上传插件mbUploadify

    一.下载 https://pan.baidu.com/s/1NEL4tkHoK4ydqdMi_hgWcw 提取码:vx7e 二.Demo示例 <div class="weui_uplo ...

  9. vue开发:移动端图片上传

    因为最近遇到个移动端上传头像的需求,上传到后台的数据是base64位,其中为了提高用户体验,把比较大的图片用canvas进行压缩之后再进行上传.在移动端调用拍照功能时,会发生图片旋转,为了解决这个问题 ...

随机推荐

  1. 【7.18总结】KM算法

    先贴代码,参考博客来源于:https://blog.csdn.net/zyy173533832/article/details/11519291#commentBox 例题:HDU 2255 题意:n ...

  2. char和achar互转

    #pragma once#include "stdafx.h" #ifndef _Convert_H_#define _Convert_H_ //定义转换类class Conver ...

  3. Java练习 SDUT-2401最大矩形面积

    最大矩形面积 Time Limit: 1000 ms Memory Limit: 65536 KiB Problem Description 在一个矩形区域内有很多点,每个点的坐标都是整数.求一个矩形 ...

  4. 在centos搭建git服务器时,不小心把/home/git目录删除了,我是怎么恢复的

    在centos搭建git服务器时,不小心把/home/git目录删除了,我是怎么恢复的 在删除掉/home/git目录后,每次 git push提交时,都让填写密码,烦 第一步:在本地找到id_rsa ...

  5. UVa 10599【lis dp,记忆化搜索】

    UVa 10599 题意: 给出r*c的网格,其中有些格子里面有垃圾,机器人从左上角移动到右下角,只能向右或向下移动.问机器人能清扫最多多少个含有垃圾的格子,有多少中方案,输出其中一种方案的格子编号. ...

  6. C#判断文件是否被混淆

    可以使用混淆工具对一个DLL 和 exe 进行混淆. 但是如何知道一个文件是否已经混淆了. 在发布之前,需要知道是不是有文件忘了混淆. 要判断文件是否混淆,必须知道常用的混淆手法. 混淆就是因为编写的 ...

  7. Python3.6正向解析与反向解析域中主机

    公司最近接手的一家跨国企业的项目,该企业单域.多站点,且遍布美国.巴西.日本.东京.新加坡等多个国家,服务器及客户端计算机数量庞大.由于处理一些特殊故障,需要找出一些不在域中的网络设备及存储.NBU等 ...

  8. @codechef - TREEPATH@ Decompose the Tree

    目录 @description@ @solution@ @accepted code@ @details@ @description@ 给定一棵无根树,每个节点上都写了一个整数. 你的任务就是统计有多 ...

  9. PHP中__FUNCTION__与__METHOD__的区别

    你知道php中__FUNCTION__与__METHOD__的区别吗?本文通过一个小例子,为大家介绍下二者的区别,有兴趣的朋友可以参考下.   PHP中__FUNCTION__与__METHOD__的 ...

  10. 仿IOS效果-带弹簧动画的ListView

    背景介绍 最近项目打算做一个界面,类似于dayone首页的界面效果,dayone 是一款付费应用,目前只有IOS端.作为一个资深懒惰的程序员,奉行的宗旨是绝对不重复造一个轮子.于是乎,去网上找一大堆开 ...