Solved

Coldfusion Dynamic Images (cfimage) Help

Posted on 2010-11-12
3
524 Views
Last Modified: 2013-12-24
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!
0
Comment
Question by:terrydoll
  • 2
3 Comments
 
LVL 52

Expert Comment

by:_agx_
ID: 34126196
>> <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

0
 
LVL 52

Accepted Solution

by:
_agx_ earned 200 total points
ID: 34126198
Let's try that code again (removed EE text formatting from code)

...
<cfloop from="1" to="3" index="i">
     <cfset myImage =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

0
 

Author Closing Comment

by:terrydoll
ID: 34127112
Thank you!  That's exactly what I needed!
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

I spent nearly three days trying to figure out how incorporate OAuth in Coldfusion for the Eventful API. Hopefully, this article will allow Coldfusion Programmers to buzz through the API when they need to. Basically, what this script does is authori…
Meet the world's only “Transparent Cloud™” from Superb Internet Corporation. Now, you can experience firsthand a cloud platform that consistently outperforms Amazon Web Services (AWS), IBM’s Softlayer, and Microsoft’s Azure when it comes to CPU and …
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now