上传base64图片并压缩
elementUI+react
布局
<Dialog
title="充值"
visible={ dialogVisible }
onCancel={ () => this.setState({ dialogVisible: false }) }
>
<Dialog.Body>
<Form labelWidth="120" ref={ e => {this.form=e} } model={ form } rules={ rules }>
<Form.Item label="支付凭证" prop="voucher">
<div className="my-upload">
<input className="upload-input" type="file" id="input" name="file1" onChange={ this.inputfile } />
{ voucher ? <img src={ voucher } className="avatar" alt="" /> : <i className="el-icon-plus avatar-uploader-icon" /> }
</div>
</Form.Item>
</Form>
</Dialog.Body>
<Dialog.Footer className="dialog-footer">
<Button onClick={ () => this.setState({ dialogVisible: false }) }>{'取 消'}</Button>
<Button type="primary" onClick={ this.saveContent } loading={ btnLoading }>{'确 定'}</Button>
</Dialog.Footer>
</Dialog>
完整方法:
beforeAvatarUpload(file) {
const isJPG = file.type === 'image/jpeg'
const isLt2M = file.size / 1024 / 1024 < 2
if (!isJPG) {
Message('上传头像图片只能是 JPG 格式!')
}
if (!isLt2M) {
Message('上传头像图片大小不能超过 2MB!')
}
return isJPG && isLt2M
}
// input onchange上传方法
inputfile =()=> {
const file1 = document.querySelector('#input').files[0]
// const flag = this.beforeAvatarUpload(file1)
// if(!flag){
// return false
// }
console.log(file1)
const that=this
if(file1){
var reader = new FileReader()
// 图片文件转换为base64
reader.readAsDataURL(file1)
reader.onload = function(){
// 显示图片
// document.getElementById('file1_img').src = this.result
// that.setState({
// voucher:this.result
// })
that.dealImage(this.result, 800, that.printing)
}
}
}
//回调方法
printing = base64 => {
// console.log('压缩后', base64.length / 1024)
this.setState({
voucher: base64
})
}
//压缩方法
dealImage = (base64, w, callback) => {
var newImage = new Image()
//压缩系数0-1之间
var quality = 0.6
newImage.src = base64
newImage.setAttribute('crossOrigin', 'Anonymous') //url为外域时需要
var imgWidth, imgHeight
newImage.onload = function () {
imgWidth = this.width
imgHeight = this.height
var canvas = document.createElement('canvas')
var ctx = canvas.getContext('2d')
if (Math.max(imgWidth, imgHeight) > w) {
if (imgWidth > imgHeight) {
canvas.width = w
canvas.height = w * imgHeight / imgWidth
} else {
canvas.height = w
canvas.width = w * imgWidth / imgHeight
}
} else {
canvas.width = imgWidth
canvas.height = imgHeight
quality = 0.6
}
ctx.clearRect(0, 0, canvas.width, canvas.height)
ctx.drawImage(this, 0, 0, canvas.width, canvas.height)
var ba = canvas.toDataURL('image/jpeg', quality) //压缩语句
// 如想确保图片压缩到自己想要的尺寸,如要求在50-150kb之间,请加以下语句,quality初始值根据情况自定
// while (base64.length / 1024 > 150) {
// quality -= 0.01;
// base64 = canvas.toDataURL("image/jpeg", quality);
// }
// 防止最后一次压缩低于最低尺寸,只要quality递减合理,无需考虑
// while (base64.length / 1024 < 50) {
// quality += 0.001;
// base64 = canvas.toDataURL("image/jpeg", quality);
// }
callback(ba) //必须通过回调函数返回,否则无法及时拿到该值
}
}
上传base64图片并压缩的更多相关文章
- 上传base64图片到七牛云前端遇到的坑
介意前端普通引入七牛云SDk上传图片到七牛云需要多个js,所以才有了base64的上传方式,简化操作,(懒.) 七牛云官方文档如下 https://developer.qiniu.com/kodo/k ...
- 基于vue上传base64图片,通过canvas压缩base64
其实和vue关系不大,和我们之前做上传压缩性质是一样的 当然下面的代码是没有处理ios横屏拍照的bug的 有兴趣的可以多搜一下 网上都有相应的解答 .. var that = this if (e. ...
- 上传base64图片至七牛云,并返回图片link
https://developer.qiniu.com/kodo/kb/1326/how-to-upload-photos-to-seven-niuyun-base64-code
- Vue directive自定义指令+canvas实现H5图片压缩上传-Base64格式
前言 最近优化项目-手机拍照图片太大,回显速度比较慢,使用了vue的自定义指令实现H5压缩上传base64格式的图片 canvas自定义指令 Vue.directive("canvas&qu ...
- vue实现PC端调用摄像头拍照人脸录入、移动端调用手机前置摄像头人脸录入、及图片旋转矫正、压缩上传base64格式/文件格式
进入正题 1. PC端调用摄像头拍照上传base64格式到后台,这个没什么花里胡哨的骚操作,直接看代码 (canvas + video) <template> <div> &l ...
- weui上传多图片,前端压缩,base64编码
记录一下在做一个报修功能的心路历程,需求功能很简单,一个表单提交,表单包含简单的文字字段以及图片 因为使用的是weui框架,前面的话去找weui的表单和图片上传组件,说实话,weui的组件写的还不错, ...
- 移动端图片上传解决方案localResizeIMG先压缩后ajax无刷新上传
现在科技太发达,移动设备像素越来越高,随便一张照片2M+,但是要做移动端图片上传和pc上略有不同,移动端你不能去限制图片大小,让用户先处理图片再上传,这样不现实.所以理解的解决方案就是在上传先进行图片 ...
- 前端上传 base64 编码图片到七牛云存储
参考文档 如何上传base64编码图片到七牛云 调试过程 文档中分别有 java 和 html 的 demo,可以根据文档示例调试. 下面是我调试的过程,可以作为参考,特别注意的是,如果需要给文件起名 ...
- 上传base64格式的图片到服务器
上传base64格式的图片到服务器 /**bash64上传图片 * @param $base64 图片的base64数据 * @param $path 保存路径 */ function base64_ ...
随机推荐
- 21.centos7基础学习与积累-007-远程连接
从头开始积累centos7系统运用 大牛博客:https://blog.51cto.com/yangrong/p5 IP地址: 互联网上的计算机 都会有一个唯一的32位的地址,ip地址,我们访问服务器 ...
- 日志聚合工具loki
目录 1.loki是什么 2.loki特点 3.loki组成 4.loki安装 4.1.添加helm的chart库 4.2.安装loki及promtail 4.3.安装grafana 5.配置和使用 ...
- linux查看redis安装目录
1.在redis下查看安装目录 如果命令 which 和whereis 都找不到安装目录,可使用以下办法 ps -ef|grep redis 得到了进程号 xxxx 然后 ls -l /proc/xx ...
- JAVA设计模式之工厂模式—Factory Pattern
1.工厂模式简介 工厂模式用于对象的创建,使得客户从具体的产品对象中被解耦. 2.工厂模式分类 这里以制造coffee的例子开始工厂模式设计之旅. 我们知道coffee只是一种泛举,在点购咖啡时需要指 ...
- CentOS7 离线安装mysql-5.7.16
CentOS7 离线安装mysql-5.7.16 1 . 安装新版mysql前,需将系统自带的mariadb-lib卸载 [root@slave mytmp]# rpm -qa|grep mariad ...
- Codeforces D. Intercity Travelling(区间组合)
题目描述: D. Intercity Travelling time limit per test 1.5 seconds memory limit per test 256 megabytes in ...
- selenium与webdriver驱动与firefox、 chrome匹配版本
一.Chrome python3 selenium3.11.0(直接pip安装即可) chromedriver_win32:2.38 Chrome版本: 65.0.3325.146(正式版本)(32 ...
- sql中如何获取一条数据中所有字段的名称和值
declare ) ) --获取表的列名 ,),filename INTO #templist FROM (select cl.name as filename from sys.tables AS ...
- Python开发应用-正则表达进行排序搜索
re模块提供了3个方法对输入的字符串进行确切的查询,match和search最多只会返回一个匹配条件的子串,可以理解为非贪婪模式,而findall会返回N个匹配条件的子串,可以理解为贪婪模式 re.m ...
- CodeForces - 83D:Numbers (数学&递归 - min25筛 )
pro:给定三个整数L,R,P求[L,R]区间的整数有多少个是以P为最小因子的.L,R,P<2e9; sol: 一: 比较快的做法是,用函数的思想递归. 用solve(N,P)表示求1到N有多少 ...