base64图片压缩
来源:互联网 发布:php扩展加密 编辑:程序博客网 时间:2024/05/29 13:17
/****压缩走的路线***/
function dealImage(re,w,callback){
var newImage=new Image();
var quality=0.7;
newImage.src=re;
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 base64=canvas.toDataURL("image/jpeg",quality);
callback(base64);//必须通过毁掉函数返回,否则无法及时拿到改值
}
}
//使用时
dealImage(e.target.result,800,next);
function next(base64){
//这里写一些业务逻辑,同时把base64赋值给后台接口
}
function dealImage(re,w,callback){
var newImage=new Image();
var quality=0.7;
newImage.src=re;
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 base64=canvas.toDataURL("image/jpeg",quality);
callback(base64);//必须通过毁掉函数返回,否则无法及时拿到改值
}
}
//使用时
dealImage(e.target.result,800,next);
function next(base64){
//这里写一些业务逻辑,同时把base64赋值给后台接口
}
阅读全文
0 0
- base64图片压缩
- js压缩图片base64长度
- 图片压缩,Base64编码后上传服务器
- js图片转base64并压缩
- js图片转base64编码压缩上传
- android 图片转Base64压缩上传
- 图片转Base64并压缩,解压
- Android端将图片压缩为Base64
- 使用图片的base64编码字符串压缩图片代码
- Android 图片压缩图片转Base64工具类
- 图片上传-form表单还是base64-前端图片压缩
- 前台图片压缩&转换base64&提交到后台
- 微信手机网页上传图片高效率压缩(Canvas+Base64)
- Android 压缩图片并转Base64方法之一
- JS压缩图片(canvas),返回base64码
- 微信端图片压缩转base64,然后转file形式上传
- ci框架经canvas压缩图片后经base64上传
- Android 图片先gzip压缩然后在Base64转成字符串
- View的事件传递机制
- 反编译工具ILSpy下载
- @Controller和@RestController的区别?
- 6-1 PTA 单链表逆转
- Restful API SpringMVC多版本实现
- base64图片压缩
- 链式存储结构的特点
- css控制元素垂直居中的几种方法
- 自定义TextView任意长度文本下自动改变文字大小以完全显示
- Java设计模式(六) 代理模式 vs. 装饰模式
- Windows 日志查看工具
- 改自CSDN提供的markdown编辑器进行详细解答
- 解决Android studio 编译报错 Error:null value in entry: outputDirectory=null
- RedisDesktopManager远程连接失败的解决办法