JavaScript,如何读取上传图片大小

iamchinese 2006-01-17 02:48:45
我想实现图片按比例缩放,以便不变形
...全文
220 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
iamchinese 2006-01-20
  • 打赏
  • 举报
回复
KimSoft(革命的小酒天天醉)
谢谢,
不过最好在dreamweaver中设置静态设置一下width或者height(注意只设1个)
这样限制了图片的上限,这样图片不但按比例缩放,而且不会太大,影响整个界面
KimSoft 2006-01-17
  • 打赏
  • 举报
回复
第一次发的代码有错误
if(image.width / image.height > = iImgWidth / iImgHeight)
应为
if(image.width / image.height >= iImgWidth / iImgHeight)
KimSoft 2006-01-17
  • 打赏
  • 举报
回复
img 不要设置宽和高,在onload(加载完成时,缩放图片),不过这种方式不是很好,最好的办法是用组件在上传时生成缩略的小图,在列表网页上显示,点击小图打开大图,组件详见
ASPJPEG
VB版龙卷风的图片缩放组件
还有其它组件,楼主自己搜索。

以下代码另存为x.htm,将sea.jpg换成你机器上的图片,双击测试
<img src="sea.jpg" onload="DrawImage(this);" />


<SCRIPT LANGUAGE="JavaScript">
//JavaScript Document
<!--
function DrawImage(oImage)
{
var iImgWidth = 150;
var iImgHeight = 150;
var image = new Image();
image.src = oImage.src;
if(image.width > 0 && image.height > 0)
{
if(image.width / image.height >= iImgWidth / iImgHeight)
{
if(image.width > iImgWidth)
{
oImage.width = iImgWidth;
oImage.height = (image.height * iImgWidth) / image.width;
}
else
{
oImage.width = image.width;
oImage.height = image.height;
}
oImage.alt = image.width + " * " + image.height;
}
else
{
if(image.height > iImgHeight)
{
oImage.height = iImgHeight;
oImage.width = (image.width * iImgHeight) / image.height;
}
else
{
oImage.width = image.width;
oImage.height = image.height;
}
oImage.alt = image.width + " * " + image.height;
}
}
}
//-->
</SCRIPT>
iamchinese 2006-01-17
  • 打赏
  • 举报
回复
楼上,我刚学网页设计,请问具体如何调用呢,请执教

比如:

<html>

<img src="about.jpg" width="218" height="295" /> ''-------怎么调用呢

</html>

<script>
function
...
</script>
zwrtv 2006-01-17
  • 打赏
  • 举报
回复
路过收藏
KimSoft 2006-01-17
  • 打赏
  • 举报
回复
//JavaScript Document
<!--
function DrawImage(oImage)
{
var iImgWidth = 150;
var iImgHeight = 150;
var image = new Image();
image.src = oImage.src;
if(image.width > 0 && image.height > 0)
{
if(image.width / image.height > = iImgWidth / iImgHeight)
{
if(image.width > iImgWidth)
{
oImage.width = iImgWidth;
oImage.height = (image.height * iImgWidth) / image.width;
}
else
{
oImage.width = image.width;
oImage.height = image.height;
}
oImage.alt = image.width + " * " + image.height;
}
else
{
if(image.height > iImgHeight)
{
oImage.height = iImgHeight;
oImage.width = (image.width * iImgHeight) / image.height;
}
else
{
oImage.width = image.width;
oImage.height = image.height;
}
oImage.alt = image.width + " * " + image.height;
}
}
}
//-- >

28,406

社区成员

发帖
与我相关
我的任务
社区描述
ASP即Active Server Pages,是Microsoft公司开发的服务器端脚本环境。
社区管理员
  • ASP
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