参考网上的用法,下面是利用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前端压缩图片的更多相关文章

  1. canvas前端压缩图片和视频首屏缩略图并上传到服务器

    图片: var img = document.createElement('img') img.src = window.URL.createObjectURL(fileObj.file) // 加载 ...

  2. js使用canvas在前端压缩图片

    HTML代码: <input id="file" type="file"> JS代码: var eleFile = document.querySe ...

  3. antdv的Upload组件实现前端压缩图片并自定义上传功能

    Ant Design of Vue的Upload组件有几个重要的api属性: beforeUpload: 上传文件之前的钩子函数,支持返回一个Promise对象. customRequest: 覆盖组 ...

  4. Vue前端压缩图片

    一.在组件包下新建compressImage.js // 压缩图片 // eslint-disable-next-line no-unused-vars export function compres ...

  5. HTML5 Canvas前台压缩图片并上传到服务器

    1.前台代码: <input id="fileOne" type="file" /> <input id="btnOne" ...

  6. 利用 canvas 实现压缩图片

    /** * nase64Data --> 要压缩的图片base64数据 * width --> 宽度 * height --> 高度 * _callback --> 回调函数 ...

  7. 结合Vue.js的前端压缩图片方案

    这是一个很简单的方案.嗯,是真的. 为什么要这么做? 在移动Web蓬勃发展的今天,有太多太多的应用需要让用户在移动Web上传图片文件了,正因如此,我们有些困难必须去攻克: 低网速下上传进度缓慢,用户体 ...

  8. 前端压缩图片,前端压缩图片后转换为base64.

    今天利用一上午研究了一下前端如何将5m左右的照片转换base64大小为 100k以内! 有两个链接:https://www.cnblogs.com/007sx/p/7583202.html :http ...

  9. php canvas 前端JS压缩,获取图片二进制流数据并上传

    <?php if(isset($_GET['upload']) && $_GET['upload'] == 'img'){ //二进制数据流 $data = file_get_c ...

随机推荐

  1. PyNLPIR python中文分词工具

    官网:https://pynlpir.readthedocs.io/en/latest/  github:https://github.com/tsroten/pynlpir          NLP ...

  2. linux 进程学习笔记-named pipe (FIFO)命名管道

    与“无名管道”不同的是,FIFO拥有一个名称来标志它,所谓的名称实际上就是一个路径,比如“/tmp/my_fifo”,其对应到磁盘上的一个管道文件,如果我们用file命令来查看其文件类型的话,会得到如 ...

  3. 白痴qwerta的胡言乱语(一句话日度感想?

    10.2 >我tm吹爆这个Latex插件!!!太漂亮了吧?!!! 10.3 >什么鬼气考试 还有这考试的大家也肽水了吧 >再吹一次这个Latex插件!!! 10.6 >今天在琢 ...

  4. android自动连接指定wifi

    public class WifiAutoConnectManager { private static final String TAG = WifiAutoConnectManager.class ...

  5. LMV实验

    在CentOS6.8上创建过程如下: #对需要创建lvm的卷进行磁盘分区[root@www ~13:40:48]#fdisk /dev/sde Device contains neither a va ...

  6. 微软 codeplex 团队

    http://www.codeplex.com/site/users/view/Microsoft

  7. java之字符串转换

    参考http://how2j.cn/k/number-string/number-string-parse/317.html 数字转字符串 方法1: 使用String类的静态方法valueOf 方法2 ...

  8. Hibernate的auto-import属性详解

    auto-import是什么意思呢? 我们经常会写这样一个HQL语句: from User u where u.name='罗灿锋'; 绝大多数时候,这样写是不会发生问题的. hibernate在处理 ...

  9. centos6.5编译安装php7,及配置与nginx通信。

    一.配置编译环境 yum update && yum upgrade yum groupinstall "Development Tools" yum instal ...

  10. VC OnCtlColor函数来修改控件背景颜色

    CWnd::OnCtlColor afx_msg HBRUSH OnCtlColor( CDC* pDC, CWnd* pWnd, UINT nCtlColor ); 返回值:OnCtlColor必须 ...