Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Coldfusion Dynamic Images (cfimage) Help

Posted on 2010-11-12
3
Medium Priority
?
560 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 800 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

Simple, centralized multimedia control

Watch and learn to see how ATEN provided an easy and effective way for three jointly-owned pubs to control the 60 televisions located across their three venues utilizing the ATEN Control System, Modular Matrix Switch and HDBaseT extenders.

Question has a verified solution.

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

Introduction This article explores the design of a cache system that can improve the performance of a web site or web application.  The assumption is that the web site has many more “read” operations than “write” operations (this is commonly the ca…
Lease-to-own eliminates the expenditure of hardware replacement and allows you to pay off the server over time. Usually, this is much cheaper than leasing servers. Think of lease-to-own as credit without interest.
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

661 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