jeppe's Fotoalbum
Microsoft JScript runtime error '800a138f'
Object expected
/inc/pic_handler.asp, line 220
pic_handler.asp:
<script language="JScript" RUNAT="Server">
/*
© Olof Hånell - 2004
olof@olf.se
*/
function PictureHandler(){
this.checkFile = function(strFileName){
if(strFileName.lastIndexOf(".")==-1) return new Dimension(-1, -1);
var extension = strFileName.substring(strFileName.lastIndexOf("."), strFileName.length).toLowerCase();
switch(extension){
case '.gif' : return this.handleGif(strFileName);
case '.bmp' : return this.handleBmp(strFileName);
case '.psd' : return this.handlePsd(strFileName);
case '.png' : return this.handlePng(strFileName);
case '.jpg' : return this.handleJpg(strFileName);
case '.tif' : /* fall through */;
case '.tiff' : return this.handleTif(strFileName);
}
return new Dimension(-1, -1);
}
this.handleGif = function(fName){
var f = this.getFileHandle(fName);
var gifVersion = this.bToStr(f.read(6), 6);
if(gifVersion == "GIF89a" || gifVersion == "GIF87a"){
//take a look at this..
//gif max resolution is limited to
//16bit other formats uses 32 bit.
var w = this.readLEWord(f);
var h = this.readLEWord(f);
f.close();
if(w>0 && h > 0) return new Dimension(w, h);
return new Dimension(-1, -1);
}else{
f.close();
return new Dimension(-1, -1);
}
}
this.handleBmp = function(fName){
var f = this.getFileHandle(fName);
var head = this.bToStr(f.read(2), 2);
if(head == "BM"){
f.read(16);
var w = this.readLEDWord(f);
var h = this.readLEDWord(f);
f.close();
if(w == 0 || h == 0) return new Dimension(-1, -1);
return new Dimension(w, h);
}
else{
f.close();
return new Dimension(-1, -1);
}
}
this.handlePsd = function(fName){
var f = this.getFileHandle(fName);
var head = this.bToStr(f.read(4), 4);
if(head == "8BPS"){
var version = parseInt(this.readBEWord(f));
if(!isNaN(version) && version == 1){
f.Position += 6;
var channels = this.readBEWord(f);
var h = this.readBEDWord(f);
var w = this.readBEDWord(f);
f.close();
if(w == 0 || h == 0) return new Dimension(-1, -1);
return new Dimension(w, h);
}
else{
return new Dimension(-1, -1);
}
}else{
f.close();
return new Dimension(-1, -1);
}
}
this.handlePng = function(fName){
//read png. tag-based format, find IHDR, then the next 64-bytes is w and h as
//32 bit each in big endian format
var f = this.getFileHandle(fName);
f.Position += 8;
var len = this.readBEDWord(f);
var ihdr = this.bToStr(f.read(4), 4);
if(ihdr == "IHDR"){
var w = this.readBEDWord(f);
var h = this.readBEDWord(f);
f.close();
if(w == 0 || h == 0) return new Dimension(-1, -1);
return new Dimension(w, h);
}
else{
f.close();
return new Dimension(-1, -1);
}
}
this.handleJpg = function(fName){
//id(byte), type(byte), length(DWord)
var f = this.getFileHandle(fName);
var inLoop = true;
var w=-1, h=-1;
var lTimes = 0;
while(inLoop){
var id = this.readByte(f);
var type = this.readByte(f);
//special id:type combination, must be skipped.
if(id == 0xFF && type == 0xD8) continue;
if(id == 0xFF && type == 0xC0){
f.read(3); //skip the three initial bytes.
h = this.readBEWord(f);
w = this.readBEWord(f);
inLoop = false;
break;
}
else{
var len = this.readBEWord(f) - 2;
if(len > 0) f.Position += len;
}
}
f.close();
if(w==-1 || h == -1 || w==0 || h == 0) return new Dimension(-1, -1);
return new Dimension(w, h);
}
this.handleTif = function(fName){
//tif-format. groups of tag,type,value
var f = this.getFileHandle(fName);
var byteOrder = this.readLEWord(f);
var useLE;
//it's so beutiful! Tiff first of all
//defines whether bytes appear in little- or
//big-endian order in the rest of the file.
if(byteOrder == 0x4949){
useLE = true;
}
else if(byteOrder == 0x4D4D){
useLE = false;
}
else{
return new Dimension(-1, -1);
}
var version = (useLE)? this.readLEWord(f) : this.readBEWord(f);// tiff-version.
var imOffset = (useLE)? this.readLEDWord(f) : this.readBEDWord(f);// where's the image-data..
imOffset -= 8;
if(imOffset > 0) f.Position += imOffset; //jump to the image data.
var numberOfEntries = (useLE)? this.readLEWord(f) : this.readBEWord(f);
var w = -1, h = -1;
for(var i = 0; i < numberOfEntries; i++){
var tag = (useLE)? this.readLEWord(f) : this.readBEWord(f);
var valueType = (useLE)? this.readLEWord(f) : this.readBEWord(f);
var numValues = (useLE)? this.readLEDWord(f) : this.readBEDWord(f);
var value = (useLE)? this.readLEWord(f) : this.readBEWord(f);
(useLE)? this.readLEWord(f) : this.readBEWord(f);
if(tag == 0x100){ //0x100 is the tag for width.
w = value;
if(h != -1) break; //have height already been found?
}
else if(tag == 0x101){ //0x101 is the tag for height.
h = value;
if(w != -1) break; //have width already been found?
}
}
f.close();
if(w != -1 && h != -1){ //if width and height was found, return dimension
return new Dimension(w, h);
}else{
return new Dimension(-1, -1); //return a dimension signifying an error
}
}
this.getFileHandle = function(fName){
//return a file-handle to the image-file.
var stream = Server.createObject("ADODB.Stream");
stream.Type = 1;
stream.Open();
stream.LoadFromFile(fName);
return stream;
}
this.readLEWord = function(f){
//reads two bytes and returns them as int in little endian format
var b0, b1;
b0 = this.readByte(f);
b1 = this.readByte(f);
return (b1 << 8) | b0;
}
this.readLEDWord = function(f){
//reads four bytes in little endian order
var b0, b1, b2, b3;
b0 = this.readByte(f);
b1 = this.readByte(f);
b2 = this.readByte(f);
b3 = this.readByte(f);
return (b3 << 24) | (b2 << 16) | (b1 << 8) | b0;
}
this.readBEWord = function(f){
//reads two bytes in big endian order
var b0, b1;
b0 = this.readByte(f);
b1 = this.readByte(f);
var back = (b0 << 8 ) | b1;
return back;
}
this.readBEDWord = function(f){
//reads four bytes in big endian order
var b0, b1, b2, b3;
b0 = this.readByte(f);
b1 = this.readByte(f);
b2 = this.readByte(f);
b3 = this.readByte(f);
return (b0 << 24) | (b1 << 16) | (b2 << 8) | b3;
}
this.readByte = function(f){
return BToByte(f.read(1));
}
this.bToStr = function(binary, numBytes){
//convert vbByteArray to string using recordset.
if(numBytes > 0){
var rs = Server.CreateObject("ADODB.Recordset"), str;
rs.fields.append(0, 201, numBytes);
rs.open();
rs.addNew();
rs.fields(0).appendChunk(binary);
rs.Update();
str = new String(rs.Fields(0));
rs.close();
rs = null;
return str;
}
return "";
}
}
function Dimension(w, h){
this.width = w;
this.height = h;
}
function vbPicHandler(){
//returns instance of PictureHandler to vbs.
return new PictureHandler();
}
</script>