JS实现按比例缩小图片宽高


Posted in Javascript onAugust 24, 2020

本文实例为大家分享了JS实现按比例缩小图片宽高的具体代码,供大家参考,具体内容如下

<!DOCTYPE html>
<html>
<head>
 <title>JS 按比例缩小图片宽高</title>
</head>
 
<body>
 <div>
 <input type="file" name="" id="upload">
 <img src="" alt="" id="preview">
 </div>
</body>
<script>
 var upd =document.getElementById('upload');
 upd.addEventListener('change',function(e){
 var file=e.target.files[0]; 
 var reader=new FileReader();
 var img = document.createElement('img');
 var canvas=document.createElement('canvas');
 var context=canvas.getContext('2d'); 
 
 reader.onload=function(e){ 
 img.src = e.target.result;
 img.onload = function () {
 var imgWidth=this.width;//上传图片的宽
 var imgHeight = this.height;//上传图片的高 
 //按比例缩放后图片宽高 
 var targetWidth = imgWidth;
 var targetHeight = imgHeight; 
 var maxWidth=1920;//图片最大宽
 var maxHeight = 1080;//图片最大高 
 var scale = imgWidth / imgHeight;//原图宽高比例
 
 //如果原图宽大于最大宽度
 if(imgWidth>maxWidth){
 targetWidth = maxWidth;
 targetHeight = targetWidth/scale;
 }
 //缩放后高度仍然大于最大高度继续按比例缩小
 if(targetHeight>maxHeight){
 targetHeight = maxHeight
 targetWidth = targetHeight*scale;
 }
 canvas.width=targetWidth;//canvas的宽=图片的宽
 canvas.height=targetHeight;//canvas的高=图片的高
 
 context.clearRect(0,0,targetWidth,targetHeight)//清理canvas
 context.drawImage(img,0,0,targetWidth,targetHeight)//canvas绘图
 var newUrl=canvas.toDataURL('image',0.92);//canvas导出成为base64
 preview.src=newUrl
 }
 }
 reader.readAsDataURL(file);
 })
</script>
 
</html>

小编再为大家分享一段:图片按宽高比例进行自动缩放代码

/**
 * 图片按宽高比例进行自动缩放
 * @param ImgObj
 * 缩放图片源对象
 * @param maxWidth
 * 允许缩放的最大宽度
 * @param maxHeight
 * 允许缩放的最大高度
 * @usage 
 * 调用:<img src="图片" onload="javascript:DrawImage(this,100,100)">
 */
