当前位置:网站首页>JS scale down the width and height of the picture
JS scale down the width and height of the picture
2022-06-12 10:29:00 【P P PHP】
<!DOCTYPE html>
<html>
<head>
<title>JS Zoom out the width and height of the picture </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;// The width of the uploaded picture
var imgHeight = this.height;// Height of uploaded picture
// The width and height of the picture after scaling
var targetWidth = imgWidth;
var targetHeight = imgHeight;
var maxWidth=1920;// The maximum width of the picture
var maxHeight = 1080;// Maximum picture height
var scale = imgWidth / imgHeight;// The width height ratio of the original drawing
// If the original width is greater than the maximum width
if(imgWidth>maxWidth){
targetWidth = maxWidth;
targetHeight = targetWidth/scale;
}
// After scaling, the height is still greater than the maximum height and continues to be scaled down
if(targetHeight>maxHeight){
targetHeight = maxHeight
targetWidth = targetHeight*scale;
}
canvas.width=targetWidth;//canvas The width of = Picture width
canvas.height=targetHeight;//canvas The height of = Picture height
context.clearRect(0,0,targetWidth,targetHeight)// clear canvas
context.drawImage(img,0,0,targetWidth,targetHeight)//canvas mapping
var newUrl=canvas.toDataURL('image',0.92);//canvas Export to base64
preview.src=newUrl
}
}
reader.readAsDataURL(file);
})
</script>
</html>
边栏推荐
- Oculus quest generation opens Bluetooth connection
- Unable to load dynamic library ‘oci8_ 12C 'or unable to load dynamic library' PDO_ OCI 'or cannot find module
- MQTT 协议中文版
- 【实验】MySQL主从复制及读写分离
- PHP: seven cattle cloud upload file
- 容器江湖的爱恨情仇
- CTF freshman cup PHP deserialization question - EzPop
- PHP maximum balance method to solve the problem that the sum of the final percentages is not equal to 100
- pycharm 查看opencv当前的版本
- Introduction to encoding formats (ASCII, Unicode and UTF-8)
猜你喜欢

Pseudo static setting of access database in win2008 R2 iis7.5

浅谈三维形状上下文特征3DSC理论及应用
![[Wayland] Wayland agreement description](/img/7b/b2dc41fcd15447252f48d2b61e8d59.jpg)
[Wayland] Wayland agreement description
![[CEGUI] log system](/img/5d/c8f76194b1ae2a62d3f5a1e84a5603.jpg)
[CEGUI] log system

A hundred secrets and a few secrets - Caesar encryption

How high can C language reach by self-study alone?

2022京東618預售定金怎麼退?京東618定金能退嗎?

【机器学习】基于鸢尾花(iris)数据集的逻辑回归分类实践

Dynamic proxy

How to refund the pre-sale deposit of JD 618 in 2022? Can JD 618 deposit be refunded?
随机推荐
【机器学习】基于鸢尾花(iris)数据集的逻辑回归分类实践
一测两三年,记测试交流经验的一些感想
93. obtain all IP addresses of the Intranet
Chapter 3 search
Binassii module - converting between binary and ASCII
Circuitbreaker fuse of resilience4j - circuitbreakerconfig configuration
Bug easily ignored by recursion
ASP.NET Core权限系统实战(零)
Mysql5.6.24 installation free deployment method
[CEGUI] resource loading process
Remote link software xshell and xftp download address
2022淘宝618超级喵运会玩法来了 超级喵运会有哪些攻略方法
在一个“去QA化”的项目中,QA能做什么?
ASP. Net core permission system practice (zero)
3. Abstract Factory
93. 獲得內網的所有IP地址
Unable to load dynamic library ‘oci8_ 12C 'or unable to load dynamic library' PDO_ OCI 'or cannot find module
Detailed explanation and use of redis data types: key and string types
Code types and data structures corresponding to the five object types of redis
reflex