Monday, December 29, 2008

Resampling images using PowerShell and GraphicsMagick

I needed to resample several images which were scanned at a higher a resolution than expected, so here's a PowerShell script that I created to resample them to a lower resolution using GraphicsMagick. It's probably not the most elegant, but it seems to work for my requirement. Here it is:

$gm = "C:\Program Files\GraphicsMagick-1.3.3-Q8\gm.exe"
$maxResolution = 100
$defaultResolution = "72x72"

foreach ($file in ls *.jpg) {
echo Processing: $file.Name
$resolutionLine = & $gm identify -verbose $file | Select-String -pattern "Resolution:"
echo $resolutionLine
if ($resolutionLine -match "(\d+)x(\d+)") {
if ([int]$matches[1] -gt $maxResolution -and [int]$matches[2] -gt $maxResolution) {
$file.CopyTo($file.FullName + ".bak")
& $gm convert -resample $defaultResolution -units PixelsPerInch $file $file
echo Resampled: $file.Name
}
}
}


To use, copy the code and save it as a *.ps1 file and run it from the directory you wish to resample images, making any edits as needed. (Note: PowerShell and GraphicsMagick must already be installed.) Use at your own risk, I make no guarantees about this script.

Please leave a comment if you find this script helpful or have any suggestions for improvement.

Thursday, December 04, 2008

Thoughts on error handling in classic ASP

Most of our code base is written in classic ASP and VBScript. And, most of it was written without any sort of error logging. We've been coming to the realization that one of our applications definitely needs some sort of error logging, so I started doing some research.

I've wondered before about adding error logging and friendlier error messages for our other ASP applications, but didn't know of a simple way to do global error handling. But, while researching I found that it's easier than I had imagined.

The trick is to create an ASP page and set it as the default handler for 500;100 errors from IIS. You can use Server.GetLastError() to retrieve information for the last error. You also get to keep context information from the Request and other objects. So, after calling Server.GetLastError(), just log the relevant error and context info, send notification e-mails and display a friendly error message.

Speaking of friendly error messages, I think this error page from Southwest.com is a good example of simplicity and effectiveness:

Southwest.com error page