Web-based Image Manipulation

Thursday, October 3, 2002

I've found a way to provide a free ASP server-side image manipulation service, using ImageMagick. I tried first using Corel PhotoPaint's CorelScript object, but it worked unreliably at best.

I wasn't sure if I could easily use ImageMagick's tools, because it doesn't provide an OLE object for VBScript in ASP, only for use in WSH or VB. Of course it supports C, C++, Perl, etc., etc.

Anyway, here's how it ends up working:

  1. Download and install ImageMagick on the server
  2. Make sure your IIS account has permssions to RW in the image folder
  3. Use the WScript OLE object to call the .EXE ImageMagick tools
  4. Create a nice interface to let people easily perform operations

Download and install Image Magick

Download from www.ImageMagick.org, and follow the (very simple) instructions to install.

Check folder permissions

Do this with caution. You don't want to give the IIS anonymous account pemission to modify local files, so you should create a special account just for this folder, and specify that account as the IIS anonymous account for those subfolders. Better way of doing this? Let me know.

Use the WScript object

Here's how I do it:

<%
 'Open the file, flipping the forward slashes in the string
 strFile = Replace(Request.QueryString("f"),"/","\")

'If a file was specified, work with it
If Len(strFile) > 0 then
  Set Magick = Server.CreateObject("WScript.Shell")
  pathStart = "C:\Images\"
  progpath = """C:\Program Files\ImageMagick\"
  strFileName = Left(strFile,Instr(strFile,".")-1)    'Strip the extension, so the file name can be modified

  Select Case Request.QueryString("mode")
  Case "thumb"

   'Build the options for convert.exe
   strOperation = progPath & "convert""" _
      & " -normalize " _
      & " -size 200x200 " _                           'Not the final size, just makes it run faster
      & """" & pathStart & strFile & """" _
      & " -resize 200x200 " _                         'This doesn't make a square, those are max values,
      & "+profile ""*"" " _ 'so the biggest gets set
      & "-quality 90 " _
      & """" & pathStart & strFileName & "_thumbnail.jpg"""     'Append a thumbnail extension

  Case "resample"
  'Set the resample size for the specified size
   If Request.QueryString("side") = "height" then
    strDims = "x" & Request.QueryString("Size")
   else
    strDims = Request.QueryString("Size") & "x"
   end if
   strOperation = progPath & "convert""" _
      & " -normalize " _
      & """" & pathStart & strFile & """" _
      & " -resize " & strDims & " " _
      & "+profile ""*"" " _
      & "-quality 90 " _
      & """" & pathStart & strFileName & ".jpg"""
 
  Case "rotate"   
   intRotate = Request.QueryString("degrees")
   strOperation = progPath & "convert""" _
      & " -normalize " _
      & " """ & pathStart & strFile & """" _
      & " +profile ""*"" " _
      & " -quality 90 " _
      & " -rotate " & intRotate _
      & " """ & pathStart & strFile & """"
  Case else
  End Select
  Response.Write Magick.Run(strOperation, , True)       'Here's the part where it actually runs;
 End if 'returns True if successful

'Go back to the requesting page
 Response.Redirect Request.ServerVariables("HTTP_REFERER")

%>

Create a nice interface

Make it really, really simple. No parameters even to set, just click buttons.

Here's a screen shot; I'll try to work up a live demo sometime, and stick it in here instead.

Sample image and toolbar

The buttons, left-to-right, are Create Thumbnail, Resample, Rotate counter-clockwise, Rotate clockwise, and Delete. All they do is link to the multisize script, telling it what path, image, and mode should be done.

4 Comments