ImageCFC: Documentation: Method Description

imageCFC.addText()

Description

adds text to an image.

Returns

A structure containing the following fields:

keyvalue
errorCode0 for success, non-zero for failure. Always returned, but not really used if the throwOnError option is set to true.
errorMessageA description of the error. Always returned, but not really used if the throwOnError option is set to true.
imgA java BufferedImage object is returned if no output file is specified

Function Syntax

addText(objImage,inputFile,outputFile,x,y,fontDetails,content,jpegCompression)

ParameterRequired?DefaultDescription
objImage YES - A java image object, or a blank string.
inputFile YES - File path or URL to an image, or a blank string.
outputFile YES - File path to write the output, or a blank string.
x YES - X coordinate where the text is to be placed.
y YES - Y coordinate where the text is to be placed.
fontDetails YES - A struct containing font information, containing these keys:
  • fontFile (optional) - path to specific truetype font file
  • fontName (optional) - logical java font name, one of "Serif" (default), "SansSerif", or "Monospaced". Used when fontFile is not specified.
  • style (optional) - string reference to a java font style constant, used only when also using the fontName attribute. Can be "font.PLAIN", "font.BOLD", or "font.ITALIC" - probably case senstive, see Java documentation (java.awt.Font) for further details.
  • size (optional) - Font size in points, default is "12"
  • color - Color to be used, either an RGB value, like "ffcccc", or a named color, like "black" (default)
content YES - The string to be added to the image.
jpegCompression NO defaultJpegCompression jpeg compression quality to use if writing a jpeg file. 0-100. 100 is the highest quality.

You must supply either an image object or a file path to a source image.

Example

<cfset imageCFC = createObject("component","image")>
<cfset fontDetails = StructNew()>
<cfset fontDetails.fontName = "Serif">
<cfset fontDetails.style = "font.BOLD">
<cfset fontDetails.color = "maroon">
<cfset fontDetails.size = 20>

<cfset results = imageCFC.addtext("", "#ExpandPath(".")#/myimage.jpg", "#ExpandPath(".")#/myimage2.jpg", 10, 10, fontDetails, "Sample Text")>

Special Considerations

When you use the addText() method with a truetype font file, you will find that java creates temp files somewhere on your system. In the Linux world, it seems to be /tmp and the files are named JF*.tmp

These don't get cleaned up!!! You'll need to write some kind of script to clean them up manually. For example, I use this as a cron job:

* * * * * /usr/bin/find /tmp -cmin +2 -name \*JF\*.tmp | /usr/bin/xargs /bin/rm

For further discussion, see this thread in the imageCFC forums:


Please read this project's LICENSE

This is an open source project written by Rick Root. Please visit The Coldfusion Open Source Blog for more information.

Please report bugs, make suggestions, and send compliments to me via email.