HTML5 input file 图片上传,压缩,上传,预览
2017-07-03 15:29
405 查看
<input type="file" id="textfile" accept="image/*"/>上传
<div class="upP_img1"></div>预览框
$('#textfile').on('change', function() {
var reader = new FileReader();//新建获取file的读取文件
var imgsrc = null;
var _file = this.files[0], //获取的的图片
fileType = _file.type; //图片类型
console.log(fileType);
reader.readAsDataURL(this.files[0]);//输出base64图片
reader.onload = function(e) {//字面理解是加载图片,得到结果吧,不是很理解
imgsrc = this.result;//输出结果
// 压缩
var image=new Image();//新建图片
image.src=imgsrc;
image.onload=function(){
var cvs=document.createElement('canvas');//画布
var cvx =cvs.getContext('2d');//
// draw image params
cvs.width=this.width;
cvs.height=this.height;
cvx.drawImage(this, 0, 0,this.width,this.height);//画图
var newImageData = cvs.toDataURL(fileType,0.2);//这是压缩,具体的看.toDataURL api 输出base64
console.log(newImageData);
$('.upP_img1').html('<img id="newimg" src="' + newImageData + '">');
console.log(this.width+'--'+this.height);
// 上传图片后的以宽高充满判断
var imgscale2 = this.width / this.height;
var photoscale2 = $('.upP_img1').width() /$('.upP_img1').height();
if (imgscale2 > photoscale2) {
$('#newimg').css({ "width": "100%", "height": "auto" });
} else {
$('#newimg').css({ "width": "auto", "height": "100%" });
}
}
}
});
$.ajax({
url:url,
type:post,
data:JSON.stringify($('#newimg').attr('src')),success:function(){},error:function(){}
});
值得一提的是,canvs画出来的图片比上传的原图片要大,39k的原图画出来有180k 这是最骚的,5m压缩20%出来有500k。
http://www.zhangxinxu.com/study/201707/js-compress-image-before-upload.html这是别人的专业答案
<div class="upP_img1"></div>预览框
$('#textfile').on('change', function() {
var reader = new FileReader();//新建获取file的读取文件
var imgsrc = null;
var _file = this.files[0], //获取的的图片
fileType = _file.type; //图片类型
console.log(fileType);
reader.readAsDataURL(this.files[0]);//输出base64图片
reader.onload = function(e) {//字面理解是加载图片,得到结果吧,不是很理解
imgsrc = this.result;//输出结果
// 压缩
var image=new Image();//新建图片
image.src=imgsrc;
image.onload=function(){
var cvs=document.createElement('canvas');//画布
var cvx =cvs.getContext('2d');//
// draw image params
cvs.width=this.width;
cvs.height=this.height;
cvx.drawImage(this, 0, 0,this.width,this.height);//画图
var newImageData = cvs.toDataURL(fileType,0.2);//这是压缩,具体的看.toDataURL api 输出base64
console.log(newImageData);
$('.upP_img1').html('<img id="newimg" src="' + newImageData + '">');
console.log(this.width+'--'+this.height);
// 上传图片后的以宽高充满判断
var imgscale2 = this.width / this.height;
var photoscale2 = $('.upP_img1').width() /$('.upP_img1').height();
if (imgscale2 > photoscale2) {
$('#newimg').css({ "width": "100%", "height": "auto" });
} else {
$('#newimg').css({ "width": "auto", "height": "100%" });
}
}
}
});
$.ajax({
url:url,
type:post,
data:JSON.stringify($('#newimg').attr('src')),success:function(){},error:function(){}
});
值得一提的是,canvs画出来的图片比上传的原图片要大,39k的原图画出来有180k 这是最骚的,5m压缩20%出来有500k。
http://www.zhangxinxu.com/study/201707/js-compress-image-before-upload.html这是别人的专业答案
相关文章推荐
- 通过HTML5 FileReader实现上传图片预览功能
- html,图片上传预览,input file获取文件等相关操作
- 上传图片预览JS脚本 Input file图片预览的实现示例
- HTML5图片上传图片实时预览,通过DataURL将图片压缩“嵌入”到HTML中,优化图片的加载速度和执行效率。
- input file文件上传图片,添加预览效果
- 纯CSS定义input file样式及上传图片预览 CSS
- javascript实现input file上传图片预览效果
- input file上传 图片预览功能实例代码
- html5手机 input file 上传图片 调用API
- bootstrap 文件上传插件 fileinput.min.js 可以预览图片
- html5 图片上传,支持图片预览、压缩、及进度显示,兼容IE6+及标准浏览器
- html5的FileReader实现图片上传预览并生成base64
- 上传图片预览JS脚本 Input file图片预览的实现示例
- html5手机 input file 上传图片 调用API
- html5 图片上传,支持图片预览、压缩、及进度显示,兼容IE6+及标准浏览器
- 简单获取input file 选中的图片,并在一个div的img里面赋值src实现预览图片(可实现多张图片上传和预览移除)
- javascript实现input file上传图片预览效果
- 通过HTML5 FileReader实现上传图片预览功能
- 一段上传图片预览JS脚本,Input file图片预览的实现
- 通过HTML5 FileReader实现上传图片预览功能