terrydoll
asked on
Coldfusion Dynamic Images (cfimage) Help
I am trying to create dynamic images within a loop; however, I receive an error. Here is my code:
Error:
Unable to cast an object of type java.lang.String to Image.
How do I loop through script without receiving error?
Thanks!
<cfset labelArray = arrayNew(1)>
<cfset labelArray[1] = 'Hello'>
<cfset labelArray[2] = 'World'>
<cfset labelArray[3] = 'USA'>
<cfset attr = StructNew()>
<cfset attr.font="Arial">
<cfset attr.size = 12>
<cfset attr.style = "bold">
<cfloop from="1" to="3" index="i">
<cfset "myImage#i#"=ImageNew("",200,30)>
<cfset ImageSetDrawingColor("myImage#i#","white")>
<cfset ImageDrawText("myImage#i#", labelArray[i],10,20,attr)>
<cfset ImageSetAntialiasing("myImage#i#","on")>
<cfset ImageRotate("myImage#i#",-90)>
<cfimage source="myImage#i#" action="write" destination="myImage#i#.png" overwrite="yes">
</cfloop>
<img src="myImage1.png">
<img src="myImage2.png">
<img src="myImage3.png">
Error:
Unable to cast an object of type java.lang.String to Image.
How do I loop through script without receiving error?
Thanks!
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
ASKER
Thank you! That's exactly what I needed!
That's passing in a string "myImage1", "myImage2", etc.. when CF expects an image variable ie #myImage1#, #myImage2#.
But you don't even need to dynamically name the variables, just the image file names. Reuse the same image variable, but make the "destination" unique
Open in new window