I needed to generate invoices based on a template, with data at many different positions on the image, then send it directly to our printer. I wrote this helper function that accepts an image.cfc instance (one that already has my blank image loaded) and writes text to a specific X / Y location on the image. Check it out, and hope it helps someone!
<br /> <cffunction name=<span class='cc_value'>"imgWriteText"</span> output=<span class='cc_value'>"false"</span> hint=<span class='cc_value'>"This is a sub method that will write text over an image in memory at a specific location, font, weight, and style."</span> returntype=<span class='cc_value'>"struct"</span>><br /> <cfargument name=<span class='cc_value'>"imageVar"</span> required=<span class='cc_value'>"yes"</span> type=<span class='cc_value'>"struct"</span> hint=<span class='cc_value'>"This is the variable name that contains our Image.cfc object with our blank invoice image already loaded."</span>><br /> <cfargument name=<span class='cc_value'>"text"</span> required=<span class='cc_value'>"yes"</span> type=<span class='cc_value'>"string"</span> hint=<span class='cc_value'>"This is the actual text to be written"</span>><br /> <cfargument name=<span class='cc_value'>"xpos"</span> required=<span class='cc_value'>"yes"</span> type=<span class='cc_value'>"numeric"</span> hint=<span class='cc_value'>"This is the X position we are going to write our text at"</span>><br /> <cfargument name=<span class='cc_value'>"ypos"</span> required=<span class='cc_value'>"yes"</span> type=<span class='cc_value'>"numeric"</span> hint=<span class='cc_value'>"This is the Y position we are going to write our text at"</span>><br /> <cfargument name=<span class='cc_value'>"type"</span> required=<span class='cc_value'>"no"</span> default=<span class='cc_value'>"System"</span> type=<span class='cc_value'>"string"</span> hint=<span class='cc_value'>"This is the type of font, either system or truetype"</span>><br /> <cfargument name=<span class='cc_value'>"font"</span> required=<span class='cc_value'>"no"</span> default=<span class='cc_value'>"Lucida Console"</span> type=<span class='cc_value'>"string"</span> hint=<span class='cc_value'>"If a system font is specified, this is the name of the font. If a truetype font is specified, provide the full path to the font."</span>><br /> <cfargument name=<span class='cc_value'>"size"</span> required=<span class='cc_value'>"no"</span> default=<span class='cc_value'>"40"</span> type=<span class='cc_value'>"numeric"</span> hint=<span class='cc_value'>"Font size to be written"</span>><br /> <cfargument name=<span class='cc_value'>"color"</span> required=<span class='cc_value'>"no"</span> default=<span class='cc_value'>"black"</span> type=<span class='cc_value'>"string"</span> hint=<span class='cc_value'>"Color of the font to be written - name format"</span>><br /><br /><br /> <!--<span class='cc_comment'>-// Declare all local variables //---><br /></span><cfset var myFont = /><br /> <cfset var myColor = /><br /> <cfset var myString = /><br /><br /> <cfsilent><br /> <span class='comment'></span><br /> <cfif Len(arguments.text)><br /> <span class='comment'></span><br /> <cfswitch expression=<span class='cc_value'>"#arguments.type#"</span>><br /> <cfcase value=<span class='cc_value'>"system"</span>><br /> <cfset myFont = arguments.imageVar.loadSystemFont(<span class='cc_value'>"#arguments.font#"</span>, #arguments.size#, <span class='cc_value'>"plain"</span>) /><br /> </cfcase><br /> <cfcase value=<span class='cc_value'>"truetype"</span>><br /> <cfset myFont = arguments.imageVar.loadTTFFile(<span class='cc_value'>"#arguments.font#"</span>, #arguments.size#, <span class='cc_value'>"plain"</span>) /><br /> </cfcase><br /> </cfswitch><br /><br /> <span class='comment'></span><br /> <cfset myColor = arguments.imageVar.getColorByName(<span class='cc_value'>"#arguments.color#"</span>) /><br /><br /> <span class='comment'></span><br /> <cfset myString = arguments.imageVar.createString(<span class='cc_value'>"#arguments.text#"</span>) /><br /><br /> <!--<span class='cc_comment'>-// set our strings font //---><br /></span> <cfset arguments.imageVar.setStringFont(myString, myFont) /><br /><br /> <span class='comment'></span><br /> <cfset arguments.imageVar.setStringForeground(myString, myColor) /><br /><br /> <span class='comment'></span><br /> <cfset arguments.imageVar.drawString(myString, #arguments.xpos#, #arguments.ypos#) /><br /> </cfif><br /> </cfsilent><br /><br /> <cfreturn arguments.imageVar /><br /><br /> </cffunction><br />








Recent Comments