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)
%>