图片按比例缩放函数
函数 图片 缩放 比例
2023-06-13 09:13:41 时间
以下是程序代码:
<script language="JavaScript">
<!--
//图片按比例缩放
var flag=false;
function DrawImage(ImgD,iwidth,iheight){
//参数(图片,允许的宽度,允许的高度)
var image=new Image();
image.src=ImgD.src;
if(image.width>0 && image.height>0){
flag=true;
if(image.width/image.height>= iwidth/iheight){
if(image.width>iwidth){
ImgD.width=iwidth;
ImgD.height=(image.height*iwidth)/image.width;
}else{
ImgD.width=image.width;
ImgD.height=image.height;
}
ImgD.alt=image.width+"×"+image.height;
}
else{
if(image.height>iheight){
ImgD.height=iheight;
ImgD.width=(image.width*iheight)/image.height;
}else{
ImgD.width=image.width;
ImgD.height=image.height;
}
ImgD.alt=image.width+"×"+image.height;
}
}
}
//-->
</script>
调用:<img src="images/toplogo.gif" onload="javascript:DrawImage(this,100,100)">
<!--
//图片按比例缩放
var flag=false;
function DrawImage(ImgD,iwidth,iheight){
//参数(图片,允许的宽度,允许的高度)
var image=new Image();
image.src=ImgD.src;
if(image.width>0 && image.height>0){
flag=true;
if(image.width/image.height>= iwidth/iheight){
if(image.width>iwidth){
ImgD.width=iwidth;
ImgD.height=(image.height*iwidth)/image.width;
}else{
ImgD.width=image.width;
ImgD.height=image.height;
}
ImgD.alt=image.width+"×"+image.height;
}
else{
if(image.height>iheight){
ImgD.height=iheight;
ImgD.width=(image.width*iheight)/image.height;
}else{
ImgD.width=image.width;
ImgD.height=image.height;
}
ImgD.alt=image.width+"×"+image.height;
}
}
}
//-->
</script>
调用:<img src="images/toplogo.gif" onload="javascript:DrawImage(this,100,100)">
相关文章
- unicloud云函数开发微信客服消息自动回复图片消息(完整步骤)
- plot函数的用法_ezplot函数
- C语言动态内存分配函数malloc(),calloc(),realloc()用法对比分析
- listagg within group函数的作用_oracletochar函数
- H5-Canvas实战-合成任意数量图片文字-函数封装处理
- 前端进阶JS函数增强与对象增强
- 使用 JavaScript 实现 PHP 函数功能:PHP.JS
- Python reversed函数及用法
- [函数]在弹出的窗口中输出内表[BKK_POPUP_DISPLAY_LIST]详解编程语言
- Oracle中取日期的函数:SYSDATE(oracle取日期函数)
- MSSQL视图与函数:实现数据更高效的查询(mssql 视图和函数)
- 利用Oracle函数Casting实现数据类型转换(oracle函数cast)
- PHP截取汉字乱码问题解决方法mb_substr函数的应用
- php图片处理:加水印、缩略图的实现(自定义函数:watermark、thumbnail)
- php中随机显示图片的函数代码
- PHP图片裁剪函数(保持图像不变形)
- PHP图片等比例缩放生成缩略图函数分享
- PHP图片处理之使用imagecopyresampled函数实现图片缩放例子