vue开发:移动端图片上传
因为最近遇到个移动端上传头像的需求,上传到后台的数据是base64位,其中为了提高用户体验,把比较大的图片用canvas进行压缩之后再进行上传。在移动端调用拍照功能时,会发生图片旋转,为了解决这个问题引入了exif去判断拍照时的信息再去处理图片,这是个很好的插件。关于exif.js可以去他的GitHub上了解,这边直接npm install exif-js --save 安装,然后import一下就可以使用了。以下就是源码,可以直接使用。
- <template>
- <div>
- <div style="padding:20px;">
- <div class="show">
- <div class="picture" :style="'backgroundImage:url('+headerImage+')'"></div>
- </div>
- <div style="margin-top:20px;">
- <input type="file" id="upload" accept="image" @change="upload">
- <label for="upload"></label>
- </div>
- </div>
- </div>
- </template>
- <script>
- import Exif from 'exif-js'
- export default {
- data () {
- return {
- headerImage:'',picValue:''
- }
- },
- mounted () {
- },
- methods: {
- upload (e) {
- let files = e.target.files || e.dataTransfer.files;
- if (!files.length) return;
- this.picValue = files[0];
- this.imgPreview(this.picValue);
- },
- imgPreview (file) {
- let self = this;
- let Orientation;
- //去获取拍照时的信息,解决拍出来的照片旋转问题
- Exif.getData(file, function(){
- Orientation = Exif.getTag(this, 'Orientation');
- });
- // 看支持不支持FileReader
- 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)) {
- self.headerImage = this.result;
- self.postImg();
- }else {
- img.onload = function () {
- let data = self.compress(img,Orientation);
- self.headerImage = data;
- self.postImg();
- }
- }
- }
- }
- },
- postImg () {
- //这里写接口
- },
- rotateImg (img, direction,canvas) {
- //最小与最大旋转方向,图片旋转4次后回到原方向
- const min_step = 0;
- const max_step = 3;
- if (img == null)return;
- //img的高度和宽度不能在img元素隐藏后获取,否则会出错
- let height = img.height;
- let width = img.width;
- let step = 2;
- if (step == null) {
- step = min_step;
- }
- if (direction == 'right') {
- step++;
- //旋转到原位置,即超过最大值
- step > max_step && (step = min_step);
- } else {
- step--;
- step < min_step && (step = max_step);
- }
- //旋转角度以弧度值为参数
- let degree = step * 90 * Math.PI / 180;
- let ctx = canvas.getContext('2d');
- switch (step) {
- case 0:
- canvas.width = width;
- canvas.height = height;
- ctx.drawImage(img, 0, 0);
- break;
- case 1:
- canvas.width = height;
- canvas.height = width;
- ctx.rotate(degree);
- ctx.drawImage(img, 0, -height);
- break;
- case 2:
- canvas.width = width;
- canvas.height = height;
- ctx.rotate(degree);
- ctx.drawImage(img, -width, -height);
- break;
- case 3:
- canvas.width = height;
- canvas.height = width;
- ctx.rotate(degree);
- ctx.drawImage(img, -width, 0);
- break;
- }
- },
- 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);
- }
- //修复ios上传图片的时候 被旋转的问题
- if(Orientation != "" && Orientation != 1){
- switch(Orientation){
- case 6://需要顺时针(向左)90度旋转
- this.rotateImg(img,'left',canvas);
- break;
- case 8://需要逆时针(向右)90度旋转
- this.rotateImg(img,'right',canvas);
- break;
- case 3://需要180度旋转
- this.rotateImg(img,'right',canvas);//转两次
- this.rotateImg(img,'right',canvas);
- break;
- }
- }
- //进行最小压缩
- let ndata = canvas.toDataURL('image/jpeg', 0.1);
- console.log('压缩前:' + initSize);
- console.log('压缩后:' + ndata.length);
- console.log('压缩率:' + ~~(100 * (initSize - ndata.length) / initSize) + "%");
- tCanvas.width = tCanvas.height = canvas.width = canvas.height = 0;
- return ndata;
- },
- }
- }
- </script>
- <style>
- *{
- margin: 0;
- padding: 0;
- }
- .show {
- width: 100px;
- height: 100px;
- overflow: hidden;
- position: relative;
- border-radius: 50%;
- border: 1px solid #d5d5d5;
- }
- .picture {
- width: 100%;
- height: 100%;
- overflow: hidden;
- background-position: center center;
- background-repeat: no-repeat;
- background-size: cover;
- }
- </style>
vue开发:移动端图片上传的更多相关文章
- 移动端图片上传解决方案localResizeIMG先压缩后ajax无刷新上传
现在科技太发达,移动设备像素越来越高,随便一张照片2M+,但是要做移动端图片上传和pc上略有不同,移动端你不能去限制图片大小,让用户先处理图片再上传,这样不现实.所以理解的解决方案就是在上传先进行图片 ...
- angularJS+Ionic移动端图片上传的解决办法
前端开发中经常会碰到图片上传的问题,网上的解决办法很多,可是有些图片上传的插件会有一些附属的插件,因此因为一个图片上传的问题可能额需要引入其他插件到项目中,久而久之项目会不伦不类,有时候插件之间也会有 ...
- 详细阐述Web开发中的图片上传问题
Web开发中,图片上传是一种极其常见的功能.但是呢,每次做上传,都花费了不少时间. 一个"小功能"花费我这么多时间,真心不愉快. So,要得认真分析下原因. 1.在最初学习Java ...
- 微信小程序开发之多图片上传+服务端接收
前言: 业务需求,这次需要做一个小程序同时选中三张图片一起上传到服务端,后端使用的.NET WEBAPI接收数据保存. 使用技术: 在这章中将会使用到微信小程序wx.uploadFile(Object ...
- vue+axios实现移动端图片上传
在利用vue做一些H5页面时,或多或少会遇到有图片上传的操作,主要是运用html5里面的input[type=file]来实现,传递到后端的数据是以二进制的格式传递,所以上传图片的请求与普通的请求稍微 ...
- vue移动端图片上传压缩
上传压缩方法 import {api} from '../../api/api.js'; import axios from 'axios'; export function imgPreview ( ...
- HTML5移动端图片上传模块
上传图片的功能是极为常用的,之前做过一个移动端上传文件的功能(基于jquery的),总结梳理一下. html <div class="uploadPic clearBox"& ...
- 用html5文件api实现移动端图片上传&预览效果
想要用h5在移动端实现图片上传&预览效果,首先要了解html5的文件api相关知识(所有api只列举本功能所需): 1.Blob对象 Blob表示原始二进制数据,Html5的file对象就继 ...
- SpringBoot + Vue前后端分离图片上传到本地并前端访问图片
同理应该可用于其他文件 图片上传 application.yml 配置相关常量 prop: upload-folder: E:/test/ # 配置SpringMVC文件上传限制,默认1M.注意MB要 ...
- Js 之移动端图片上传插件mbUploadify
一.下载 https://pan.baidu.com/s/1NEL4tkHoK4ydqdMi_hgWcw 提取码:vx7e 二.Demo示例 <div class="weui_uplo ...
随机推荐
- Mysql字符集与校对规则
字符集是一套字符和编码的集合,校对规则是用于比较字符集的一套规则. 所以字符集有两部分组成字符集合和对应的编码集合.比如说,现在有这几个字符:A B a b, 假设它们对应的编码分别是00, 01, ...
- 【BZOJ2049】洞穴勘测(LCT)
题意:一张图,要求支持以下操作: 1.加边 2.删边 3.询问两点之间是否联通 100%的数据满足n≤10000, m≤200000 思路:LCT裸题,不需要维护任何信息 ..,..]of longi ...
- 转载自CSDN,结论:windows下按ENTER键应该是\r\n ascii码为 13 10
记得在Windows下学X86汇编语言时,用0DH(\r)和0AH(\n)来输出回车(跳到下一行的开始处).问题来了,在Windows下是 先回车再换行呢还是先换行再回车呢?在Unix系统下换行只有\ ...
- SGU 105 数学找规律
观察一下序列,每3个数一组,第一个数余1,不能,加第二个数后整除(第二个数本身余2),第三数恰整除.一行代码的事.011011011.... #include<iostream> usin ...
- call 和 apply 方法区别
在js中call和apply它们的作用都是将函数绑定到另外一个对象上去运行,两者仅在定义参数方式有所区别,下面我来给大家介绍一下call和apply用法. 在web前端开发过程中,我们经常需要改变th ...
- 51 NOD 1383 整数分解为2的幂
设f[i]为i这个数的划分方案,则: 1.i是奇数的时候,最前面只能放1,所以f[i] = f[i-1] 2.i是偶数的时候,最前面可以放1也可以不放1,而不放1的时候数列都是偶数所以 f[i] = ...
- Codechef Yet another cute girl
题意大概就是让你求一下[L,R]中的约数个数是素数的数的个数. 其中1<=L<=R<=1e12,R-L<=1e6. 然后我写了两种做法,第一种是可以直接搞出来L-R的约数个数, ...
- springboot启动报异常,Failed to load property source from location 'classpath:/application.yml'
学习springboot,在启动时抛出下图异常 往下看异常信息,找到异常的具体位置 找到application.yml文件的对应位置,发现params配置前面多了空格 去掉空格重新启动,可以了 写代码 ...
- Android SDK Manager 更新时的“https://dl-ssl.google.com refused”错误
Android SDK Manager 消除SDK更新时的“https://dl-ssl.google.com refused”错误 消除SDK更新时,有可能会出现这样的错误:Download int ...
- 问题:typedef char *pstring????
typedef char *pstring; const pstring cstr = 0; //cstr是指向char的常量指针: const pstring *ps; //ps是一个指针,它的对象 ...