纯JS实现图片预览与等比例缩放和居中
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title> ResizeImage </title>
<meta name="Author" content="Ryan Shaw">
<meta name="Keywords" content="Javascript,js,css,img,margin,onload,onchange">
<meta name="Description" content="image preview and autoresize">
<style type="text/css">
.form-txt {
height: 22px;
padding-left: 3px;
line-height: 22px;
background-color: #FFFFFF;
border: 1px solid #BBBBBB;
vertical-align: middle;
width: 200px;
color: #666;
font-size: 12px;
}
.release-hd{ background:#FFFFFF; border:1px solid #e5e5e5; padding:15px;}
.item { margin:0 auto; padding-bottom: 10px; overflow: hidden; height:24px;}
.item-name { float: left; font-size: 12px; padding-right: 8px; padding-top: 0px; text-align: right; width: 100px; color:#617b24; font-weight:700;}
</style>
<script type="text/javascript">
// 不喜欢定义全局变量的同学可以不在这儿定义,我没有使用,用起来的话要好些个人认为
var MAX_WIDTH = 300; // 最大宽度
var MAX_HEIGHT = 200; // 最大高度
// 预览图片
function previewImage(file,order)
{
var div = document.getElementById("preview");
if (file.files && file.files[0])
{
div.innerHTML = "<img id=\"imghead\">";
var img = document.getElementById("imghead");
var reader = new FileReader();
reader.onload = function(evt){
AutoResizeImage(300,200,img,evt.target.result);
}
reader.readAsDataURL(file.files[0]);
}
else
{
div.innerHTML = "<img id=\"imghead\">";
var img = document.getElementById("imghead");
AutoResizeImage(300,200,img,file.value);
}
}
// 缩放图片,imgSrc用户延迟加载图片url
function AutoResizeImage(maxWidth,maxHeight,objImg,imgSrc){
var img = new Image();
img.src = imgSrc || 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.style.height = Math.round(h) + "px";
objImg.style.width = Math.round(w) + "px";
if(h < maxHeight) { // 纵向有空余空间
objImg.style.marginTop = Math.round((maxHeight - h) / 2) + "px";
}
if(w < maxWidth) { // 横向有空余空间
objImg.style.marginLeft = Math.round((maxWidth - w) / 2) + "px";
}
if(!!!objImg.src)
objImg.src = imgSrc;
}
</script>
</head>
<body>
<div class="release-hd">
<table>
<tbody>
<tr>
<td>
<p class="item">
<label class="item-name">图片:</label>
<input class="form-txt" type="file" name="adPic" onchange=previewImage(this)>
</p>
<p class="item">
<label class="item-name">链接:</label>
<input class="form-txt" type="text" name="adUrl" value="">
</p>
<p class="item" style="height:72px">
<label class="item-name">主题:</label>
<textarea name="adTitle" cols=30 rows=10 style="height:66px" maxlength="250" class="form-txt"></textarea>
</p>
<span style="height:26px; font-weight:700; line-height:26px;color:#030; border:solid 1px #666;float:right; margin-right:10px;">
<input type="button" style="height:26px;border: 0 none;color:#006600; width:60px;" value="保存" />
</span>
</td>
<td>
<div id="preview">
<img id="imghead" width=300 height=200 border=0 src="http://su.bdimg.com/static/skin/img/logo_white.png" onload=AutoResizeImage(300,200,this)>
</div>
</td>
</tr>
</tbody>
</table>
</div>
</body>
</html>