Avatar of terrydoll
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:

<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">

Open in new window


Error:

Unable to cast an object of type java.lang.String to Image.

How do I loop through script without receiving error?


Thanks!
Web ServersColdFusion Language

Avatar of undefined
Last Comment
terrydoll

8/22/2022 - Mon
_agx_

>> <cfimage source="myImage#i#"

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

<cfloop from="1" to="3" index="i">
     <cfset [b]myImage [/b]=ImageNew("",200,30)>
    <cfset ImageSetDrawingColor(myImage,"white")>
    <cfset ImageDrawText(myImage, labelArray[i],10,20,attr)>
    <cfset ImageSetAntialiasing(myImage,"on")>
    <cfset ImageRotate(myImage,-90)>

    <cfimage source="#myImage#" action="write" destination="myImage#i#.png" overwrite="yes">
</cfloop>

Open in new window

ASKER CERTIFIED SOLUTION
_agx_

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
terrydoll

ASKER
Thank you!  That's exactly what I needed!
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck