Manipulating / Resizing Images using Classic ASP

By Classic ASP do you mean the COBOL of early 21st Century web development that hasn’t expanded functionality in what seems like forever?

Yes that Classic ASP, the one that you may encounter when maintaining enterprise codebases.

What magic incantation do I have to use?

Classic ASP supports 3 main languages VBScript, JScript and PERLScript which although they are not known for their strong OO they can access COM/OLE/System objects and this is how we are going to manipulate images.

Standing on the shoulders of giants

There are a few components that we can leverage for this end but this tutorial will cover the legendary and reliable Image Magick suite, download the version that you desire (I used the q16 winx64 static version) , restart your server and then fire up your notepad.

The code

In the below code we will be resizing the height of an image while preserving the aspect ratio

 

   1:  'Declare the variables
   2:  dim srcImg, fs, imageMagick, tmpVar,targImgPath, imgPath,image_width, image_height, newHeight
   3:   
   4:  'New image height
   5:  newHeight = 400             
   6:   
   7:  'Create a File System Object for manipulating files
   8:  Set fso= CreateObject("Scripting.FileSystemObject")
   9:   
  10:  'Declare the path of the image we wish to manipulate
  11:  targImgPath = "c:\\image.jpg"
  12:   
  13:  'Get the path of the image taht we wish to manipulate by using the Server.MapPath function
  14:  imgPath = Server.MapPath(targImgPath)
  15:   
  16:  'Get image dimensions using the load picture operation
  17:  'Get the image contentthat we wish to manipulate by using the fs.GetFile object
  18:   
  19:  set srcImg = loadpicture(fs.GetFile(imgPath))
  20:   
  21:  'Get the current image height and width        
  22:  image_width = srcImg.width
  23:  image_height = srcImg.height
  24:         
  25:  'Calcualate the aspect ratio of the image
  26:  tempVar =  image_width / image_height 
  27:   
  28:  'Calculate the new width of the image 
  29:  image_width = Round(newHeight * tempVar) 
  30:   
  31:  'Create an Image Magick object to manipulate the image
  32:  Set imageMagick = Server.CreateObject("ImageMagickObject.MagickImage.1")
  33:   
  34:  'Resize the image to the desired width and height and output the image to where desired
  35:  imageMagick.Convert Server.MapPath(imgPath, "-resize" , image_width&"x"&image_height, "C:\\resizedimage.jpg")

Simple! and only the tip of the iceberg,  Image Magick has lots of functions that you can invoke in a similar manner Smile


Comments

  1. WOWWWW God article..!!

Leave a Reply

Your email address will not be published / Required fields are marked *

d1b8D

Please type the text above: