Chinaunix首页 | 论坛 | 博客
  • 博客访问: 315876
  • 博文数量: 128
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 1611
  • 用 户 组: 普通用户
  • 注册时间: 2013-08-19 11:49
文章分类

全部博文(128)

文章存档

2018年(2)

2016年(2)

2014年(10)

2013年(114)

我的朋友

分类: Web开发

2013-10-10 11:21:26

缩放图片脚本分享


点击(此处)折叠或打开

  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "">
  2. <html>
  3. <head>
  4. <title> New Document </title>
  5. <meta name="Generator" content="EditPlus">
  6. <meta name="Author" content="">
  7. <meta name="Keywords" content="">
  8. <meta name="Description" content="">

  9. <script>

  10. function AutoResizeImage(maxWidth,maxHeight){
  11. var objImg = document.getElementById("operImg");
  12. var img = new Image();
  13. img.src = objImg.src;
  14. var hRatio;
  15. var wRatio;
  16. var Ratio = 1;
  17. var w = img.width;
  18. var h = img.height;
  19. wRatio = maxWidth / w;
  20. hRatio = maxHeight / h;
  21. if (maxWidth ==0 && maxHeight==0){
  22. Ratio = 1;
  23. }else if (maxWidth==0){//
  24. if (hRatio<1) Ratio = hRatio;
  25. }else if (maxHeight==0){
  26. if (wRatio<1) Ratio = wRatio;
  27. }else if (wRatio<1 || hRatio<1){
  28. Ratio = (wRatio<=hRatio?wRatio:hRatio);
  29. }
  30. if (Ratio<1){
  31. w = w * Ratio;
  32. h = h * Ratio;
  33. }
  34. objImg.height = h;
  35. objImg.width = w;
  36. }
  37. </script>

  38. </head>

  39. <body>

  40. <img src="1111.jpg" border="0" alt="534 X 800" id="operImg"/>

  41. <input type="button" value="缩放至宽100px,等比例压缩" onclick="AutoResizeImage(100,0)"/>

  42. <input type="button" value="缩放至300px,等比例压缩" onclick="AutoResizeImage(300,0)"/>

  43. <input type="button" value="原图" onclick="AutoResizeImage(0,0)"/>
  44. </body>
  45. </html>
unity3d 

重点js:


点击(此处)折叠或打开

  1. function AutoResizeImage(maxWidth,maxHeight){
  2. var objImg = document.getElementById("operImg");
  3. var img = new Image();
  4. img.src = objImg.src;
  5. var hRatio;
  6. var wRatio;
  7. var Ratio = 1;
  8. var w = img.width;
  9. var h = img.height;
  10. wRatio = maxWidth / w;
  11. hRatio = maxHeight / h;
  12. if (maxWidth ==0 && maxHeight==0){
  13. Ratio = 1;
  14. }else if (maxWidth==0){//
  15. if (hRatio<1) Ratio = hRatio;
  16. }else if (maxHeight==0){
  17. if (wRatio<1) Ratio = wRatio;
  18. }else if (wRatio<1 || hRatio<1){
  19. Ratio = (wRatio<=hRatio?wRatio:hRatio);
  20. }
  21. if (Ratio<1){
  22. w = w * Ratio;
  23. h = h * Ratio;
  24. }
  25. objImg.height = h;
  26. objImg.width = w;
  27. }

阅读(891) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~