vuejs开发组件分享之H5图片上传、压缩及拍照旋转的问题处理
作者:东河村长 发布时间:2024-05-10 14:14:25
标签:vue,组件,上传
一、前言
三年.net开发转前端已经四个月了,前端主要用webpack+vue,由于后端转过来的,前端不够系统,希望分享下开发心得与园友一起学习。
图片的上传之前都是用的插件(ajaxupload),或者传统上传图片的方式,各有利弊:插件的问题是依赖jq并且会使系统比较臃肿,还有传统的web开发模式 前后端偶尔在一起及对用户体验要求低,现在公司采用webpack+vue+restfullApi开发模式 前后端完全分离,遵从高内聚,低偶尔的原则,开发人员各司其职,一则提升开发效率(从长期来看,短期对于很多开发人员需要有个适应的过程,特别是初中级的前端处理业务逻辑方面的能力比较欠缺),二则提升用户体验。今天分享下在项目开发中写的的图片上传 vue组件。
二、处理问题
这里用h5做图片上传考虑到浏览器支持的问题,这里考虑的场景是在做webapp的时候
1.移动web图片上传还包括拍摄上传,但是在移动端会出现拍摄的照片会旋转,处理这个问题需要得到图片旋转的情况,可以用exif.js来获取,具体可以参看文档
2.图片压缩
3.旋转
三、代码
1组件代码
<template>
<div>
<input type="file" style="display: none;" id="img-upload" multiple accept="image/*" @change="uploadImg($event)"/>
</div>
</template>
<script>
import EXIF from '../../../Resource/Global/Js/exif'
export default{
name:"image-html5-upload",
props:{
imgArr:{
type:Array,
twoWay: true,
default:Array
},
imgNumLimit:{//一次最多可以上传多少张照片
type:Number,
default:4
}
},
methods:{
"uploadImg": function(e){
let tag = e.target;
let fileList = tag.files;
let imgNum = fileList.length;
let _this = this;
_this.imgArr = [];//图片数据清零
if(this.imgArr.length + imgNum > this.imgNumLimit){
alert('一次最多上传'+this.imgNumLimit+'张图片!');
return;
}
var Orientation;
for(let i=0;i<imgNum;i++){
EXIF.getData(fileList[i], function(){
Orientation = EXIF.getTag(fileList[i], 'Orientation');
});
let reader = new FileReader();
reader.readAsDataURL(fileList[i]);
reader.onload = function(){
var oReader = new FileReader();
oReader.onload = function(e) {
var image = new Image();
image.src = e.target.result;
image.onload = function() {
var expectWidth = this.naturalWidth;
var expectHeight = this.naturalHeight;
if (this.naturalWidth > this.naturalHeight && this.naturalWidth > 800) {
expectWidth = 800;
expectHeight = expectWidth * this.naturalHeight / this.naturalWidth;
} else if (this.naturalHeight > this.naturalWidth && this.naturalHeight > 1200) {
expectHeight = 1200;
expectWidth = expectHeight * this.naturalWidth / this.naturalHeight;
}
var canvas = document.createElement("canvas");
var ctx = canvas.getContext("2d");
canvas.width = expectWidth;
canvas.height = expectHeight;
ctx.drawImage(this, 0, 0, expectWidth, expectHeight);
var base64 = null;
//修复ios上传图片的时候 被旋转的问题
if(Orientation != "" && Orientation != 1){
switch(Orientation){
case 6://需要顺时针(向左)90度旋转
_this.rotateImg(this,'left',canvas);
break;
case 8://需要逆时针(向右)90度旋转
_this.rotateImg(this,'right',canvas);
break;
case 3://需要180度旋转
_this.rotateImg(this,'right',canvas);//转两次
_this.rotateImg(this,'right',canvas);
break;
}
}
base64 = canvas.toDataURL("image/jpeg", 0.8);
if(fileList[i].size / 1024000 > 1){
_this.imgScale(base64, 4)
}else{
_this.imgArr.push({"src": base64});
}
console.log(JSON.stringify(_this.imgArr));
};
};
oReader.readAsDataURL(fileList[i]);
}
}
},
"imgScale": function(imgUrl,quality){
let img = new Image();
let _this = this;
let canvas = document.createElement('canvas');
let cxt = canvas.getContext('2d');
img.src = imgUrl;
img.onload = function(){
//缩放后图片的宽高
let width = img.naturalWidth/quality;
let height = img.naturalHeight/quality;
canvas.width = width;
canvas.height = height;
cxt.drawImage(this, 0, 0, width, height);
_this.imgArr.push({"src": canvas.toDataURL('image/jpeg')});
}
},
"rotateImg":function (img, direction,canvas) {//图片旋转
var min_step = 0;
var max_step = 3;
if (img == null)return;
var height = img.height;
var width = img.width;
var 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);
}
var degree = step * 90 * Math.PI / 180;
var 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;
}
}
}
}
</script>
2.使用方法
<template>
<div>
<div class="album-img-list">
<ul>
<li v-for="img in imgList"><div class="album-bg-img"><img :src='img.src'> </div></li>
</ul>
</div>
<div class="album">
<label for="img-upload">上传照片</label>
<image-html5-upload :img-arr.sync="imgList"></image-html5-upload>
</div>
</div>
</template>
以上所述是小编给大家介绍的vuejs开发组件分享之H5图片上传、压缩及拍照旋转的问题处理网站的支持!
来源:http://www.cnblogs.com/net-xiejun/p/5901598.html


