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 ...
随机推荐
- ElasticSearch索引自定义类型
ES可以自动检测字段并设置映射类型.如果设置的索引类型不是我们所需要的,我们可以自行定义. Rest API设置自定义索引 首先通过ES自动映射一个IP地址的字段的类型: <pre name=& ...
- 改变input的value值,同时在HTML中将value进行改变
在使用lodop进行打印的时候,需求中有这样一个功能:某个字段可以在页面的input框中进行修改. 但是在进行打印时调用的是静态的HTML代码,这就导致在页面的input框中改变字段之后,但是HTML ...
- 王垠 :写给支持和反对《完全用Linux工作》的人们 ( 2004/8/26)
在一阵阵唾骂和欢呼声中,<完全用linux工作>被转载到了天涯海角.很多人支持我,也有很多人唾骂我.我不知道它是怎样流传到那么多地方,其实 我不知道它都被转到哪里去了…… 我知道现在这篇文 ...
- Unity3D 对象池思想 在游戏开发中的运用
分类:U3D 1.在王者荣耀中,每30秒小兵会出现一波,出现之后会被敌方玩家或敌方小兵销毁,一局游戏下来,小兵会被创建多次,同时也会被销毁,在游戏中,这种频繁的创建和销毁游戏对象是很损耗性能的.在游戏 ...
- 2017-10-29-morning-清北模拟赛
T1 遭遇 #include <algorithm> #include <cstdio> #include <cmath> inline void read(int ...
- Maven配置将war包部署到Tomcat(tomcat7-maven-plugin)
Tomcat7/8: 提示:经过测试Tomcat7的配置和插件在Tomcat8中能正常运行 tomcat7-maven-plugin官方帮助文档:http://tomcat.apache.org/ma ...
- spring工具类获取bean
import org.springframework.web.context.ContextLoader; import org.springframework.web.context.WebAppl ...
- 深入GCD(四):使用串行队列实现简单的预加载
其主要思路是使用gcd创建串行队列,然后在此队列中先后执行两个任务:1.预加载一个viewController 2.将这个viewController推入代码如下:@implementation DW ...
- 《C++ Primer Plus》学习笔记9
<C++ Primer Plus>学习笔记9 第15章 友元.异常和其他 <<<<<<<<<<<<<<& ...
- Android4.42-Setting源代码分析之蓝牙模块Bluetooth(下)
接着上一篇Android4.42-Settings源代码分析之蓝牙模块Bluetooth(上) 继续蓝牙模块源代码的研究 THREE.蓝牙模块功能实现 switch的分析以及本机蓝牙重命名和可见性的分 ...