function DrawImage(ImgObj, maxWidth, maxHeight){
 var image = new Image();
 //原图片原始地址(用于获取原图片的真实宽高,当<img>标签指定了宽、高时不受影响)
 image.src = ImgObj.src;
 // 用于设定图片的宽度和高度
 var tempWidth;
 var tempHeight;
 
 if(image.width > 0 && image.height > 0){
 //原图片宽高比例 大于 指定的宽高比例,这就说明了原图片的宽度必然 > 高度
 if (image.width/image.height >= maxWidth/maxHeight) {
  if (image.width > maxWidth) {
  tempWidth = maxWidth;
  // 按原图片的比例进行缩放
  tempHeight = (image.height * maxWidth) / image.width;
  } else {
  // 按原图片的大小进行缩放
  tempWidth = image.width;
  tempHeight = image.height;
  }
 } else {// 原图片的高度必然 > 宽度
  if (image.height > maxHeight) { 
  tempHeight = maxHeight;
  // 按原图片的比例进行缩放
  tempWidth = (image.width * maxHeight) / image.height; 
  } else {
  // 按原图片的大小进行缩放
  tempWidth = image.width;
  tempHeight = image.height;
  }
 }
 // 设置页面图片的宽和高
 ImgObj.height = tempHeight;
 ImgObj.width = tempWidth;
 // 提示图片的原来大小
 ImgObj.alt = image.width + "×" + image.height;
 }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。 

Javascript 相关文章推荐
javascript-TreeView父子联动效果保持节点状态一致
Aug 12 Javascript
Jquery 基础学习笔记
May 29 Javascript
js jquery数组介绍
Jul 15 Javascript
使用JavaScript的AngularJS库编写hello world的方法
Jun 23 Javascript
全面解析Bootstrap排版使用方法(标题)
Nov 30 Javascript
微信小程序 toast 详解及实例代码
Nov 09 Javascript
H5移动端图片压缩上传开发流程
Nov 09 Javascript
jQuery实现淡入淡出的模态框
Feb 09 Javascript
十大 Node.js 的 Web 框架(快速提升工作效率)
Jun 30 Javascript
vue-quill-editor实现图片上传功能
Aug 08 Javascript
vue-cli3.0实现一个多页面应用的历奇经历记录总结
Mar 16 Javascript
Vue 禁用浏览器的前进后退操作
Sep 04 Javascript
JS实现简易贪吃蛇游戏
Aug 24 #Javascript
通过高德地图API获得某条道路上的所有坐标用于描绘道路的方法
Aug 24 #Javascript
Node.js 中判断一个文件是否存在
Aug 24 #Javascript
Javascript中Math.max和Math.max.apply的区别和用法详解
Aug 24 #Javascript
在Vue中使用HOC模式的实现
Aug 23 #Javascript
详解Howler.js Web音频播放终极解决方案
Aug 23 #Javascript
利用React高阶组件实现一个面包屑导航的示例
Aug 23 #Javascript
You might like
这部番真是良心,画质好到像风景区,剧情让人跟着小公会热血沸腾
2020/03/10 日漫
SSI指令
2006/11/25 PHP
phpQuery占用内存过多的处理方法
2013/11/13 PHP
smarty模板引擎中内建函数if、elseif和else的使用方法
2015/01/22 PHP
PHP使用curl_multi实现并发请求的方法示例
2018/04/29 PHP
jquery checkbox全选、取消全选实现代码
2010/03/05 Javascript
JS正则表达式大全(整理详细且实用)
2013/11/14 Javascript
Ubuntu系统下Angularjs开发环境安装
2016/09/01 Javascript
jQuery动态添加与删除tr行实例代码
2016/10/18 Javascript
jQuery模拟下拉框选择对应菜单的内容
2017/03/07 Javascript
详解如何使用vue-cli脚手架搭建Vue.js项目
2017/05/19 Javascript
zTree节点文字过多的处理方法
2017/11/24 Javascript
vue-router重定向不刷新问题的解决
2018/06/25 Javascript
Angular angular-file-upload文件上传的示例代码
2018/08/23 Javascript
layer更改皮肤的实现方法
2019/09/11 Javascript
python实现迭代法求方程组的根过程解析
2019/11/25 Javascript
JavaScript中的Proxy对象
2020/11/27 Javascript
[00:49]完美世界DOTA2联赛10月28日开团时刻:随便打
2020/10/29 DOTA
[01:42:49]DOTA2-DPC中国联赛 正赛 iG vs PSG.LGD BO3 第一场 2月26日
2021/03/11 DOTA
Python排序算法之选择排序定义与用法示例
2018/04/29 Python
Python实现去除图片中指定颜色的像素功能示例
2019/04/13 Python
Python3+Pycharm+PyQt5环境搭建步骤图文详解
2019/05/29 Python
PyQt5 加载图片和文本文件的实例
2019/06/14 Python
Python+OpenCV实现实时眼动追踪的示例代码
2019/11/11 Python
python实现3D地图可视化
2020/03/25 Python
CSS3 简写animation
2012/05/10 HTML / CSS
MYPROTEIN澳大利亚官方网站:欧洲运动营养品牌
2019/06/26 全球购物
C,C++的几个面试题小集
2013/07/13 面试题
优秀大学生推荐信范文
2013/11/28 职场文书
四查四看自我剖析材料
2014/09/19 职场文书
授权收款委托书范本
2014/10/10 职场文书
工作疏忽检讨书500字
2014/10/26 职场文书
西湖英语导游词
2015/02/06 职场文书
2015年学校党建工作总结
2015/05/19 职场文书
golang slice元素去重操作
2021/04/30 Golang
基于Redis实现分布式锁的方法(lua脚本版)
2021/05/12 Redis