ColdFusion 8: Image Rotate Tip

I was playing with image rotation last night (I know, call me Mr. Excitement), and I found an interesting issue. Consider this code:

<cfset textImage=ImageNew("",200,200,"rgb","white")>

<cfset ImageSetDrawingColor(textImage,"black")>

<cfset attr=StructNew()> <cfset attr.size=30> <cfset attr.style="bold"> <cfset attr.font="ArialMT">

<cfset ImageSetAntialiasing(textImage, "on")> <cfset ImageDrawText(textImage,"Paris",50,75,attr)>

<cfset ImageRotate(textImage,30,2,2)>

<cfimage action="writeToBrowser" source="#textImage#">

I create a new blank image and then draw some black text and then rotate. I end up with this:

Notice the black background? I tried to fix it by using this for my image:

<cfset textImage=ImageNew("",200,200,"rgb","white")>

But it had no impact. Luckily I got some help from Adobe (thank you Hemant!) and he mentioned that if I switch my image type to ARGB instead of RGB, it will work. With that, I got this:

Not bad - but a bit ugly. The imageRotate function takes a 5th argument: interpolation. This basically sets the quality of the rotation. Adobe defaulted to the quickest method, "nearest." Switching to the highest, "bicubic", makes a real pretty rotation, but the leaves some "crud" on the sides:

Of course, I could crop that easily enough, but, still, a bit of a problem. The middle ground interpolation, bilinear, also leaves the same.

Archived Comments

Comment 1 by Chad posted on 7/12/2007 at 5:51 PM

Did Adobe explain why the color space change would "fix" this? It makes no sense to me. ARGB just adds the ability to have an alpha channel for transparency.

Now i dont know anything about these new funcitons and have not read the documentation, but i a suggestion to try.

Why specify the ImageSetDrawingColor on your second line of code? I think this is what is causing the black to show when you rotate it. Try switching to white before the rotate. Or finding a new way of defining the text color.

I would think that the function ImageDrawText would take a color attribute to defined the color of the text you are drawing.

Cant wait to get my hands on CF8! Love the new features so far!

Comment 2 by Tjarko posted on 7/12/2007 at 6:29 PM

Maybe a really stupid question but is the text-rendering of the ImageDrawText function in cleartype or bitmap?? I'm really excited about this because I was playing with the gFont cfx tag of efflare and if this function is already in CF8 I don't need this cfx tag.

Comment 3 by Tom Mollerus posted on 7/12/2007 at 9:24 PM

@Ray,

Have you tried setting the image's background color and clearing a rectangle before the rotation?

ImageSetBackgroundColor(image, color)
ImageClearRect(image, x, x, width, height)

Comment 4 by Raymond Camden posted on 7/12/2007 at 10:04 PM

Tom: I tried the first one, it didn't help, and in theory, shouldn't be necessary. When I used the imageNew and specified a BG color, that should have did it.

Comment 5 by Hemant Khandelwal posted on 7/26/2007 at 8:50 PM

The problem is with the imaging library that we are using here. We are checking if we can get a solution or workaround that can help here.

In principal what interpolation does is take a set of pixels around the source pixel to determine the target pixel. For exampl, nearest will take least number of pixels around source to determine target.

Thanks,
Hemant

Comment 6 by Terry Sasaki posted on 8/3/2007 at 5:40 AM

Hi Ray,

I'm a turtle and have just reached this post :)

ImageRotateDrawingAxis(textImage,30) before ImageDrawText() could be a temporary alternative.
Then ImageRotate() is not needed here.

cheers
-terry

Comment 7 by Jeff M posted on 10/23/2007 at 11:00 PM

This may be a stupid question, but why can I not get this to work. I am using CFMX 8, i paste your code in and I get nothing... Any test code I try, i get nothing. Its enabled in the admin, but I can not get anything regarding CFIMAGE to work.. Any tips on making sure CFIMAGE will work?

Comment 8 by Raymond Camden posted on 10/24/2007 at 1:56 AM

So you can't get any cfimage stuff to work at all? Did you try other examples?

Comment 9 by cc posted on 4/8/2008 at 11:11 PM

When I rotate an image and then write to file, it seems like the image file size is reduce even after I set the quality of the imagewrite to the highest value for quality. Is there any tip on how to make the image quality remain the same, after I rotate and save an image file?

Thanks.

Comment 10 by Raymond Camden posted on 4/9/2008 at 3:18 PM

If you are setting the quality to the highest, then that's all I can recommend for now. Have you tried it with 8.0.1?

Comment 11 by Joseph Lee Hunsaker posted on 10/22/2009 at 11:30 AM

To get rid of the black around the edges you need to have a border of transparency around the image. In other words, you need to have a transparent image and no part of that image can touch a border.

Comment 12 by David posted on 9/11/2012 at 4:08 AM

Have you noticed that your image didn't rotate by 30 degrees, but by just 2 degrees. I've found that when using the x and y parameters I have to the the rotation AFTER them.
Anyone else noticed this?

Comment 13 by Raymond Camden posted on 9/11/2012 at 5:36 AM

I thought maybe it was just a bad screen shot, but I can't test this now as my local Apache server doesn't seem to be serving up cfimage correctly. I'll need to fix that I suppose.

Comment 14 by Raymond Camden posted on 9/11/2012 at 5:39 AM

Ok, got a test working. So - yeah - this is weird. When I do just 30, it is really obvious. When I do 30,2,2 - it is different - but the specs say 2,2 is the X/Y is the default.

I'll file a bug report.