canvas前端压缩图片
参考网上的用法,下面是利用canvas进行的图片压缩
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta content="width=device-width,initial-scale=1.0,maximum-scale=1.0,user-scalable=no" name="viewport">
<meta content="yes" name="apple-mobile-web-app-capable">
<meta content="black" name="apple-mobile-web-app-status-bar-style">
<meta content="telephone=no" name="format-detection">
<meta content="email=no" name="format-detection">
<title>slip</title>
<style>
*{padding: ;margin: ;} </style> </head> <body>
<div class="input">
<input type="file" id="file" accept="image/*">
</div> </body> <script type="text/javascript"> var img=new Image();
var reader=new FileReader();
// 选择的文件对象
var file = null;
var eleFlile=document.querySelector('#file');
var canvas=document.createElement('canvas');
var context=canvas.getContext('2d');
img.onload=function () {
//图片原始大小
var originWidth = this.width;
var originHeight = this.height;
//限制图片大小
var maxWidth = ;
var maxHeight = ; var targetWidth = originWidth, targetHeight = originHeight;
if (originHeight > maxHeight || originWidth > maxWidth) {
if (originWidth / originHeight > maxWidth / maxHeight) {
//更宽
targetWidth = maxWidth;
targetHeight = Math.round(maxWidth * (originHeight / originWidth));
} else {
//更高
targetHeight = maxHeight;
targetWidth = Math.round(targetHeight * (originWidth / originHeight))
}
}
//图片缩放
canvas.width = targetWidth;
canvas.height = targetHeight;
//清除画布
context.clearRect(, , targetWidth, targetHeight);
//图片压缩
context.drawImage(img, , , targetWidth, targetHeight);
//转换上传toBlob
canvas.toBlob(function (blob) {
//图片ajax上传
var xhr=new XMLHttpRequest();
//文件上传成功
xhr.onreadystatechange=function () {
if(xhr.status==){
alert("上传成功")
}
};
xhr.open("POST","upload.php",true);
xhr.send(bolb);
} ,file.type || 'image/png');
var newImg = document.createElement("img"),
url = URL.createObjectURL(blob); newImg.onload = function () {
// no longer need to read the blob so it's revoked
URL.revokeObjectURL(url);
}; newImg.src = url;
document.body.appendChild(newImg); // })
//图片上传toDataURL
var url=canvas.toDataURL();
var newImg = document.createElement("img")
newImg.src = url;
document.body.appendChild(newImg);
}
//文件base64化
reader.onload=function (e) {
img.src=e.target.result;
};
eleFlile.addEventListener("change",function (event) { file=event.target.files[];
if(file.type.indexOf("image")==){
reader.readAsDataURL(file);
}else {
alert("请传图片")
}
})
</script> </html>
canvas前端压缩图片的更多相关文章
- canvas前端压缩图片和视频首屏缩略图并上传到服务器
图片: var img = document.createElement('img') img.src = window.URL.createObjectURL(fileObj.file) // 加载 ...
- js使用canvas在前端压缩图片
HTML代码: <input id="file" type="file"> JS代码: var eleFile = document.querySe ...
- antdv的Upload组件实现前端压缩图片并自定义上传功能
Ant Design of Vue的Upload组件有几个重要的api属性: beforeUpload: 上传文件之前的钩子函数,支持返回一个Promise对象. customRequest: 覆盖组 ...
- Vue前端压缩图片
一.在组件包下新建compressImage.js // 压缩图片 // eslint-disable-next-line no-unused-vars export function compres ...
- HTML5 Canvas前台压缩图片并上传到服务器
1.前台代码: <input id="fileOne" type="file" /> <input id="btnOne" ...
- 利用 canvas 实现压缩图片
/** * nase64Data --> 要压缩的图片base64数据 * width --> 宽度 * height --> 高度 * _callback --> 回调函数 ...
- 结合Vue.js的前端压缩图片方案
这是一个很简单的方案.嗯,是真的. 为什么要这么做? 在移动Web蓬勃发展的今天,有太多太多的应用需要让用户在移动Web上传图片文件了,正因如此,我们有些困难必须去攻克: 低网速下上传进度缓慢,用户体 ...
- 前端压缩图片,前端压缩图片后转换为base64.
今天利用一上午研究了一下前端如何将5m左右的照片转换base64大小为 100k以内! 有两个链接:https://www.cnblogs.com/007sx/p/7583202.html :http ...
- php canvas 前端JS压缩,获取图片二进制流数据并上传
<?php if(isset($_GET['upload']) && $_GET['upload'] == 'img'){ //二进制数据流 $data = file_get_c ...
随机推荐
- hdu-5805 NanoApe Loves Sequence(线段树+概率期望)
题目链接: NanoApe Loves Sequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 262144/131072 ...
- CentOS 性能监控之nmon
工具集: Nmon 性能数据收集分析工具Nmon analyser 性能数据分析工具,excel文件nmon_x86_sles10 Nmon在x86_sles10下二进制执行文件 nmon概述 n ...
- liunx命令之:命令链接ftp服务器
1. 连接ftp服务器 格式:ftp [hostname| ip-address]a)在linux命令行下输入: ftp 192.168.1.1 b)服务器询问你用户名和密码,分别输入用户名和相应密码 ...
- ACM学习历程——HDU2227 Find the nondecreasing subsequences(线段树 && dp)
Description How many nondecreasing subsequences can you find in the sequence S = {s1, s2, s3, ...., ...
- 理解I/O Completion Port(完成端口)
欢迎阅读此篇IOCP教程.我将先给出IOCP的定义然后给出它的实现方法,最后剖析一个Echo程序来为您拨开IOCP的谜云,除去你心中对IOCP的烦恼.OK,但我不能保证你明白IOCP的一切,但我会尽我 ...
- bzoj 1202 [HNOI2005]狡猾的商人——带偏移量的并查集
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1202 带偏移量的并查集. 注意先 find() 再调用 dis !!! 自己的对拍太水了. ...
- TFS 备注
1,更改任何文件, 先checkout, 再继续更改. 2. 更新sln时, 一定要更新include文件 3. 每次提交代码放到shelf上, 自己本地建立2个workspace, 来进行coder ...
- zk 04之 Zookeeper Api(java)与应用
如何使用 Zookeeper 作为一个分布式的服务框架,主要用来解决分布式集群中应用系统的一致性问题,它能提供基于类似于文件系统的目录节点树方式的数据存储,但是 Zookeeper 并不是用来专门存储 ...
- Redis实现求交集操作结果缓存的设计方案
Redis的集合操作 实话说,Redis提供的集合操作是我选择它成为内存数据库的一个主要理由,它弥补了传统关系型数据库在这方面带来的复杂度,使得只需要简单的一个命令就可以完成一个复杂SQL任务,并且交 ...
- 在VS中对WCF服务进行更新,但是配置文件没有更新解决办法
取消下面的勾选框