|
YOUR FEEDBACK
Did you read today's front page stories & breaking news?
SYS-CON.TV SYS-CON.TV WEBCASTS |
TOP COLDFUSION LINKS Convert and Resize Images Using CFXImage
Convert and Resize Images Using CFXImage
By: Greg Bell
Feb. 5, 2002 12:00 AM
Many of today's Web pages take too long for users to download. We've all landed on a page, stared impatiently at a blank screen for a minute or two, and become so frustrated that we found another activity to occupy our time. After responding to the 25 e-mails left minimized on the desktop, we check the page again only to experience a very large image struggling to open before a page times out. We all know there's a correlation between page load time and page size: an increase in page size means an increase in page load time. Unfortunately, users don't increase the time they spend waiting on your site. Several studies show that most users won't wait longer than 20 seconds for a page to load before leaving the site. Keep the page size less than 50KB in order to keep a user interested in your site. Even though we know a smaller page size will retain more users, we often come across images larger than 200KB on our own sites. The problem stems from images being posted to the site either directly or through a content-providing template, unknown to the Web administrators. If the latter case is true, you may be able to use a custom tag called CFXImage to control image size and format.
Introducing CFXImage
Here are some things the tag can do through the actions invoked by the ACTION attribute as taken from the CFXImage documentation:
The tag also supports many file types:
How I Used CFXImage
After weeks of painstakingly fixing the images, I found a solution using CFXImage. CFXImage provides the functionality to resize and convert image formats as well as basically manipulate almost any image to your liking. Implementing CFXImage into my code was as painless as installing the custom tag on a ColdFusion Server. I first call the custom tag to read all the file attributes of the uploaded file through the READ action: <CFIF #UploadedFile# NEQ ''>The READ action will get many image attributes such as height, width, type, and size. Next, in Listing 1 I check if the image is already in GIF format. I also check if the image width is greater than 300 pixels. If both cases are true, the image is resized to a width of 300 pixels through the RESIZE action. If an image is the correct size and format, it's copied to the directory through the COPY action. To produce a sharper 8-bit image, set the BITCOUNT to 24. This allows the image to be proportionately resized back to 8 bits before it's saved back to a GIF format. Using this method, an 8-bit image will have increased colors resulting in a sharper output around the edges. However, this method doesn't work well with transparent images. Last, in Listing 2 a size check is performed on an image in the wrong format. The image is then converted into GIF format through the CONVERT action and resized through the RESIZE action if it's too large. Notice how easy the syntax is to convert an image into another format. Basically, it's a matter of making the file extension in the OUTPUT path the desired format and specifying an image type. You can use several different formats, such as GIF, JPEG, BMP, TIFF, and PNG. That's it. The images on Motorola Computer Group's Partner Web pages are now automatically controlled upon upload. As a result, the pages download faster and look cleaner, which can mean increased traffic throughout the site. Customers can spend more time viewing images on your site rather than checking their e-mail while your graphics load. The difficult task of resizing and converting files is just the tip of the iceberg for CFXImage. I encourage you to explore the options in this custom tag and see what it has to offer your site. CFDJ LATEST STORIES . . .
SUBSCRIBE TO THE WORLD'S MOST POWERFUL NEWSLETTERS SUBSCRIBE TO OUR RSS FEEDS & GET YOUR SYS-CON NEWS LIVE!
|
SYS-CON FEATURED WHITEPAPERS MOST READ THIS WEEK |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||