Note: This blog post is from 2012. Some content may be outdated--though not necessarily. Same with links and subsequent comments from myself or others. Corrections are welcome, in the comments. And I may revise the content as necessary.Are you having slow ColdFusion pages and wondering what may be the cause? There can of course be many root causes, but a common one that I'm finding lately as I help people is due to using certain of CF's image processing features, especially resizing such as to create thumbnails after a file is uploaded (or when many files are uploaded).
Such folks may be using the CFIMAGE action="resize" tag, or the imageResize() or ImageScaleToFit() functions to do resizing. (Or they may be also processing images using ImageRotate, ImageShear, or ImageTranslate, though the defaults for those are not problematic like the resize/scale tag/function processing).
The "problem" (if this is the cause of a slow page) is due to a default "interpolation" setting for CFIMAGE resizing, imageResize, and ImageScaletoFit. The default may not perform well at all. The good news is that the value is configurable, and you can test to compare quality/performance of difference values, as will explained below. There are still some other things to consider also. (If you're currently using CFIMAGE to do resizing, jump to the last section of this entry to see an example of code switching from the "slow" approach to the faster one. But really, you ought to read the rest of this entry to understand what's being proposed.)
While I offer all the info here for your consideration, if you need help implementing the solution, or better understanding how to find and resolve these or other problems affecting your CF server performance, see more on my CF server troubleshooting consulting services.
Problem: Default "Interpolation" value is "highestquality"
Solution: There are alternative values for "interpolation"
So the first "good news" is that there is a range of options one can set for this interpolation value, and the opposite one is "highestperformance". For many people, changing to that value has no negative impact on quality (again, that may depend on the image, its size, what you're using the result for, etc.) but it offers a GREAT improvement in performance. You need to test for yourself.
Implementation 1: Controlling Interpolation in the CFIMAGE tagSo if you use CFIMAGE action="resize", what can you do? Well, the "bad news" is that for CF8 and CF9, the CFIMAGE tag has no means for you to affect the interpolation value. It has no INTERPOLATION attribute.
Some great news is that CF10 adds this attribute (for use specifically with action="resize"), but note that the default is still "highestquality", so you will want to consider changing it. See the docs for CF10 on CFIMAGE for the range of INTERPOLATION attribute values.
If you're on CF 8 or 9, though, you will need to consider changing your code to use the imageResize() function instead, which is discussed in the next section.
(Note that, for now, only the action="rewrite" option for CFIMAGE makes any use of interpolation at all.)
Implementation 2: Controlling Interpolation in the imageResize(), imageScaleToFit(), and other image processing functions
In all three releases (CF 8, 9, and 10), the imageResize() function--and the perhaps less-well-known imageScaleToFit()--all have an available (optional) argument for controlling the interpolation value. It's the 4th arg in both imageResize() and imageScaleToFit(). But in all three releases, the value for each again defaults to "highestquality".
Specifying it as "highestperformance" (or some value between the two) again may have a noticeable effect on improving performance without much loss of image quality. See the docs on these functions (here's a link to the CF9 CFML reference.) Test things for yourself, and report here if it helps.
(As an interesting aside, the docs suggest that while imageRotate(), imageShear(), and imageTranslate() functions also have an optional interploation argument, theirs each defaults to the fastest of the values they offer. One wonders why Adobe chose for resizing and scaling to opt for the opposite values.)
Changing from CFIMAGE will require reading and writing the file a new way
Finally, if you do have to change from using CFIMAGE to the imageResize() function (not required for CF10), you may also need to deal with how to load the image from disk into memory, and write it out, if you're also doing that in the one tag.
CFIMAGE has both a SOURCE and DESTINATION attribute which can point to a file on disk to be read/written. The imageResize() function's does not support that. Instead, its first ("name") argument instead expects to be passed a variable with an image in it, such as is created by other CFML image-processing tags or functions. imageNew() can read an image from disk into memory.
Similarly, the result of calling imageResize() is a manipulation of that image object in memory. The function has no means to write out the result. That can be done with either imageWrite() or another CFIMAGE action="write".
So to change from using the tag-based resize to function-based, you may need to add a line of code to load the image from disk, such as using imageNew(), and one to write the resulting image to disk, such as using imageWrite().
Consider the following tag-based resize code, which reads, resizes, and writes out an image (available in the CFDOCS directory, if you installed that with ColdFusion):
That could be re-written as either:
or (for those who prefer script to tags):
Again, test things for yourself to determine whether changing to highestperformance results in an image quality you (and your users) can find acceptable. See the docs for a range of alternative options to consider. And if you move up to CF10, note that you can solve the problem with CFIMAGE action="rewrite" by just adding the new INTERPOLATION attribute right onto the tag.
For more on the many (many) new features of CF10, check out my blog entry on the topic.
And again if you need help solving these or such other problems affecting your CF server performance, let me know how I can help, via my CF server troubleshooting consulting services.
Of course I welcome and encourage followup and feedback here on this particular problem. And for solutions to other such knotty CF troubleshooting challenges, see my cf911.com site of CF server troubleshooting resources.
For more content like this from Charlie Arehart:
Need more help with problems?
- Signup to get his blog posts by email:
- Follow his blog RSS feed
- View the rest of his blog posts
- View his blog posts on the Adobe CF portal
- If you may prefer direct help, rather than digging around here/elsewhere or via comments, he can help via his online consulting services
- See that page for more on how he can help a) over the web, safely and securely, b) usually very quickly, c) teaching you along the way, and d) with satisfaction guaranteed