上传压缩方法

 import {api} from '../../api/api.js';
import axios from 'axios';
export function imgPreview (that, file, type) {
let self = that;
let Orientation;
if (!file || !window.FileReader) return;
if (/^image/.test(file.type)) {
// 创建一个reader
let reader = new FileReader();
// 将图片2将转成 base64 格式
reader.readAsDataURL(file);
// 读取成功后的回调
reader.onloadend = function () {
let result = this.result;
let img = new Image();
img.src = result;
//判断图片是否大于100K,是就直接上传,反之压缩图片
if (this.result.length <= (100 * 1024)) {
if(type == 'imageFront'){
upImgFront(self, this.result);
}
}else {
img.onload = function () {
let src = compress(img,Orientation);
if(type == 'imageFront'){
upImgFront(self, src);
}
}
}
}
}
}
function compress(img,Orientation) {
let canvas = document.createElement("canvas");
let ctx = canvas.getContext('2d');
//瓦片canvas
let tCanvas = document.createElement("canvas");
let tctx = tCanvas.getContext("2d");
let initSize = img.src.length;
let width = img.width;
let height = img.height;
//如果图片大于四百万像素,计算压缩比并将大小压至400万以下
let ratio;
if ((ratio = width * height / 4000000) > 1) {
console.log("大于400万像素")
ratio = Math.sqrt(ratio);
width /= ratio;
height /= ratio;
} else {
ratio = 1;
}
canvas.width = width;
canvas.height = height;
// 铺底色
ctx.fillStyle = "#fff";
ctx.fillRect(0, 0, canvas.width, canvas.height);
//如果图片像素大于100万则使用瓦片绘制
let count;
if ((count = width * height / 1000000) > 1) {
console.log("超过100W像素");
count = ~~(Math.sqrt(count) + 1); //计算要分成多少块瓦片
// 计算每块瓦片的宽和高
let nw = ~~(width / count);
let nh = ~~(height / count);
tCanvas.width = nw;
tCanvas.height = nh;
for (let i = 0; i < count; i++) {
for (let j = 0; j < count; j++) {
tctx.drawImage(img, i * nw * ratio, j * nh * ratio, nw * ratio, nh * ratio, 0, 0, nw, nh);
ctx.drawImage(tCanvas, i * nw, j * nh, nw, nh);
}
}
} else {
ctx.drawImage(img, 0, 0, width, height);
}
//进行最小压缩
let ndata = canvas.toDataURL('image/jpeg', 0.3);
tCanvas.width = tCanvas.height = canvas.width = canvas.height = 0;
return ndata;
}
// 身份证正面
function upImgFront(that, src) {
that.isLoadingShow = true;
that.loadingTit = '图片上传中...';
axios({
method: 'post',
url: api + '/upload/image/base64',
data: {
fileBase64: src,
authType: '1'
},
transformRequest: [function (data) {
let ret = ''
for (let it in data) {
ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&'
}
return ret
}],
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
}
})
.then((res) => {
that.isLoadingShow = false;
if(res.data.code == 0){
that.imageFrontPath = res.data.data.path;
if(res.data.data.info){
if(res.data.data.info.address){
that.idCardFrontDialogAddress = res.data.data.info.address;
}
if(res.data.data.info.name){
that.idCardFrontDialogName = res.data.data.info.name;
}
if(res.data.data.info.number){
that.idCardFrontDialogId = res.data.data.info.number;
}
}
that.idCardFrontDialog = true;
that.isFrontSuccess = true;
that.imageFront = src;
}else{
setTimeout(() => {
that.isDialogShow = true;
},100);
that.dialogTit = '图片上传失败';
}
})
.catch((error) => {
// that.isLoadingShow = false;
// setTimeout(() => {
// that.isDialogShow = true;
// },100);
// that.dialogTit = '服务器错误';
// console.log('错误了'+ error);
});
}

html

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

javascript

 addPicFront(e){
e.preventDefault();
this.$refs.onFileFrontChange.click();
return false;
}, onFileFrontChange(e) {
var files = e.target.files || e.dataTransfer.files;
if (!files.length) return;
imgPreview(this, files[0], 'imageFront');
this.$refs.onFileFrontChange.value = '';
this.idCardFrontDialogName = '';
this.idCardFrontDialogId = '';
this.idCardFrontDialogAddress = '';
},

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. PLAY2.6-SCALA(五) Action的组合、范围的设置以及错误的处理

    一.自定义action 从一个日志装饰器的例子开始 1.在invokeBlock方法中实现 import play.api.mvc._ class LoggingAction @Inject() (p ...

  2. poj 2385【动态规划】

    poj 2385 Apple Catching Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 14007   Accepte ...

  3. request header....

    root@xxx# curl -i --get --include 'http://ali-barcode.showapi.com/barcode?code=6938166920785' -H 'Au ...

  4. 2019-9-2-win10-uwp-随着数字变化颜色控件

    title author date CreateTime categories win10 uwp 随着数字变化颜色控件 lindexi 2019-09-02 12:57:38 +0800 2018- ...

  5. EL表达式简单总结

    EL表达式 ## EL表达式的取值范围 JSP的四个作用域: pagecontext(生命周期用户离开或者跳转页面,作用域范围这个页面) request(生命周期用户离开页面,作用于这个页面) ses ...

  6. Gym - 101480K_K - Kernel Knights (DFS)

    题意:有两队骑士各n人,每位骑士会挑战对方队伍的某一个位骑士. (可能相同) 要求找以一个区间s: 集合S中的骑士不会互相挑战. 每个集合外的骑士必定会被集合S内的某个骑士挑战. 题解:讲真被题目绕懵 ...

  7. uva 11916 Emoogle Grid (BSGS)

    UVA 11916 BSGS的一道简单题,不过中间卡了一下没有及时取模,其他这里的100000007是素数,所以不用加上拓展就能做了. 代码如下: #include <cstdio> #i ...

  8. LA 4119 Always an integer (数论+模拟)

    ACM-ICPC Live Archive 一道模拟题,题意是问一个给出的多项式代入正整数得到的值是否总是整数. 这题是一道数论题,其实对于这个式子,我们只要计算1~最高次项是否都满足即可. 做的时候 ...

  9. @bzoj - 4382@ [POI2015] Podział naszyjnika

    目录 @description@ @solution@ @accepted code@ @details@ @description@ 长度为 n 的一串项链,每颗珠子是 k 种颜色之一. 第 i 颗 ...

  10. Rikka with Mista 线段树求交点个数

    由于上下线段是不可能有交点的 可以先看左右线段树,按照y递增的顺序,对点进行排序. 升序构造,那么对于从某一点往下的射线,对于L,R进行区间覆盖,线段交点个数就是单点的被覆盖的次数. 降序构造,那么对 ...