ImageObject.cfc
上传用户:ah_jiwei
上传日期:2022-07-24
资源大小:54044k
文件大小:12k
源码类别:

数据库编程

开发平台:

Visual C++

  1. <cfcomponent name="ImageObject">
  2. <!---
  3. ImageObject.cfc written by Rick Root (rick@webworksllc.com)
  4. Related Web Sites:
  5. - http://www.opensourcecf.com/imagecfc (home page)
  6. This is an object oriented interface to the original
  7. ImageCFC.
  8. Example Code:
  9. io = createObject("component","ImageObject");
  10. io.setOption("defaultJpegCompression",95);
  11. io.init("#ExpandPath(".")#/emily.jpg");
  12. io.scaleWidth(500);
  13. io.save("#ExpandPath(".")#/imagex1.jpg");
  14. io.flipHorizontal();
  15. io.save("#ExpandPath(".")#/imagex2.jpg");
  16. io.revert();
  17. io.filterFastBlur(2,5);
  18. io.save("#ExpandPath(".")#/imagex3.jpg");
  19. io.revert();
  20. io.filterPosterize(32);
  21. io.save("#ExpandPath(".")#/imagex4.jpg");
  22. LICENSE
  23. -------
  24. Copyright (c) 2006, Rick Root <rick@webworksllc.com>
  25. All rights reserved.
  26. Redistribution and use in source and binary forms, with or 
  27. without modification, are permitted provided that the 
  28. following conditions are met:
  29. - Redistributions of source code must retain the above 
  30.   copyright notice, this list of conditions and the 
  31.   following disclaimer. 
  32. - Redistributions in binary form must reproduce the above 
  33.   copyright notice, this list of conditions and the 
  34.   following disclaimer in the documentation and/or other 
  35.   materials provided with the distribution. 
  36. - Neither the name of the Webworks, LLC. nor the names of 
  37.   its contributors may be used to endorse or promote products 
  38.   derived from this software without specific prior written 
  39.   permission. 
  40. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND 
  41. CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, 
  42. INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 
  43. MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 
  44. DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 
  45. CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
  46. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 
  47. BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 
  48. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 
  49. HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
  50. CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 
  51. OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 
  52. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  53. --->
  54. <cfset variables.img = "">
  55. <cfset variables.revertimg = "">
  56. <cfset variables.imageCFC = createObject("component","image")>
  57. <cfset variables.imageInfo = structNew()>
  58. <cfset variables.imageInfo.width = 0>
  59. <cfset variables.imageInfo.height = 0>
  60. <cfset variables.imageInfo.colorModel = "">
  61. <cfset variables.imageInfo.colorspace = "">
  62. <cfset variables.imageInfo.objColorModel = "">
  63. <cfset variables.imageInfo.objColorspace = "">
  64. <cfset variables.imageInfo.sampleModel = "">
  65. <cfset variables.imageInfo.imageType = 0>
  66. <cfset variables.imageInfo.misc = "">
  67. <cfset variables.imageInfo.canModify = false>
  68. <cfset variables.imageCFC.setOption("throwonerror",true)>
  69. <!---
  70. init(filename)        Initialize object from a file.
  71. init(width, height)   Initialize with a blank image
  72. init(bufferedImage)   Initiailize with an existing object
  73. --->
  74. <cffunction name="init" access="public" output="false" returnType="void">
  75. <cfargument name="arg1" type="any" required="yes">
  76. <cfargument name="arg2" type="any" required="no">
  77. <cfif isDefined("arg2") and isNumeric(arg1) and isNumeric(arg2)>
  78. <cfset arg1 = javacast("int",int(arg1))>
  79. <cfset arg2 = javacast("int",int(arg2))>
  80. <cfset variables.img = createObject("java","java.awt.image.BufferedImage")>
  81. <cfset variables.img.init(arg1,arg2,variables.img.TYPE_INT_RGB)>
  82. <cfelseif arg1.getClass().getName() eq "java.awt.image.BufferedImage">
  83. <cfset variables.img = arg1>
  84. <cfelseif isSimpleValue(arg1) and len(arg1) gt 0>
  85. <cfset imageResults = variables.imageCFC.readImage(arg1, "no")>
  86. <cfset variables.img = imageResults.img>
  87. <cfelse>
  88. <cfthrow message="Object Instantiation Error" detail="You have attempted to initialize ooimage.cfc with invalid arguments.  Please consult the documentation for correct initialization arguments.">
  89. </cfif>
  90. <cfif variables.revertimg eq "">
  91. <cfset variables.revertimg = variables.img>
  92. </cfif>
  93. <cfset variables.imageInfo = imageCFC.getImageInfo(variables.img,"")>
  94. <cfreturn>
  95. </cffunction>
  96. <cffunction name="flipHorizontal" access="public" output="true" returnType="void" hint="Flip an image horizontally.">
  97. <cfset var imageResults = imageCFC.flipHorizontal(variables.img,"","")>
  98. <cfset variables.revertimg = variables.img>
  99. <cfset variables.img = imageResults.img>
  100. <cfset variables.imageInfo = imageCFC.getImageInfo(variables.img,"")>
  101. </cffunction>
  102. <cffunction name="getImageInfo" access="public" output="true" returntype="struct" hint="Returns image information.">
  103. <cfreturn variables.imageInfo>
  104. </cffunction>
  105. <cffunction name="getImageObject" access="public" output="true" returntype="struct" hint="Returns a java Buffered Image Object.">
  106. <cfreturn variables.img>
  107. </cffunction>
  108. <cffunction name="flipVertical" access="public" output="true" returntype="void" hint="Flop an image vertically.">
  109. <cfset var imageResults = imageCFC.flipVertical(variables.img,"","")>
  110. <cfset variables.revertimg = variables.img>
  111. <cfset variables.img = imageResults.img>
  112. <cfset variables.imageInfo = imageCFC.getImageInfo(variables.img,"")>
  113. </cffunction>
  114. <cffunction name="scaleWidth" access="public" output="true" returntype="void" hint="Scale an image to a specific width.">
  115. <cfargument name="newWidth" required="yes" type="numeric">
  116. <cfset var imageResults = imageCFC.scaleWidth(variables.img,"","", newWidth)>
  117. <cfset variables.revertimg = variables.img>
  118. <cfset variables.img = imageResults.img>
  119. <cfset variables.imageInfo = imageCFC.getImageInfo(variables.img,"")>
  120. </cffunction>
  121. <cffunction name="scaleHeight" access="public" output="true" returntype="void" hint="Scale an image to a specific height.">
  122. <cfargument name="newHeight" required="yes" type="numeric">
  123. <cfset var imageResults = imageCFC.scaleHeight(variables.img,"","", newHeight)>
  124. <cfset variables.revertimg = variables.img>
  125. <cfset variables.img = imageResults.img>
  126. <cfset variables.imageInfo = imageCFC.getImageInfo(variables.img,"")>
  127. </cffunction>
  128. <cffunction name="resize" access="public" output="true" returntype="void" hint="Resize an image to a specific width and height.">
  129. <cfargument name="newWidth" required="yes" type="numeric">
  130. <cfargument name="newHeight" required="yes" type="numeric">
  131. <cfargument name="preserveAspect" required="no" type="boolean" default="FALSE">
  132. <cfargument name="cropToExact" required="no" type="boolean" default="FALSE">
  133. <cfset var imageResults = imageCFC.resize(variables.img,"","",newWidth,newHeight,preserveAspect,cropToExact)>
  134. <cfset variables.revertimg = variables.img>
  135. <cfset variables.img = imageResults.img>
  136. <cfset variables.imageInfo = imageCFC.getImageInfo(variables.img,"")>
  137. </cffunction>
  138. <cffunction name="crop" access="public" output="true" returntype="void" hint="Crop an image.">
  139. <cfargument name="fromX" required="yes" type="numeric">
  140. <cfargument name="fromY" required="yes" type="numeric">
  141. <cfargument name="newWidth" required="yes" type="numeric">
  142. <cfargument name="newHeight" required="yes" type="numeric">
  143. <cfset var imageResults = imageCFC.crop(variables.img,"","",fromX,fromY,newWidth,newHeight)>
  144. <cfset variables.revertimg = variables.img>
  145. <cfset variables.img = imageResults.img>
  146. <cfset variables.imageInfo = imageCFC.getImageInfo(variables.img,"")>
  147. </cffunction>
  148. <cffunction name="rotate" access="public" output="true" returntype="void" hint="Rotate an image (+/-)90, (+/-)180, or (+/-)270 degrees.">
  149. <cfargument name="degrees" required="yes" type="numeric">
  150. <cfset var imageResults = imageCFC.rotate(variables.img,"","",degrees)>
  151. <cfset variables.revertimg = variables.img>
  152. <cfset variables.img = imageResults.img>
  153. <cfset variables.imageInfo = imageCFC.getImageInfo(variables.img,"")>
  154. </cffunction>
  155. <cffunction name="setOption" access="public" output="true" returnType="void" hint="Sets values for allowed CFC options.">
  156. <cfargument name="key" type="string" required="yes">
  157. <cfargument name="val" type="string" required="yes">
  158. <cfif lcase(trim(key)) eq "throwonerror">
  159. <cfthrow message="Option Configuration Error" detail="You cannot set the throwOnError option when using ImageObject.cfc">
  160. </cfif>
  161. <cfset imageCFC.setOption(key, val)>
  162. </cffunction>
  163. <cffunction name="getOption" access="public" output="true" returnType="any" hint="Returns the current value for the specified CFC option.">
  164. <cfargument name="key" type="string" required="yes">
  165. <cfreturn imageCFC.getOption(key)>
  166. </cffunction>
  167. <cffunction name="filterFastBlur" access="public" output="true" returntype="void" hint="Internal method used for flipping and flopping images.">
  168. <cfargument name="blurAmount" required="yes" type="numeric">
  169. <cfargument name="iterations" required="yes" type="numeric">
  170. <cfset var imageResults = imageCFC.filterFastBlur(variables.img,"","",blurAmount,iterations)>
  171. <cfset variables.revertimg = variables.img>
  172. <cfset variables.img = imageResults.img>
  173. <cfset variables.imageInfo = imageCFC.getImageInfo(variables.img,"")>
  174. </cffunction>
  175. <cffunction name="filterSharpen" access="public" output="true" returntype="void" hint="Internal method used for flipping and flopping images.">
  176. <cfset var imageResults = imageCFC.filterSharpen(variables.img,"","")>
  177. <cfset variables.revertimg = variables.img>
  178. <cfset variables.img = imageResults.img>
  179. <cfset variables.imageInfo = imageCFC.getImageInfo(variables.img,"")>
  180. </cffunction>
  181. <cffunction name="filterPosterize" access="public" output="true" returntype="void" hint="Internal method used for flipping and flopping images.">
  182. <cfargument name="amount" required="yes" type="string">
  183. <cfset var imageResults = imageCFC.filterPosterize(variables.img,"","",amount)>
  184. <cfset variables.revertimg = variables.img>
  185. <cfset variables.img = imageResults.img>
  186. <cfset variables.imageInfo = imageCFC.getImageInfo(variables.img,"")>
  187. </cffunction>
  188. <cffunction name="addText" access="public" output="true" returntype="void" hint="Add text to an image.">
  189. <cfargument name="x" required="yes" type="numeric">
  190. <cfargument name="y" required="yes" type="numeric">
  191. <cfargument name="fontDetails" required="yes" type="struct">
  192. <cfargument name="content" required="yes" type="String">
  193. <cfset var imageResults = imageCFC.addText(variables.img,"","",x,y,fontDetails,content)>
  194. <cfset variables.revertimg = variables.img>
  195. <cfset variables.img = imageResults.img>
  196. <cfset variables.imageInfo = imageCFC.getImageInfo(variables.img,"")>
  197. </cffunction>
  198. <cffunction name="watermark" access="public" output="false" returnType="void">
  199. <cfargument name="wmImage" required="yes" type="Any">
  200. <cfargument name="alpha" required="yes" type="numeric">
  201. <cfargument name="placeAtX" required="yes" type="numeric">
  202. <cfargument name="placeAtY" required="yes" type="numeric">
  203. <cfset var imageResults = "">
  204. <cfif isSimpleValue(wmImage)>
  205. <!--- filename or URL --->
  206. <cfset imageResults = imageCFC.watermark(variables.img,"","",wmImage,alpha,placeAtX,placeAtY)>
  207. <cfelse>
  208. <!--- must be a java object --->
  209. <cfset imageResults = imageCFC.watermark(variables.img,wmImage,"","",alpha,placeAtX,placeAtY)>
  210. </cfif>
  211. <cfset variables.revertimg = variables.img>
  212. <cfset variables.img = imageResults.img>
  213. <cfset variables.imageInfo = imageCFC.getImageInfo(variables.img,"")>
  214. </cffunction>
  215. <cffunction name="save" access="public" output="false" returnType="void">
  216. <cfargument name="filename" type="string" required="no">
  217. <cfargument name="jpegCompression" type="numeric" required="no">
  218. <cfif isDefined("arguments.jpegCompression") and isNumeric(arguments.jpegCompression)>
  219. <cfset imageCFC.writeImage(filename,variables.img,jpegCompression)>
  220. <cfelse>
  221. <cfset imageCFC.writeImage(filename,variables.img)>
  222. </cfif>
  223. </cffunction>
  224. <cffunction name="revert" access="public" output="true" returntype="void" hint="Undo the previous manipulation.">
  225. <cfset variables.img = variables.revertimg>
  226. <cfset variables.imageInfo = imageCFC.getImageInfo(variables.img,"")>
  227. </cffunction>
  228. </cfcomponent>