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