I was working on a script the other day at work and came upon the need to determine the dimensions of some images. Because the script was being written in VBScript, I was basically stumped, since, although I’ve figured out a great deal about simple scripting in VBScript, I’m still an absolute novice when it comes to file manipulation and the more advanced features available.
I hit the Web and started googling. Luckily, I came across a great solution on one of my first tries. The Four Guys from Rolla (which, if you’re working with VBScript and haven’t bookmarked this site, yet, I highly recommend it – they seem to be the number one authority on VBScript) offered a great tutorial, with code, explaining how to obtain the dimensions and a lot more information from any image.
In addition, they later expanded on the tutorial by explaining how to actually resize an image file with VBScript. You can check out both tutorials (along with code downloads) at their Web site.
- Determining Image Properties through ASP
- Proportional Image Resizing within a Constrained Area
Both tutorials use the default VBScript installation. You do not need to have .NET installed, nor do you have to install any extra components. Anyone with an IIS server can utilize these tutorials.
Why might you use these tutorials?
I was working on a newsletter template. The template is used to dynamically display articles for our newsletter. I have all of the articles for each issue stored in an XML file with three possible properties: the title of the article, the content of the article and an image to associate with the article. The image then excepts two properties: the location of the image and the caption that should appear with the article.
When the image is displayed within the articles, it’s laid out inside of a floated div, with the caption displaying directly below it. The problem, for those of you that are experienced in CSS, is that, unless you give a floated div an explicit width, it could potentially fill the entire width of its container element. Depending on how long the caption text is, I could end up with a very unattractive layout when a picture is embedded in an article.
The solution was to determine the width of the image, and then assign a slightly larger width (accounting for my margin and padding, and giving a little extra room for the caption) to my div. In order to do that, I used the gfxSpex function provided in the first tutorial to obtain the actual width of the image. Then, I used VBScript to output that width into the style tag for the image’s containing div.
Of course, if I were to use the second tutorial, too, I could actually obtain the width, then decide whether it’s a good size to embed in the article. If not, I could use the code from the second tutorial to resize the image before embedding it in the newsletter.
Neat, huh?