猜你喜欢
- 目标在本章中,将学习使用kNN来构建基本的OCR应用程使用OpenCV自带的数字和字母数据集手写数字的OCR目标是构建一个可以读取手写数字的
- 原文地址:30 Days of Mootools 1.2 Tutorials - Day 8 - Input Filtering Part
- 一、简介Selenium是一个用于Web应用程序测试的工具。Selenium测试直接运行在浏览器中,就像真正的用户在操作一样。支持的浏览器包
- 一、需求来源:如果用户在文本框中填了一段<script>alert(xxx);</script>代码,然后我们还保存
- csv(Comma-Separated Values)文件是什么?它是一种文件格式,一般也被叫做逗号分隔值文件,可以使用 Excel 软件或
- 前言本文主要给大家总结介绍了关于Python的一些基础技巧,分享出来供大家参考学习,下面话不多说了,来一起看看详细的介绍吧。1.starts
- 一、re是什么?正则表达式是一个特殊的字符序列,能方便的检查一个字符串是否与某种模式匹配。re模块使得python拥有全部的正则表达式功能。
- 爬虫所需要的功能,基本上在urllib中都能找到,学习这个标准库,可以更加深入的理解后面更加便利的requests库。首先在Pytho2.x
- 本文详细介绍了Windows下安装MySQL5.5.19的全过程,希望对初学者有帮助。下载mysql-5.5.19-win32.msi安装文
- 默认情况下IDE goland 是不支持protobuf协议文件类型".proto"的,为了更快高效的编写proto文件
- 编码问题response = requests.get(URL, params=params, he
- 写入Excel中后有显示第一列客户款号总库存这些,开始写在第12行第一列开始写入,一行写入5个,然后再隔12行,再写入下边的数据,图片需要对
- 在网页中放iframe,如果frameborder=0;就没有边框显示了;但动态创建时,在IE7中就不行了,从网上找到解决的办法,写出来记录
- 一、使用 reflect.Type 创建实例在通过 reflect.TypeOf 函数获取到变量的反射类型对象之后,可以通过反射类型对象 r
- 一、队列基本操作from queue import Queueq = Queue(5) # 创建一个容量为5的队列。如果给一个小
- python中的sys是提供了一系列有关python运行环境的变量和函数的模块,如sys.argv函数实现从程序外部向程序传递参数;sys.
- 一:获取指定文件夹的文件 procedure searchfile(path:string);//注意,path后面要有'\'
- 这篇文章我们学习 Python 变量与数据类型变量变量来源于数学,是计算机语言中能储存计算结果或能表示值抽象概念,变量可以通过变量名访问。在
- 前言本项目为IOT实验室人员签到考勤设计,系统实现功能:1.人员人脸识别并完成签到/签退2.考勤时间计算3.保存考勤数据为CSV格式(Exc
- 下面代码的功能是先训练一个简单的模型,然后保存模型,同时保存到一个pb文件当中,后续可以从pd文件里读取权重值。import tensorf