- /**
- * 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;
- }
- }
- }
- }
- }
/**
* 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;
}
}
}
}
}
调用方法:
- 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 = "";
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 = "";
作者:
陈中山