Determining Image Properties through ASPBy Mike Shaffer
|Determining Image Properties in an ASP.NET Web Page|
|Determining image properties in an ASP.NET Web page is much easier than determining image properties in an ASP page. For more information on ASP.NET's image capabilities, be sure to read:|
In a recent small project, I needed a way to display a series of images in a square area (150x150 pixels). The images in question are uploaded by our customers, and we have no real control over what size or color depth (or even type) they are. While it's easy to force a resize of an image to, say, 150 pixels wide x 150 pixels high, you run the risk of stretching the image incorrectly in one dimension. And if you simply decide to force the width of the image to 150 and leave the height proportional (or vice-versa), then you may exceed the boundaries of 150 pixels.
This is a problem.
Fortunately, the solution is relatively easy. All we need is a tool that will report the dimensions (and color depth) of any image. I looked out on the web and found several that will do the trick. Of those, some were relatively inexpensive, and some were even free, but all of them were distributed as components. While there is certainly nothing wrong with that, I wanted something that could run entirely within an ASP page, without needing the ability to register a component on the server (since many people are unable to do so).
Therefore, what follows is a set of routines (written in VBScript for your ASP pages) which will determine the height, width, and color depth of any GIF, JPG, BMP or PNG file. Additionally, these routines do not rely on the file extension to determine the graphic file-type. In other words, you can dynamically point this routine to any filename, regardless of name and extension, and determine if is a valid graphic file (actually, there is a very small chance [roughly one in 17 million] that a non-graphic file chosen at random will appear to be a graphics file, so it would be more appropriate to say that this routine determines PROBABLE graphic formats).
Here are some possible uses for these routines:
- intelligent image scaling (as mentioned above)
- Check user file upload, to see if the graphical image
adheres to your requirements (e.g. make your upload function
accept only files that are 640x480 or less and 256 colors)
- perform directory scans and gather information on
your graphics files
- Show graphical directories (using thumbnails) on the web
The comments in the routine should be sufficient to help you get started. The only real thing you need to
understand is the format of the call to the main routine,
strFlnm=> Filespec of file to read
lngWidth=> width of image
lngHeight=> height of image
lngDepth=> color depth (in number of colors)
strImageType=> type of image (e.g. GIF, BMP, etc.)
The routine returns FALSE if the file was not recognized as a valid image type, and TRUE if it is 'probably' a valid image file.
So there you have it, a complete new tool for your arsenal, with all VBScript source code, and best of all, IT'S FREE! (For a good ListServ that discusses graphics and ASP, be sure to join the ASP Graphics ListServ.)
Alert 4Guys reader Chris K. suggests that you create a file named |
imgsz.aspin text format