Library code snippets
Retrieving Remote Image Properties in ASP
Recently, my friends and I decided to make a site similar to www.hotornot.com. We wanted to allow users to just put the URL of an image they want displayed on the site and the image would eventually be displayed. However, some people put images that were too large and distorted the layout of the website. We attempted to make every image a set length and width, but that was disproportional and stretched out most of the images. I found a file by Mike Shaffer called IMGSZ. I attempted to make the script work with local files, but I had troubles. I wrote a file based on the size defining algorithms in IMGSZ that would also pull the image from a remote website using XML. It works very similarly to what IMGSZ should and I would like to note his help on this. The only flaw is that it only supports GIF and JPG files, which are the only files I needed to support.
The syntax to use getSize is as follows:
someBoolean = getSize(URL, width, height, depth, flType)
-
someBooleanwill be set to true or false based on whether the script was able to determine the images attributes -
URLis an input stating the URL of the image, such as http://www.4guysfromrolla.com/images/newbanner.gif -
Widthwill be set to the width of the image, or –1 if the script failed -
Heightwill be set to the height of the image, or –1 if the script failed -
Depthwill be set to the depth of the image, or –1 if the script failed -
FlTypewill be set to the type of file the image was, GIF or JPG
A major problem with IMGSZ was that it did not use binary string functions such as midb and lenb. Since all images are binary, these functions are required opposed to mid and len.
'This script was written by Lawrence Finn'
'Anyone is free to use this file or alter it at their discretion.
' lngconvert and lngconvert2 were written by Mike Shaffer
'The rest is based on his file IMGSZ with major modifications
'...now it actually works! function GetBytes(objHTTP, offset, bytes)
Dim SizeofFile
on error resume next
SizeofFile = objHTTP.getResponseHeader("Content-Length")
'gets the size of the file from the HTTP header
if offset > 0 then 'start getting bytes NOT at the beginning
strbuff = midb(objHTTP.responseBody, offset, bytes)
end if
if bytes = -1 then ' Get All!
GetBytes = objHTTP.responseBody 'ReadAll
else 'start at front and go some odd distance
GetBytes = midb(objHTTP.responseBody, 1, bytes)
end if
end function
function lngConvert(strTemp)
lngConvert = clng(ascb(leftb(strTemp, 1)) + ((ascb(rightb(strTemp,
1)) * 256)))
end function
function lngConvert2(strTemp)
lngConvert2 = clng(ascb(rightb(strTemp, 1)) + ((ascb(leftb(strTemp,
1)) * 256)))
end function
'''''lngconvert was taken from Mike Shaffer's IMGSZ
function getSize(URL, width, height, depth, flType)
dim PNGflType
dim GIFflType
dim BMPflType
dim flTypeOf
dim obj
flTypeOf = ""
flType = "(unknown)"
Set obj = Server.CreateObject ("Microsoft.XMLHTTP")
obj.open "GET", URL, False
obj.send
'Here we have gotten the data for the image file
getSize = False
PNGflType = chr(137) & chr(80) & chr(78)
GIFflType = chrb(71) & chrb(73) & chrb(70)
BMPflType = chr(66) & chr(77)
'Here are the definitions for the image flTypes, I only support
GIF and JPG but you can add others :)
flTypeOf = GetBytes(obj, 0, 3)
'Find out what flType of image it is
if flTypeOf = GIFflType then 'It is a GIF!!!
flType = "GIF"
strbuffer = getbytes(obj, 0, -1) 'get all of the data of the image
width= lngconvert(midb(strbuffer, 7, 2))
Height = lngconvert(midb(strbuffer, 9, 2))
Depth = 2 ^ ((ascb(GetBytes(obj, 11, 1)) and 7) + 1)
'It is very important to note the ascB and midB, images ARE binary
files
getSize = True
else
strBuff = GetBytes(obj, 0, -1) ' get
the entire file
SizeofFile = lenb(strBuff)
flgFound = 0
strTarget = chrb(255) & chrb(216) & chrb(255)
flgFound = instrb(strBuff, strTarget)
char = (midb(strbuff, 1, 3)) 'check out the first few characters
if flgFound = 0 then 'not a jpg, and definatly not a GIF
exit function
end if
flType = "JPG"
lngPos = flgFound + 2
ExitLoop = false
do while ExitLoop = False and lngPos < SizeofFile
do while ascb(midb(strBuff, lngPos, 1)) =
255 and lngPos < SizeofFile
lngPos = lngPos + 1
loop
'search through to find the data
if ascb(midb(strBuff, lngPos, 1)) < 192
or ascb(midb(strBuff, lngPos, 1)) > 195 then
lngMarkerSize = lngConvert2(midb(strBuff,
lngPos + 1, 2))
lngPos = lngPos + lngMarkerSize
+ 1
else
ExitLoop = True 'have everything
we need
end if
loop
if ExitLoop = False then 'oh no!
Width = -1
Height = -1
Depth = -1
else
Height = lngConvert2(midb(strBuff,
lngPos + 4, 2))
Width = lngConvert2(midb(strBuff,
lngPos + 6, 2))
Depth = 2 ^ (ascb(midb(strBuff,
lngPos + 8, 1)) * 8)
getSize = True
end if
end if
set obj = Nothing
end function
Related articles
Related discussion
-
Binary Studio | software development outsourcing Ukraine
by Soft Industry (5 replies)
-
asp Request.QueryString("dir")
by realmeteo (1 replies)
-
Looking for Senior Web Designer
by lwsmedia (0 replies)
-
Read eMails from Outlook express using ASP
by kumaravelu (1 replies)
-
Help to Call ASP function from onclick event in HTML to pass an array
by vka (0 replies)
Related podcasts
-
Scott Guthrie
Scott catches up with Scott Guthrie in an interview covering Ajax, Asp 2.0, extender controls, CSS adapters and more.
Hi,
I wrote a code to save on my site a file from an URL in ASP. You can save an image (jpg,gif,...) or another binary file.
The code is here : http://www.eromandie.com/codesource/asp/savefilefrom_url.asp
Regards
Youk88
Don't forget to set theHeight, theWidth, and theURL before running this code.
I styled it based on the first post. Since it was assumed the variables were set before running this code, I did the same. If you are displaying an image that you don't know the height and width ahead of time you can use a component to find out what dimensions are. ImageSize component available at http://www.serverobjects.com/products.htm is commonly used and it's free!
If you neglected to set the height and width of the original image, my code will generate something like the following...
<img src="theImage.jpg" height="" width="" />
Internet Explorer displays a 0px X 0px graphic which can't be seen.
Better yet try this fucntion...
<%'----Function to scale an image to fit------
Function ImageBound(theURL,maxht,maxwt,scaleup)
'maxht - max height of image
'maxwt - max width of image
'scaleup - False only scales down large images. True also scales up small images.
' Note: When set to false the function only scales down images that are oversized.
' True resizes all images to fit within the available rectangle with aspect retained.
'theURL - The relative URL of the image.
'You must have the ImageSize component installed for this to work.
'http://www.serverobjects.com/products.htm
set Img = Server.CreateObject("ImgSize.Check")
Img.FileName = Server.MapPath(theURL)
if Img.Error <> "" then
Response.Write "An error occurred in processing this image.<br>"
Response.Write "The error was: <b>" & Img.Error & "</b>"
else
theHeight=Img.Height
theWidth=Img.Width
maxaspect=maxht/maxwt 'aspect ratio of max rectangle
If theWidth>=1 and theHeight >=1 and ((theWidth>maxwt or theHeight>maxht) or scaleup) then
theAspect=theHeight/theWidth
If theAspect>maxaspect then
'Image has taller aspect ratio than max rectangle
imght=maxht
imgwt=int(maxht/theAspect+.5)
Else
'Image has wider aspect ratio than max rectangle
imght=int(maxwt*theAspect+.5)
imgwt=maxwt
End If
Else
'Image has either no width or no height or fits within max rectangle
imght=theHeight
imgwt=theWidth
End If
end if
ImageBound= "<img src=""" & theURL & """ height=""" & imght & """ width=""" & imgwt & """ />"
set Img = nothing
End Function
response.write ImageBound("/images/image.jpg",50,75,True)
response.write ImageBound("/images/image.jpg",50,75,False)
%>
Howzit
i'm new to asp, i tried to apply the code, now my pictures do not display at all?
do u need a component to run this code or issit asp.net?
thanks
I adapted some code I used so it used the same style and variable names as your code.
The idea of this code is to make sure the image will fit within a max rectangle and keep the image's aspect ratio the same. Also it prevents divide by zero errors if the image dimensions are unknown and rounds the new height and width to the nearest pixel instead of the next smaller pixel.
The code can be made to scale up smaller images to fit the max ractangle as well by replacing the following line with the one just below it. Great for thumbnail images!
If theWidth>=1 and theHeight >=1 and (theWidth>maxwt or theHeight>maxht) then
If theWidth>=1 and theHeight >=1 then
<%'----routine to scale image if it is too big------
maxht = 50 'max height of image
maxwt = 130 'max width of image
maxaspect=maxht/maxwt 'aspect ratio of max rectangle
If theWidth>=1 and theHeight >=1 and (theWidth>maxwt or theHeight>maxht) then
theAspect=theHeight/theWidth
If theAspect>maxaspect then
'Image has taller aspect ratio than max rectangle
imght=maxht
imgwt=int(maxht/theAspect+.5)
Else
'Image has wider aspect ratio than max rectangle
imght=int(maxwt*theAspect+.5)
imgwt=maxwt
End If
Else
'Image has either no width or no height or fits within max rectangle
imght=theHeight
imgwt=theWidth
End If
%>
<img src="<%=theURL%>" height="<%=imght%>" width="<%=imgwt%>" />
I am confirming that is has something to do with a firewall.
I have access to 4 servers. 2 running Windows 2000 SBS (small business server) with ISA firewall - 2 nics. 2 servers with regular 2000 server.
The 2 with 2k server run great. No problems.
The 2 with SBS both have problems giving the c0005 error.
If you run your ASP page to do your request within the server, then it works. Not externally though. The error is always at the .send command.
Traxxas Revo T-Maxx Parts TMaxx Store
Dear stoneman,
Thanks a lot.
Regards,
Ram Janm Singh
Chetu Inc.
Ph: +91 120-3946734 - outside US
Ph: +1 (305) 402 6724 - within US
Fax: +1 305 832 5987
Mb: +91 9868 567 152
For more information, please visit: http://www.chetu.com
Singh,
I know this post is almost 6 months old, but as your listing is first in Google, I thought I would respond with what solved this problem for me. I believe my issue was some kind of DNS / Firewall issue. I was using http://www.mydomain.com/thefileforxmlparsertoget.asp. However, when I logged into my server and tried to get to that page, I couldn't. I could get to it from my own machine, but not from the server. It was not until I used my internal 192.168 address (private internal address) of the web server that I actually got my script to work. So, instead of using my domain, I used http://192.168.1.1/thefileforthexmlparsertoget.asp. I hope that helps. If you think about it, it makes sense. The XML script is running server side in my script, so therefore, the SERVER is actually making the call, not the client.
I doubt that helps any, but it sure solved my problem after about an hour or two of digging.
Thanks, I have been trying to do this from many days. This has help me a lot. Once again thank you very much.
It works on our local test server but it is not working on our web server
and it's show the error msxml3.dll error '800c0005'.
if any body have any solution for this then plz mail me.
Thanks for the code
I've been waiting it for so long. It takes 1 month to get the reply.
Can I change the URL with the path such as /image ??
I find it becomes very slow for the script to be run if I use the complete URL like http://...
I place the image file in one folder inside the folder where I put the script. And probably it will be faster if I change URL with the path.
If I can change it, what is the command?
yes,it can deal with local image file, use it like this:
aa = getSize( Server.MapPath("emblem/chidori.gif"), width, height, depth, flType )
Good function! Thank you for share!
Have you had any luck in getting this script to work?
I am trying to fit this script into an asp.net page, and I am not able to make it function either. I was wondering if you were able to get it too work?
I used the "getsize" script on a shared web server, that I believe had Windows 2000 server on it, and it worked great. I then moved to a dedicated server that had Windows 2003 server on it and suddenly the script didn't work. No errors occured, it's just that I was not able to assertain the image properties anymore. Basically it can't identify the file type for some reason, and therefore can't return the image width and height. Would you know any reason why the getsize script would not work the same on a Windows 2003 server?
Tutorial of how to use the posted script to enter image dimensions into Access database fields.
Thank you, thank you!!!!!!!!!!!!!!!!!!!!!!!!!!
I created this code to scale an image to fit in a fixed space. Does anyone know a shorter script that will work?
<%'----routine to scale image if it is too big------
maxht = 50 'max height of image
maxwt = 130 'max width of image
If theHeight > maxht AND theWidth > maxwt then
imgfactor = theHeight / maxht
imght = int(theHeight / imgfactor)
imgwt = int(theWidth / imgfactor)
End If
If theWidth > maxwt AND theHeight < maxwt then
imgfactor = theWidth / maxwt
imght = int(imght / imgfactor)
imgwt = int(theWidth / imgfactor)
End If
If theHeight > maxwt AND theWidth < maxwt then
imgfactor = theHeight / maxht
imght = int(theHeight / imgfactor)
imgwt = int(imgwt / imgfactor)
End If
%>
<img src="<%=theURL%>" height="<%=imght%>" width="<%=imgwt%>" />
Hi,
I needed a way to find width and height of an image and came up with this script. I use loadpicture vbscript function to get image dimensions. The OS will return dimensions in metric scale (cm/1000). Windows screen res. is 96 dots per inch. 1 inch is 2.54 cm.
2.54/96*1000 = 26.4583
I know that the script above scales images but if you just need image dimensions this works fine. You can also get the type, handle, and palette of the image. Props are on msdn: click here
<%
dim iWidth, iHeight, iType
sub ImgDimension(img)
dim myImg, fs
Set fs= CreateObject("Scripting.FileSystemObject")
if not fs.fileExists(img) then exit sub
set myImg = loadpicture(img)
iWidth = round(myImg.width / 26.4583)
iHeight = round(myImg.height / 26.4583)
iType = myImg.Type
select case iType
case 0
iType = "None"
case 1
iType = "Bitmap"
case 2
iType = "Metafile"
case 3
iType = "Icon"
case 4
iType = "Win32-enhanced metafile"
end select
set myImg = nothing
end sub
' so if you whant to test it in asp just give the path to your image
ImgDimension(Server.MapPath("../.") & "\images\uc.gif")
response.write("Dimensions: " & iWidth & " x " & iHeight & "<br>")
response.write("Image Type: " & iType & "<br>")
%>
I think the script uses the HTTP headers to acertain certain stuff, which would be information which would have to be come by another way with a file sitting on your server, so in short, no. It's well beyond me powers. soz
salmo xxx
Thanks for the code
I've been waiting it for so long. It takes 1 month to get the reply.
Can I change the URL with the path such as /image ??
I find it becomes very slow for the script to be run if I use the complete URL like http://...
I place the image file in one folder inside the folder where I put the script. And probably it will be faster if I change URL with the path.
If I can change it, what is the command?
Hey y'all, I've just astounded myself by being able to use this peice o' code, so I thought I'd share it with you.
The code below calls the function and print the results.
Salmo xxx
<%
dim theURL, theWidth, theHeight, theDepth, theFlType, blnImageStuff
theURL = "http://www.notongues.com/flyer.jpg"
theWidth = 0
theHeight = 0
theDepth = 0
theFlType = ""
blnImageStuff = getSize(theURL, theWidth, theHeight, theDepth, theFlType)
if blnImageStuff then
response.Write(theWidth & "<br>")
response.Write(theHeight & "<br>")
response.Write(theDepth & "<br>")
response.Write(theFlType & "<br>")
else
response.Write("Hmm, that didn't seem to work...")
end if
%>
I'm newbie in ASP.
Can anyone tell me how to display the output of the width and height of the images?
Let say I have a jpg file, picture.jpg, and I store it in root directory of my web folder.
What command must I add to display the width and height of picture.jpg ?
Response.Write blablabla bla?
Hi Spanno;
- - I'm dealing with this same issue. I have developed a self-administrated flash photo gallery which accepts user-uploaded jpgs via an ASP server-side upload component (saFileUp). Unfortunately, some people are uploading progressive jpgs, which then do not show up in the flash interface. I DO have a COM object from SoftArtisans (ImgWriter), which can re-save JPG's in non-progressive format. The problem is, I have no way of determining (reading) the progressive property. Thus, I would have to indiscriminately re-save all jpgs as non-progressive, without knowing whether they were progressive to begin with. This results in the re-compression of existing jpgs and the consequent loss of image quality. Plus, the imgWriter's compression algorithm is not very good, and it often results in a larger file size for a poorer quality image. I was hoping someone might know of how I can read the progressive property at upload time and then I can either reject the file or auto-re-save it with the progessive property set to false.
Let me know if you figured this one out spanno! I will also re-post here if I can solve this...
This is great! Ive been looking for something like this for weeks! Thanks
But is there a way to get the size of the file? Purhaps in KBs? Thanks!
I used this in its original form, but this is a better version. Has anyone ever converted this the Javascript?
I have an ASP based content control utility with around 30 editors each usnig the system. We have a flash application that only takes in non-progressive images dynamically. I need to ensure that when an editor uploads a new image to our server it's in the non-progressive format. I looked over your code brriefly and noticed that you were checking bytes from the file to gather information about it. What byte information could I grab to check to see if the file's progressive or not?
Especially the line:
- obj.open "GET", URL, False
returns the error: invalid procedure call.
Could you please print your changes you made to make the code run?
Thanx a lot,
Flummi
Excellent code -- i'd been struggling with the original code and had a problem opening and reading the image files -- i swapped in this code and my routines ran the first time.
My travel pages contain hundreds of images and i need a quick way to add new images and display them across many different pages. so i wrote a routine that lets the user decide whether to display in thumbnail, caption only, or full size. until now, i didnt have an easy way of knowing the format, so i had to encode whether the image was horizontal or vertical -- now it works as it should, and will allow me to scale images in all fomats, too.
to see a sample of this in action:
http://cascoly.com/trav/turkey/Konya.asp
thanks
steve
This thread is for discussions of Retrieving Remote Image Properties in ASP.