How can you create transparent images using ColdFusion 8's new image manipulation functionality? I get this question asked of me sometimes and I feel like the ColdFusion 8 documentation is especially unclear as to how this is done. In fact, I think the ColdFusion 8 documentation is unclear as to whether or not this is even possible. Look at one of the Notes that is part of the documentation:
Note: If you specify the ARGB image type, the image is white; however, if you specify RGB or grayscale, the image is black. To create blank images consistently, use the canvasColor parameter.
This statement isn't necessarily unclear; I think it's just downright misleading. At first glance, it looks like the canvas type, ARGB or RGB, simply changes the background color from white to black. Then, it says to create a "blank" canvas consistently, use the cavasColor parameter. What does "blank" mean? Transparent? Empty? If the first sentence talked about color, then it might lead that the second sentence talks about transparency?? This is all very poorly worded and doesn't mention "transparent" properties at all.
You might think that maybe it has to do with the default drawing color, but it does not. Whether you create a canvas of type RGB or ARGB, the default drawing color is still white.
Despite the confusing live docs, rest assured that creating a transparent canvas (transparent image) in ColdFusion 8 is not only possible, it is, in fact, quite simple. The trick is to use the canvas type ARGB and to not specify a canvas color at all. The "A" in "argb" stands for "alpha" and provides the canvas with an alpha channel (for transparency). Then, by not specifying the canvas color, which is an optional argument, we make sure not to cover the transparent background with a color fill.
Take a look at the following example:
Launch code in new window » Download code as text file »
In this code, the BODY of the document has a striped background so that we can easily see whether or not the image canvas is transparent. In the ImageNew() method call, we specify the image type, ARGB, and do not pass in the 5th optional argument, which is the background color (remember, we don't want a background color). Running the above example, we get this output:
| | | | ||
| | ![]() | | ||
| | | |
The transparent image is successfully created and written to the browser. Of course, we didn't have to write it to the browser, we could have written the image to the file system instead.
Download Code Snippet ZIP File
Comments (2) | Post Comment | Ask Ben | Permalink | Other Searches | Print Page
Just wondering in what scenario you'd want to generate a transparent image on the fly? Can't think of anything off the top of my head. Either way, thanks for another great tutorial!
Posted by Dave Hoff on Nov 19, 2007 at 9:25 AM
@Dave,
I don't think it's that often that you would ever need your "primary" graphic to be transparent; however, if you were going to build a graphic using multiple parts, on the fly, you might want to create a bunch of transparent images which would then be pasted onto the "primary" image.
Now, you might just ask, why not work directly on the primary image itself? I think there is something nice to being able to compartmentally construct an image using smaller, individual images, and then merging them all together. This allows for image aspects to be reused among different algorithms and gives you the added bonus of being able to more easily determine the height and width of a given image sub-component.
Posted by Ben Nadel on Nov 19, 2007 at 9:33 AM