参考网上的用法,下面是利用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. timing-function: steps()

    animation语法 animation:name duration timing-function delay iteration-count direction timing-function取 ...

  2. 解决 sublime text3 运行python文件无法input的问题

    怎么输入都没有用,原来需要配置可交互环境来运行 首先,Ctrl+Shift+p快捷键,弹出框框输入 install Package,回车后又弹出一个框,输入SublimeREPL(要安装的插件名字), ...

  3. 洛谷 P4238 [模板] 多项式求逆

    题目:https://www.luogu.org/problemnew/show/P4238 看博客:https://www.cnblogs.com/xiefengze1/p/9107752.html ...

  4. 微信开放平台搭建之EasyWeChat

    背景 公司有一个产品,需要用到微信授权登录及微信消息推送等功能.本来能够简单的使用公众号的接口将appid和appsecrect等信息写到配置文件里,但是作为一个产品化的东西,从体验等各方面来讲都不能 ...

  5. 47: error: undefined reference to `QWebView::QWebView(QWidget*)'

    QT  5.6版本 用Qt界面设计器打开界面文件,在界面上托入QWebView控件,这时运行会出现错误,错误如下: ......... undefined reference to `QWebView ...

  6. shuts down an ExecutorService

    shuts down an ExecutorService in two phases, first by calling shutdown to reject incoming tasks, and ...

  7. 一次ssh被植入后门的经历及解决方案

    昨天发现服务器上面很多程序被挂马了,跟开发一起处理了挂马文件,今早发现游戏后台又打不开了,上服务器发现后台程序的入口文件都被删了,恢复了index.php.admin.php后才能正常访问,晚上谁也没 ...

  8. 0005_Linux下的SSH连接操作

    1.启动SSH服务:service sshd start 2.开机自动启动SSH:chkconfig sshd on 3.获取Linux的ip地址:ifconfig 4.连接Linux的SSH:打开x ...

  9. AngularJs(Part 1)

    I am tired to translate these into Chinese. but who cares? i write these posts just for myself Scope ...

  10. Oracle tns 协议

    下面是翻译国外的一篇博客,原文连接如下: https://thesprawl.org/research/oracle-tns-protocol/ 简介 TNS(Transparent Network ...