Link to home
Start Free TrialLog in
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!
Avatar of _agx_
_agx_
Flag of United States of America image

>> <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
Avatar of _agx_
_agx_
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of terrydoll
terrydoll

ASKER

Thank you!  That's exactly what I needed!