Rob Gonda's Blog

Better image manipulation quality with ASPJpeg

ColdFusion runs on top of Java and therefore can leverage of native Java classes such as BufferedImage and ImageIO. There are a few wrapper libraries that allow you to interact with them. The core relies on CreateObject("java", "java.awt.image.BufferedImage") and CreateObject("Java", "javax.imageio.ImageIO"), for the text manipulation you can use CreateObject("Java", "java.awt.Font"), and for reading and writing files you can use CreateObject("Java", "java.io.FileInputStream"). All this will be replaced with native image manipulation with the release of Scorpio (CFMX8).

However, I ran into image quality problems when resizing large images to smaller thumbnails... the resizing algorithm used by these native Java technologies is not optimal, therefore it really doesn't matter which wrapper you use, you will ultimately run into the same issues.

After much research, I chose to install a COM object used by ASP called ASPJpeg. As you probably know, ColdFusion can also leverage COM objects, and let me tell you, the quality of ASPJpeg is superb.

It really couldn't be any simpler. First initialize the COM Object:

<cfset myImage = createObject("COM","Persits.Jpeg") />


Then you may open an existing image:
<cfset myImage.Open("/path/to/original/file.ext") />


You may get the original width and height
<cfset orig_size_w = myImage.OriginalWidth() />
<cfset orig_size_h = myImage.OriginalHeight() />


Set new quality and resolution
<cfset myImage.ResolutionX(72) />
<cfset myImage.ResolutionY(72) />
<cfset myImage.Quality(80) />


Set a new width and height
<cfset myImage.Width(new_large_w) />
<cfset myImage.Height(new_large_h) />


Crop the image
<cfset myImage.Crop(crop_w, crop_h, starting_w, starting_y) />


and finally save the image to disk
<cfset myImage.Save("/path/to/original/file.ext") />


As you can see, it's extremely easy to leverage COM objects to read, resize, crop, modify quality ... ASPJpeg is capable of many other functions, but these should get you started.

Enjoy good quality!

TrackBacks
There are no trackbacks for this entry.

Trackback URL for this entry:
http://www.robgonda.com/blog/trackback.cfm?C643A1E6-3048-7431-E49A13670728466D

Comments (Comment Moderation is enabled. Your comment will not appear until approved.)
This blog is running version 5.9.003. Contact Blog Owner