js 控制圖片大小核心講解
更新時間:2013年10月09日 16:29:26 作者:
控制圖片大小的方法有很多,在本文將為大家詳細介紹下使用js實現(xiàn)縮放圖片,核心代碼如下,感興趣的朋友可以參考下
縮放圖片腳本分享
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title> New Document </title>
<meta name="Generator" content="EditPlus">
<meta name="Author" content="">
<meta name="Keywords" content="">
<meta name="Description" content="">
<script>
function AutoResizeImage(maxWidth,maxHeight){
var objImg = document.getElementById("operImg");
var img = new Image();
img.src = objImg.src;
var hRatio;
var wRatio;
var Ratio = 1;
var w = img.width;
var h = img.height;
wRatio = maxWidth / w;
hRatio = maxHeight / h;
if (maxWidth ==0 && maxHeight==0){
Ratio = 1;
}else if (maxWidth==0){//
if (hRatio<1) Ratio = hRatio;
}else if (maxHeight==0){
if (wRatio<1) Ratio = wRatio;
}else if (wRatio<1 || hRatio<1){
Ratio = (wRatio<=hRatio?wRatio:hRatio);
}
if (Ratio<1){
w = w * Ratio;
h = h * Ratio;
}
objImg.height = h;
objImg.width = w;
}
</script>
</head>
<body>
<img src="1111.jpg" border="0" alt="534 X 800" id="operImg"/>
<input type="button" value="縮放至寬100px,等比例壓縮" onclick="AutoResizeImage(100,0)"/>
<input type="button" value="縮放至300px,等比例壓縮" onclick="AutoResizeImage(300,0)"/>
<input type="button" value="原圖" onclick="AutoResizeImage(0,0)"/>
</body>
</html>
重點js:
function AutoResizeImage(maxWidth,maxHeight){
var objImg = document.getElementById("operImg");
var img = new Image();
img.src = objImg.src;
var hRatio;
var wRatio;
var Ratio = 1;
var w = img.width;
var h = img.height;
wRatio = maxWidth / w;
hRatio = maxHeight / h;
if (maxWidth ==0 && maxHeight==0){
Ratio = 1;
}else if (maxWidth==0){//
if (hRatio<1) Ratio = hRatio;
}else if (maxHeight==0){
if (wRatio<1) Ratio = wRatio;
}else if (wRatio<1 || hRatio<1){
Ratio = (wRatio<=hRatio?wRatio:hRatio);
}
if (Ratio<1){
w = w * Ratio;
h = h * Ratio;
}
objImg.height = h;
objImg.width = w;
}
復(fù)制代碼 代碼如下:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title> New Document </title>
<meta name="Generator" content="EditPlus">
<meta name="Author" content="">
<meta name="Keywords" content="">
<meta name="Description" content="">
<script>
function AutoResizeImage(maxWidth,maxHeight){
var objImg = document.getElementById("operImg");
var img = new Image();
img.src = objImg.src;
var hRatio;
var wRatio;
var Ratio = 1;
var w = img.width;
var h = img.height;
wRatio = maxWidth / w;
hRatio = maxHeight / h;
if (maxWidth ==0 && maxHeight==0){
Ratio = 1;
}else if (maxWidth==0){//
if (hRatio<1) Ratio = hRatio;
}else if (maxHeight==0){
if (wRatio<1) Ratio = wRatio;
}else if (wRatio<1 || hRatio<1){
Ratio = (wRatio<=hRatio?wRatio:hRatio);
}
if (Ratio<1){
w = w * Ratio;
h = h * Ratio;
}
objImg.height = h;
objImg.width = w;
}
</script>
</head>
<body>
<img src="1111.jpg" border="0" alt="534 X 800" id="operImg"/>
<input type="button" value="縮放至寬100px,等比例壓縮" onclick="AutoResizeImage(100,0)"/>
<input type="button" value="縮放至300px,等比例壓縮" onclick="AutoResizeImage(300,0)"/>
<input type="button" value="原圖" onclick="AutoResizeImage(0,0)"/>
</body>
</html>
重點js:
復(fù)制代碼 代碼如下:
function AutoResizeImage(maxWidth,maxHeight){
var objImg = document.getElementById("operImg");
var img = new Image();
img.src = objImg.src;
var hRatio;
var wRatio;
var Ratio = 1;
var w = img.width;
var h = img.height;
wRatio = maxWidth / w;
hRatio = maxHeight / h;
if (maxWidth ==0 && maxHeight==0){
Ratio = 1;
}else if (maxWidth==0){//
if (hRatio<1) Ratio = hRatio;
}else if (maxHeight==0){
if (wRatio<1) Ratio = wRatio;
}else if (wRatio<1 || hRatio<1){
Ratio = (wRatio<=hRatio?wRatio:hRatio);
}
if (Ratio<1){
w = w * Ratio;
h = h * Ratio;
}
objImg.height = h;
objImg.width = w;
}
相關(guān)文章
JavaScript+html5 canvas實現(xiàn)圖片破碎重組動畫特效
這篇文章主要介紹了JavaScript+html5 canvas實現(xiàn)破碎重組的視頻特效,感興趣的小伙伴們可以參考一下2016-02-02elementui-樹形控件實現(xiàn)子節(jié)點右側(cè)添加圖標和數(shù)據(jù)鼠標放上去顯示文字效果
這篇文章主要介紹了elementui-樹形控件實現(xiàn)子節(jié)點右側(cè)添加圖標和數(shù)據(jù)鼠標放上去顯示文字效果,本文結(jié)合實例代碼給大家介紹的非常詳細,感興趣的朋友一起看看吧2024-01-01