[!] ****** ImageMagick Functions ******** ** FILE: IM_Functions.inc ** Version: 1.0 ** By: Terry Wilson, Terryfic.com ** Description: Use the ImageMagick suite of programs for Extending WebDNA. Sandbox environment or primary WebDNA install. More info on ImageMagick, including installers and instructions can be found at their website, http://www.imagemagick.org Advanced users: Learn more about the power of ImageMagick at the above site, join the mailing list, and eventually create your own custom functions. ** How To Use: can be "[included]'ed" into the top of any WebDNA template, or can be added to your "FunctionDefs/" directory to be used as a pre-parse script for global access. ** REQUIREMENTS: WebDNA 6.x and above ** function naming convention prefix "im"; those for Sandbox users appended with SB ** Part of IMFunctions.zip package with ready-made usage template ** Change History: Initial revision: Terry Wilson June 17, 2009 ************ NOTE for SANDBOX Environments ******** For security reasons, Sandbox environments cannot run the [shell] script directly. However, you can run all of these IM scripts quite easily by having just one sandbox script configured by your administrator. Please refer to the ReadMe file for further Sandbox instructions. **************************************************** NOTES: 1. The first function includes a practical example. The rest of them don't, but you should be able to understand how it works based on the first one. The template IM_Manipulation.html contains more comprehensive working examples, and is an entire template ready to use for manipulating individual files. Sandbox users: The usage info for each function shows text variables being set. If you are sending these variables in from a form submission, you won't need to explicitly set them again... they are already there. So conceivably, the Sandbox usage can be as simple as [imResizeImageSB]. 2. RGB or grayscale is assumed 3. Each function requires a fileIn and fileOut definition. If you want to overwrite the original file, just make the fileOut the same as the fileIn. 4. The Sitewide Setting below needs to be confirmed or edited to reflect the path to ImageMagick on your server. +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ +++++++++++++++++ SITEWIDE SETTING: PathToIM ++++++++++++++ +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ ** Description: defines the path to ImageMagick. Likely, this is /sw/bin/ ** Used in the non-sandbox functions below. Sandbox users: does not apply. ** Note that Imagemagick is not one app, but a suite of apps; therefore this path is to Imagemagick's parent directory, excluding the application name itself, which is taken care of in the individual functions. All of these functions use the convert program, but for future expansion, we will likely use some of the other ones. [/!][text]PathToIM=/sw/bin[/text][!] *************************** THE *************************** ************************ FUNCTIONS ************************ *********************************************************** +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ FUNCTION Name: imList ** Description: List all function names availabe in this Library. ** Input: none ** ouput: function names [/!][function name=imList] [return][grep search=%09&replace=][grep search=[%0D%0A]&replace=
] imList - List all function names available in this library. Functions appended with SB are for Sandbox installations. imResizeImage/imResizeImageSB - Proportionately resize images to your chosen maximum width and height. (Does not enlarge.) imMakeThumb/imMakeThumbSB - Proportionately creates thumbnails for images, within your maximum width or height. imImprintText/imImprintTextSB - imprints solid text onto your image, as in a credit line or caption. imRotate/imRotateSB - Rotates image in 90 degree increments. imMirror/imMirrorSB - Mirrors image according to input, either vertically or horizontally. imChangeFormat/imChangeFormatSB - Change format to jpg, gif, or png. [/grep][/return] [/function][!] +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ FUNCTION Name: imResizeImage ** Description: Proportionately resize images to your chosen maximum width and height, so you can upload without having to worry about final size beforehand. ** This will NOT enlarge files smaller than your maximum values. ** Code will retain the existing file format. To change file format, use the imChangeFormat function. OR, renaming the destination file to the preferred extension will automatically change the format as well. ** Input: maxWidth = (maximum pixel width) maxHeight = (maximum pixel height) FileIn = (path to original file) FileOut = (path to final destination) ** usage: [imResizeImage FileIn=&FileOut=&maxWidth=&maxHeight=] ** Sandbox usage: [text multi=t]FileIn=&maxWidth=&maxHeight=&FileOut=[/text] [imResizeImageSB] [/!][function name=imResizeImage][shell][PathToIM]/convert [FileIn] -scale '[maxwidth]x[maxheight]>' +profile "*" [FileOut][/shell][/function][!] SANDBOX: [/!][function name=imResizeImageSB][text]imInput=[FileIn] -scale '[maxwidth]x[maxheight]>' +profile "*" [FileOut][/text][shell scriptid=ImageMagick_convert][/shell][/function][!] ****** Practical Example *********** At top of template, set some default values: [text multi=T&secure=f]maxWidth=800&maxHeight=600[/text] Form submision sends in these formvariables: OPTIONAL: to override your defaults above (secure=f will allow this): [imResizeImage FileIn=[FileIn]&maxWidth=[maxWidth]&maxHeight=[maxHeight]&FileOut=[FileOut]] [imResizeImageSB] (for Sandbox users) +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ FUNCTION Name: imMakeThumb ** Description: Proportionately creates thumbnails for images, within your maximum width or height. ** Input: maxTHwidth = (maximum pixel width) maxTHheight = (maximum pixel height) THtype = (filetype -- WITHOUT the dot -- for thumbnail. Use jpg, gif or png) FileIn = (path to original file) ThFileOut = (path to destination thumbnail file) ** usage: [imMakeThumb FileIn=&FileOut=&maxTHwidth=&maxTHheight=&THtype=] ** Sandbox usage: [text multi=t]FileIn=&FileOut=&maxTHwidth=&maxTHheight=&THtype=[/text] [imMakeThumbSB] [/!][function name=imMakeThumb][shell][PathToIM]/convert [FileIn] -scale [maxTHwidth]x[maxTHheight] -format [THtype] +profile "*" -unsharp 0x.5 [ThFileOut][/shell][/function][!] [/!][function name=imMakeThumbSB][text]imInput=[FileIn] -scale [maxTHwidth]x[maxTHheight] -format [THtype] +profile "*" -unsharp 0x.5 [ThFileOut][/text][shell scriptid=ImageMagick_convert][/shell][/function][!] +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ FUNCTION Name: imImprintText ** Description: imprints solid text onto your image, as in a credit line or caption ** Input: font = (font on server) note: if not present on server, sans serif will be used textsize = (size in points) textloc = (north, northeast, east, southeast, south, southwest, west, northwest, center) textcolor = (color in hex value or web named color, e.g. black, white, red) offsetX = (horizontal pixel distance from image edge) * you will have to play with this a bit offsetY = (vertical pixel distance from image edge) textstring = (your text) FileIn = (path to original file) FileOut = (path to final destination) ** usage: [imImprintText FileIn=&FileOut=&textstring=&textsize=&textcolor=&textloc=&offsetX=&offsetY=] ** Sandbox usage: [text multi=t]FileIn=&FileOut=&textstring=&textsize=&textcolor=&textloc=&offsetX=&offsetY=[/text] [imImprintTextSB] [/!][function name=imImprintText][shell][PathToIM]/convert [FileIn] -font [font] -pointsize [textsize] -draw "gravity [textloc] fill [textcolor] text [offsetX],[offsetY] '[textstring]'" [FileOut][/shell][/function][!] [/!][function name=imImprintTextSB][text]imInput=[FileIn] -font [font] -pointsize [textsize] -draw "gravity [textloc] fill [textcolor] text [offsetX],[offsetY] '[textstring]'" [FileOut][/text][shell scriptid=ImageMagick_convert][/shell][/function][!] +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ FUNCTION Name: imRotate ** Description: Rotates image according to input. ** Input: rotation = (0, 90, 180, -90) FileIn = (path to original file) FileOut = (path to final destination) ** usage: [imRotate FileIn=&FileOut=&rotation=] ** Sandbox usage: [text multi=t]FileIn=&FileOut=&rotation=[/text] [imRotateSB] [/!][function name=imRotate][shell][PathToIM]/convert [FileIn] -rotate [rotation] [FileOut][/shell][/function][!] [/!][function name=imRotateSB][text]imInput=[FileIn] -rotate [rotation] [FileOut][/text][shell scriptid=ImageMagick_convert][/shell][/function][!] +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ FUNCTION Name: imMirror ** Description: Mirrors image according to input, either vertically or horizontally. ** Input: whichway = (flip, flop) note: flop = mirrored reversal; flip = upside-down reversal FileIn = (path to original file) FileOut = (path to final destination) ** usage: [imMirror FileIn=&FileOut=&whichway=] ** Sandbox usage: [text multi=t]FileIn=&FileOut=&whichway=[/text] [imMirrorSB] [/!][function name=imMirror][shell][PathToIM]/convert [FileIn] -[whichway] [FileOut][/shell][/function][!] [/!][function name=imMirrorSB][text]imInput=[FileIn] -[whichway] [FileOut][/text][shell scriptid=ImageMagick_convert][/shell][/function][!] +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ FUNCTION Name: imChangeFormat ** Description: Change format to jpg, gif, or png. ** Input: fileform = (jpg, png, gif) FileIn = (path to original file) FileOut = (path to final destination) ** usage: [imChangeFormat FileIn=&FileOut=&fileform=] ** Sandbox usage: [text multi=t]FileIn=&FileOut=&fileform=[/text] [imChangeFormatSB] [/!][function name=imChangeFormat][shell][PathToIM]/convert [FileIn] [getchars start=4&from=end][FileOut][/getchars][fileform][/shell][/function][!] [/!][function name=imChangeFormatSB][text]imInput=[FileIn] [getchars start=4&from=end][FileOut][/getchars][fileform][/text][shell scriptid=ImageMagick_convert][/shell][/function]