Solved

Coldfusion Dynamic Images (cfimage) Help

Posted on 2010-11-12
3
528 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Hi, I will be creating today a basic tutorial on how we can create a Mail Custom Function and use it where ever we want. The main advantage about creating a custom function is that we can accommodate a range of arguments to pass to the Function and …
Recently while working on a project I got a very annoying cfdocument has no body error message. I had never seen this error before. So I checked the code. The code was pretty simple; it was Just showing me the cfdocumnt tag and inside that tag a …
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

770 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