我写了一个上传服务器前制作缩略图的函数,请大家看看

本文介绍了一个用于图片预览的JavaScript工具,该工具支持图片的比例缩放和限制,适用于上传图片前的预览及HTTP路径图片处理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

 

 

 
Java代码
  1. /**  
  2.  * Created by IntelliJ IDEA.  
  3.  * author: dean  
  4.  * Date: 2006-8-14  
  5.  * Time: 10:45:39  
  6.  * description: 支持比例缩放,限制比例。  
  7.  *                        主要目的为在上传图片到服务器端之前进行预览。  
  8.  *                        当然更可用在HTTP形式路径的图片上。  
  9.  *  location: mapbar.com  
  10.  */  
  11.   
  12. function ThumbnailTool() {   
  13.   
  14.     this.width = 0;   
  15.     this.height = 0;   
  16.     this.maxPixel = 50;   
  17.     this.boolean = false;   
  18.     this.src = "";   
  19.   
  20.     this.setMaxPixel = function (maxPixel) {   
  21.         this.maxPixel = maxPixel;   
  22.     }   
  23.   
  24.     this.isAutoMaxSize = function (bToMaxSize) {   
  25.         this.bToMaxSize = bToMaxSize;   
  26.     }   
  27.   
  28.     this.getWidth = function () {   
  29.         return this.width;   
  30.     }   
  31.   
  32.     this.getHeight = function () {   
  33.         return this.height;   
  34.     }   
  35.   
  36.     this.getSrc = function () {   
  37.         return this.src;   
  38.     }   
  39.   
  40.     this.toThumbnail = function (imgSrc) {   
  41.   
  42.         var img = new Image();   
  43.         img.src = imgSrc;   
  44.         var imgWidht = img.width;   
  45.         var imgHeight = img.height;   
  46.         this.src = img.src;   
  47.   
  48.         if (!(this.width > 0 && this.height > 0)) {   
  49.             if (imgWidht >= imgHeight) {   
  50.                 if (imgWidht > this.maxPixel || this.bToMaxSize) {   
  51.                     this.height = parseInt(parseFloat(imgHeight) / parseFloat(imgWidht) * this.maxPixel + 0.5);   
  52.                     this.width = this.maxPixel;   
  53.                 } else {   
  54.                     this.width = imgWidht;   
  55.                     this.height = imgHeight;   
  56.                 }   
  57.             } else {   
  58.                 if (imgHeight > this.maxPixel || this.bToMaxSize) {   
  59.                     this.width = parseInt(parseFloat(imgWidht) / parseFloat(imgHeight) * this.maxPixel + 0.5);   
  60.                     this.height = this.maxPixel;   
  61.                 } else {   
  62.                     this.width = imgWidht;   
  63.                     this.height = imgHeight;   
  64.                 }   
  65.             }   
  66.         }   
  67.     }   
  68. }  
/**
 * Created by IntelliJ IDEA.
 * author: dean
 * Date: 2006-8-14
 * Time: 10:45:39
 * description: 支持比例缩放,限制比例。
 *                        主要目的为在上传图片到服务器端之前进行预览。
 *                        当然更可用在HTTP形式路径的图片上。
 *  location: mapbar.com
 */

function ThumbnailTool() {

    this.width = 0;
    this.height = 0;
    this.maxPixel = 50;
    this.boolean = false;
    this.src = "";

    this.setMaxPixel = function (maxPixel) {
        this.maxPixel = maxPixel;
    }

    this.isAutoMaxSize = function (bToMaxSize) {
        this.bToMaxSize = bToMaxSize;
    }

    this.getWidth = function () {
        return this.width;
    }

    this.getHeight = function () {
        return this.height;
    }

    this.getSrc = function () {
        return this.src;
    }

    this.toThumbnail = function (imgSrc) {

        var img = new Image();
        img.src = imgSrc;
        var imgWidht = img.width;
        var imgHeight = img.height;
        this.src = img.src;

        if (!(this.width > 0 && this.height > 0)) {
            if (imgWidht >= imgHeight) {
                if (imgWidht > this.maxPixel || this.bToMaxSize) {
                    this.height = parseInt(parseFloat(imgHeight) / parseFloat(imgWidht) * this.maxPixel + 0.5);
                    this.width = this.maxPixel;
                } else {
                    this.width = imgWidht;
                    this.height = imgHeight;
                }
            } else {
                if (imgHeight > this.maxPixel || this.bToMaxSize) {
                    this.width = parseInt(parseFloat(imgWidht) / parseFloat(imgHeight) * this.maxPixel + 0.5);
                    this.height = this.maxPixel;
                } else {
                    this.width = imgWidht;
                    this.height = imgHeight;
                }
            }
        }
    }
}



调用方法:

Java代码 复制代码  收藏代码
  1. var tbImg = new ThumbnailTool();   
  2.   
  3. //解决有时候尺寸太大的图片没有读入内存时出现的问题imgSrc 可以是本地路径也可以是http方式的路径   
  4. while(tbImg.getWidth()<2 || tbImg.getHeight()<2 ){   
  5.     tbImg.setMaxPixel(50);   
  6.     tbImg.toThumbnail(imgSrc);   
  7. /   tbImg.isAutoMaxSize (true);   
  8. }   
  9.   
  10. document.getElementById("d9").width = tbImg.getWidth();   
  11. document.getElementById("d9").height = tbImg.getHeight();   
  12. document.getElementById("d9").src = tbImg.getSrc();   
  13. document.getElementById("d9").alt = "";  
     var tbImg = new ThumbnailTool();

     //解决有时候尺寸太大的图片没有读入内存时出现的问题imgSrc 可以是本地路径也可以是http方式的路径
     while(tbImg.getWidth()<2 || tbImg.getHeight()<2 ){
         tbImg.setMaxPixel(50);
         tbImg.toThumbnail(imgSrc);
    //   tbImg.isAutoMaxSize (true);
     }

     document.getElementById("d9").width = tbImg.getWidth();
     document.getElementById("d9").height = tbImg.getHeight();
     document.getElementById("d9").src = tbImg.getSrc();
     document.getElementById("d9").alt = "";


 
作者: 陈中山

转载于:https://www.cnblogs.com/myfapiao/archive/2013/01/17/2865232.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值