<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<style type="text/css">
img{
max-width: 400px; }
.select{ }
</style>
</head>
<body>
<div class="head_img pr">
<em class="pa"></em> <div class="select">
选择压缩的文件<input id="photo" type="file" accept="image/*" />
</div>
<a href="" download="" id="down">
<img src='' alt="" class="modify_img" />
</a>
</div>
</body>
<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript">
$('#photo').change(function(){
var _this = $(this)[0],
_file = _this.files[0],
fileType = _file.type;
console.log(_file.size);
if(/image\/\w+/.test(fileType)){ var fileReader = new FileReader();
fileReader.readAsDataURL(_file);
fileReader.onload = function(event){
var result = event.target.result; //返回的dataURL
var image = new Image();
image.src = result;
image.onload = function(){ //创建一个image对象,给canvas绘制使用
var cvs = document.createElement('canvas');
var scale = 0.8;
if(this.width > 1000 || this.height > 1000){ //1000只是示例,可以根据具体的要求去设定
if(this.width > this.height){
scale = 1000 / this.width;
}else{
scale = 1000 / this.height;
}
}
cvs.width = this.width*scale;
cvs.height = this.height*scale; //计算等比缩小后图片宽高
var ctx = cvs.getContext('2d');
ctx.drawImage(this, 0, 0, cvs.width, cvs.height);
var newImageData = cvs.toDataURL(fileType, 0.8); //重新生成图片,<span style="font-family: Arial, Helvetica, sans-serif;">fileType为用户选择的图片类型</span>
var sendData = newImageData.replace("data:"+fileType+";base64,",'');
$('.modify_img').attr('src',newImageData);
$("#down").attr("href",newImageData);
} }
}else{
$.notify.show('请选择图片格式文件', {placement: 'center'});
}
});
</script>
</html>

  

js图片压缩的更多相关文章

  1. 使用ajax上传图片,支持图片即时浏览,支持js图片压缩后上传给服务器

    使用ajax上传图片,支持图片即时浏览,支持js图片压缩后上传给服务器 ajax上传主要使用了 var reader = new FileReader() 此方法 js图片压缩主要是利用canvas进 ...

  2. Angular里使用(image-compressor.js)图片压缩

    参考资料: http://www.imooc.com/article/40038 https://github.com/xkeshi/image-compressor 示例代码: <nz-upl ...

  3. js图片压缩和上传并显示

    由于近期项目中需要做个图片压缩上传,所以就在网上找了些资料自己写了一个 html部分 <input id="file" type="file"> & ...

  4. js 图片压缩上传(base64位)以及上传类型分类

    一.input file上传类型 1.指明只需要图片 <input type="file" accept='image/*'> 2.指明需要多张图片 <input ...

  5. js图片压缩+ajax上传

    图片压缩用到了localresizeimg 地址: https://github.com/think2011/localResizeIMG 用起来比较简单 <input type="f ...

  6. JS—图片压缩上传(单张)

    *vue+webpack环境,这里的that指到vue实例 <input type="file" name="file" accept="ima ...

  7. 无组件客户端js图片压缩

    <div class="free-upload"> <p>上传您的约会照片,一张合影.一张票据哦!</p> <div class=&quo ...

  8. js 图片压缩上传(纯js的质量压缩,非长宽压缩)

    下面是大神整理的demo,很实用,这里存一下备用,感谢大神! 此demo为大于1M对图片进行压缩上传 若小于1M则原图上传,可以根据自己实际需求更改. demo源码如下 <!DOCTYPE ht ...

  9. js图片压缩上传

    最近公司的移动产品相约app要做一次活动,涉及到图片上传,图片又不能太大,不然用户体验太差,必须先压缩再上传,所以用到了html5的canvas和FileReader,代码先上,小弟前端经验不足,代码 ...

随机推荐

  1. ThinkPHP框架 系统规定的方法查询数据库内容!!同时也支持原生的SQL语句!

    <?php namespace Admin\Controller; use Think\Controller; class MainController extends Controller{ ...

  2. 怎么关闭win10防火墙

    reg add "HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Microsoft\Windows Defender" /v "Disable ...

  3. cuteftp9破解及安装、使用

    一.破解: 参考:https://jingyan.baidu.com/article/ca00d56c4e43b2e99febcf70.html 1. 首先下载cuteftp替换文件及cuteftp9 ...

  4. MSSQL 将大表改成已分区表

    --select * from sys.partition_functions --select * from sys.partition_range_values use [UpdateLog] g ...

  5. {Django基础八之cookie和session}一 会话跟踪 二 cookie 三 django中操作cookie 四 session 五 django中操作session

    Django基础八之cookie和session 本节目录 一 会话跟踪 二 cookie 三 django中操作cookie 四 session 五 django中操作session 六 xxx 七 ...

  6. [No0000D4]批处理全部代码详解Allbat

    COPY REM Copies one or more files from one location to another. REM [/d] - Allows the encrypted file ...

  7. Deck of Cards ZOJ - 2852 dp 多决策 三维 滚动更新

    题意:一个特殊21点游戏 具体http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2852 题解:建一个三维dp,表示三个卡槽分别 ...

  8. cocoapod引入FLEX,debug模式正常,Release报错library not found for -lXXX

    cocoapod引入FLEX,debug模式正常,Release报错library not found for -lXXX, 因为podfile是这么写的: pod 'FLEX', '~> 2. ...

  9. scala-LinkedList

    LinkedList每隔元素乘以3: import scala.collection.mutable.LinkedList var list1=LinkedList.apply(1,2,3,4,5) ...

  10. You-Get——基于Python3的媒体下载工具

    You-Get是一个基于 Python 3 的下载工具.使用 You-Get 可以很轻松的下载到网络上的视频.图片及音乐. 项目主页:https://github.com/soimort/you-ge ...