上传图片本地预览缩小图片

<HTML>
<HEAD>
 <title>Upload Image</title>
 <meta http-equiv="Content-Type" content="text/html; charset=gb2312">
</HEAD>
<STYLE type="text/css">
* {font-size:9pt;font-family:SimSun}
body {margin:0px;overflow:hidden}
#upfile {width:100%}
#preview {width:100%;height:100%;overflow:hidden;background-color:#808080;margin:auto}
#pv {display:none}
</STYLE>
<script language="JScript">
var obImg = new Image();
var validExt = ".jpg|.jpeg|.gif|.bmp|.png|";
var bOK = false;
function ShowInfo(sInfo)
{
 document.all["info"].innerText = sInfo;
}
function CheckProperty()
{
 if(obImg.readyState != "complete")
 {
  setTimeout("CheckProperty()",500);
  return;
 }
 bOK = true;
 var iImgSize = Math.round(obImg.fileSize / 1024 * 100) / 100;
 ShowInfo("图片大小:" + iImgSize + " KB 图片尺寸:" + obImg.width + " x " + obImg.height);
 var iContentWidth = document.all["preview"].offsetWidth;
 var iContentHeight = document.all["preview"].offsetHeight;
 if(obImg.width > iContentWidth || obImg.height > iContentHeight)
 {
  if(obImg.width <= iContentWidth)
  {
    document.all["pv"].height = iContentHeight;
    document.all["pv"].width = Math.round(iContentHeight * obImg.width / obImg.height);
  }
  else if(obImg.height <= iContentHeight)
  {
    document.all["pv"].width = iContentWidth;
    document.all["pv"].height = Math.round(iContentWidth * obImg.height / obImg.width);
  }
  else
  {
   if(obImg.width / iContentWidth > obImg.height / iContentHeight)
   {
    document.all["pv"].width = iContentWidth;
    document.all["pv"].height = Math.round(iContentWidth * obImg.height / obImg.width);
   }
   else
   {
    document.all["pv"].height = iContentHeight;
    document.all["pv"].width = Math.round(iContentHeight * obImg.width / obImg.height);
   }
  }
 }
 else
 {
  document.all["pv"].width = obImg.width;
  document.all["pv"].height = obImg.height;
 }
 document.all["pv"].src = obImg.src;
 document.all["pv"].style.display = "block";
}
function OnClickUpload()
{
 bOK = false;
 var sFile = document.fmUpload.upfile.value;
 if(/(/.[^/.]*)$/ig.exec(sFile) == null)
 {
  ShowInfo("图片大小:? KB 图片尺寸:? x ?");
  document.all["pv"].style.display = "none";
  alert("不能识别的文件扩展名!");
  return;
 }
 if(validExt.indexOf(RegExp.$1 + "|") == -1)
 {
  ShowInfo("图片大小:? KB 图片尺寸:? x ?");
  document.all["pv"].style.display = "none";
  alert("不能上传 " + RegExp.$1 + " 格式的文件");
  return;
 }
 obImg.src = sFile;
 setTimeout("CheckProperty()",0);
}
function OnSubmitForm()
{
 alert("test");
 return false;
}
function Upload_Err()
{
 ShowInfo("上传文件失败!");
}
</script>
<BODY">
<table align="center" width="50%" height="50%" border="1" cellspacing="0">
 <form name="fmUpload" action="upload-do.php" method="post" enctype="multipart/form-data" οnsubmit="return OnSubmitForm()" target="frmDoUpload">
 <tr>
  <td width="100%"><input type="file" name="upfile" id="upfile" οnchange="OnClickUpload()"></td>
  <td>
   <input type="submit" name="bnSubmit" value="上传">
   <input type="hidden" name="fileExt">
   <input type="hidden" name="picType">
   <input type="hidden" name="isAutoSmall">
  </td>
 </tr>
 <tr><td align="center" id="info" colspan="2">&nbsp;</td></tr>
 <tr>
  <td height="100%" colspan="2" align="center" valign="middle">
   <div id="preview"><img id="pv"></div>
  </td>
 </tr>
 <tr style="display:none">
  <td colspan="2" height=50></td>
 </tr>
 </form>
</table>
</BODY>
</HTML> 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值