/
Image.java
executable file
·573 lines (455 loc) · 15.2 KB
/
Image.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
package railo.runtime.tag;
import java.awt.Color;
import java.io.IOException;
import javax.servlet.jsp.JspException;
import railo.commons.color.ColorCaster;
import railo.commons.io.res.Resource;
import railo.commons.io.res.util.ResourceUtil;
import railo.commons.lang.StringUtil;
import railo.runtime.exp.ApplicationException;
import railo.runtime.exp.ExpressionException;
import railo.runtime.exp.PageException;
import railo.runtime.ext.tag.TagImpl;
import railo.runtime.functions.other.CreateUUID;
import railo.runtime.functions.system.ContractPath;
import railo.runtime.img.ImageUtil;
import railo.runtime.img.MarpleCaptcha;
import railo.runtime.op.Caster;
import railo.runtime.type.List;
import railo.runtime.type.Struct;
import railo.runtime.type.util.ArrayUtil;
// GetWriteableImageFormats
// GetReadableImageFormats
/**
* Lets you resize and add labels to GIF and JPEG format images.
*
*
*
**/
public final class Image extends TagImpl {
private static int ACTION_BORDER=0;
private static int ACTION_CAPTCHA=1;
private static int ACTION_CONVERT=2;
private static int ACTION_INFO=3;
private static int ACTION_READ=4;
private static int ACTION_RESIZE=5;
private static int ACTION_ROTATE=6;
private static int ACTION_WRITE=7;
private static int ACTION_WRITE_TO_BROWSER=8;
private int action=ACTION_READ;
private String strAction="read";
private int angle=-1;
private Color color=Color.BLACK;
private Resource destination;
private int difficulty=MarpleCaptcha.DIFFICULTY_LOW;
private String[] fonts=new String[]{"arial"};
private int fontsize=24;
private String format="png";
private String height;
private String width;
private boolean isbase64;
private String name;
private boolean overwrite;
private float quality=.75F;
private Object oSource;
private railo.runtime.img.Image source;
private String structName;
private String text;
private int thickness=1;
private String passthrough;
private boolean base64;
/**
*
* @see railo.runtime.ext.tag.TagImpl#release()
*/
public void release() {
super.release();
action=ACTION_READ;
strAction="read";
angle=-1;
color=Color.BLACK;
destination=null;
difficulty=MarpleCaptcha.DIFFICULTY_LOW;
fonts=new String[]{"arial"};
fontsize=24;
format="png";
height=null;
width=null;
isbase64=false;
name=null;
overwrite=false;
quality=0.75F;
source=null;
oSource=null;
structName=null;
text=null;
thickness=1;
passthrough=null;
base64=false;
}
/**
* @param action the action to set
* @throws ApplicationException
*/
public void setAction(String strAction) throws ApplicationException {
this.strAction = strAction;
strAction=strAction.trim().toLowerCase();
if(StringUtil.isEmpty(strAction))action=ACTION_READ;
else if("border".equals(strAction))action=ACTION_BORDER;
else if("captcha".equals(strAction))action=ACTION_CAPTCHA;
else if("convert".equals(strAction))action=ACTION_CONVERT;
else if("info".equals(strAction))action=ACTION_INFO;
else if("read".equals(strAction))action=ACTION_READ;
else if("resize".equals(strAction))action=ACTION_RESIZE;
else if("rotate".equals(strAction))action=ACTION_ROTATE;
else if("write".equals(strAction))action=ACTION_WRITE;
else if("writetobrowser".equals(strAction))action=ACTION_WRITE_TO_BROWSER;
else if("write-to-browser".equals(strAction))action=ACTION_WRITE_TO_BROWSER;
else if("write_to_browser".equals(strAction))action=ACTION_WRITE_TO_BROWSER;
else throw new ApplicationException("invalid action ["+this.strAction+"], " +
"valid actions are [border,captcha,convert,info,read,resize,rotate,write,writeToBrowser]");
}
/**
* @param base64 the base64 to set
*/
public void setBase64(boolean base64) {
this.base64 = base64;
}
/**
* @param angle the angle to set
*/
public void setAngle(double angle) {
this.angle = (int) angle;
}
/**
* @param color the color to set
* @throws ExpressionException
*/
public void setColor(String strColor) throws ExpressionException {
this.color = ColorCaster.toColor(strColor);
}
/**
* @param destination the destination to set
*/
public void setDestination(String destination) {
this.destination = ResourceUtil.toResourceNotExisting(pageContext, destination);
}
/**
* @param difficulty the difficulty to set
* @throws ApplicationException
*/
public void setDifficulty(String strDifficulty) throws ApplicationException {
strDifficulty=strDifficulty.trim().toLowerCase();
if(StringUtil.isEmpty(strDifficulty)) difficulty=MarpleCaptcha.DIFFICULTY_LOW;
else if("low".equals(strDifficulty)) difficulty=MarpleCaptcha.DIFFICULTY_LOW;
else if("medium".equals(strDifficulty)) difficulty=MarpleCaptcha.DIFFICULTY_MEDIUM;
else if("high".equals(strDifficulty)) difficulty=MarpleCaptcha.DIFFICULTY_HIGH;
else throw new ApplicationException("invalid difficulty level ["+strDifficulty+"], " +
"valid difficulty level are [low,medium,high]");
}
/**
* @param fonts the fonts to set
* @throws PageException
*/
public void setFonts(String fontList) throws PageException {
fonts=ArrayUtil.trim(List.toStringArray(List.listToArray(fontList, ',')));
}
/**
* @param passthrough the passthrough to set
*/
public void setPassthrough(String passthrough) {
this.passthrough = passthrough;
}
/**
* @param fontsize the fontsize to set
*/
public void setFontsize(double fontsize) {
this.fontsize = (int) fontsize;
}
/**
* @param format the format to set
* @throws ApplicationException
*/
public void setFormat(String format) throws ApplicationException {
format=format.trim().toLowerCase();
if("gif".equalsIgnoreCase(format)) this.format = "gif";
else if("jpg".equalsIgnoreCase(format)) this.format = "jpg";
else if("jpe".equalsIgnoreCase(format)) this.format = "jpg";
else if("jpeg".equalsIgnoreCase(format))this.format = "jpg";
else if("png".equalsIgnoreCase(format)) this.format = "png";
else if("tiff".equalsIgnoreCase(format))this.format = "tiff";
else if("bmp".equalsIgnoreCase(format)) this.format = "bmp";
else throw new ApplicationException("invalid format ["+format+"], " +
"valid formats are [gif,jpg,png,tiff,bmp]");
}
/**
* @param height the height to set
*/
public void setHeight(String height) {
this.height = height;
}
/**
* @param width the width to set
*/
public void setWidth(String width) {
this.width = width;
}
/**
* @param isbase64 the isbase64 to set
*/
public void setIsbase64(boolean isbase64) {
this.isbase64 = isbase64;
}
/**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}
/**
* @param overwrite the overwrite to set
*/
public void setOverwrite(boolean overwrite) {
this.overwrite = overwrite;
}
/**
* @param quality the quality to set
* @throws ApplicationException
*/
public void setQuality(double quality) throws ApplicationException {
this.quality = (float) quality;
if(quality<0 || quality>1)
throw new ApplicationException("quality ("+Caster.toString(quality)+") has to be a value between 0 and 1");
}
/**
* @param source the source to set
* @throws PageException
*/
public void setSource(Object source) {
this.oSource=source;
// this.source=railo.runtime.img.Image.createImage(pageContext, source, false, false);
}
/**
* @param structName the structName to set
*/
public void setStructname(String structName) {
this.structName = structName;
}
/**
* @param structName the structName to set
*/
public void setResult(String structName) {
this.structName = structName;
}
/**
* @param text the text to set
*/
public void setText(String text) {
this.text = text;
}
/**
* @param thickness the thickness to set
*/
public void setThickness(double thickness) {
this.thickness = (int) thickness;
}
/**
*
* @see railo.runtime.ext.tag.TagImpl#doStartTag()
*/
public int doStartTag() throws JspException {
try {
if(this.oSource!=null){
if(isbase64) this.source=new railo.runtime.img.Image(Caster.toString(oSource));
else this.source=railo.runtime.img.Image.createImage(pageContext, oSource, false, false,true);
}
if(action==ACTION_BORDER) doActionBorder();
else if(action==ACTION_CAPTCHA) doActionCaptcha();
else if(action==ACTION_CONVERT) doActionConvert();
else if(action==ACTION_INFO) doActionInfo();
else if(action==ACTION_READ) doActionRead();
else if(action==ACTION_RESIZE) doActionResize();
else if(action==ACTION_ROTATE) doActionRotate();
else if(action==ACTION_WRITE) doActionWrite();
else if(action==ACTION_WRITE_TO_BROWSER)
doActionWriteToBrowser();
}
catch(Throwable t) {
throw Caster.toPageException(t);
}
return SKIP_BODY;
}
// Add a border to an image
private void doActionBorder() throws PageException, IOException {
required("source", source);
source.addBorder(thickness,color,railo.runtime.img.Image.BORDER_TYPE_CONSTANT);
write();
}
// Create a CAPTCHA image
private void doActionCaptcha() throws PageException, IOException {
required("height", height);
required("width", width);
required("text", text);
String path=null;
// create destination
if(StringUtil.isEmpty(name))path=touchDestination();
MarpleCaptcha c=new MarpleCaptcha();
source=new railo.runtime.img.Image(c.generate(text, Caster.toIntValue(width),Caster.toIntValue(height), fonts, true,
Color.BLACK,fontsize,
difficulty));
// link destination
if(StringUtil.isEmpty(name))writeLink(path);
// write out
write();
}
private void writeLink(String path) throws IOException, PageException {
String add="";
if(passthrough!=null) {
add=" "+passthrough;
}
if(base64) {
String b64 = source.getBase64String(format);
pageContext.write("<img src=\"data:image/"+ImageUtil.getMimeTypeFromFormat(format)+";base64,"+b64+"\" width=\""+source.getWidth()+"\" height=\""+source.getHeight()+"\""+add+" />");
return;
}
pageContext.write("<img src=\""+path+"\" width=\""+source.getWidth()+"\" height=\""+source.getHeight()+"\""+add+" />");
}
private String touchDestination() throws IOException {
if(destination==null) {
String name=CreateUUID.call(pageContext)+"."+format;
Resource folder = pageContext.getConfig().getTempDirectory().getRealResource("graph");
if(!folder.exists())folder.createDirectory(true);
destination = folder.getRealResource(name);
cleanOld(folder);
// create path
String cp = pageContext.getHttpServletRequest().getContextPath();
if(StringUtil.isEmpty(cp)) cp="";
return cp+"/railo-context/graph.cfm?img="+name+"&type="+(List.last(ImageUtil.getMimeTypeFromFormat(format),'/').trim());
}
return ContractPath.call(pageContext, destination.getAbsolutePath());
}
private static void cleanOld(Resource folder) throws IOException {
if(!folder.exists())folder.createDirectory(true);
else if(folder.isDirectory() && ResourceUtil.getRealSize(folder)>(1024*1024)) {
Resource[] children = folder.listResources();
long maxAge=System.currentTimeMillis()-(1000*60);
for(int i=0;i<children.length;i++) {
if(children[i].lastModified()<maxAge)
children[i].delete();
}
}
}
// Convert an image file format
private void doActionConvert() throws PageException, IOException {
required("source", source);
required("destination", destination);
source.convert(ImageUtil.getFormat(destination));
write();
}
// Retrieve information about an image
private void doActionInfo() throws PageException {
required("source",source);
required("structname",structName);
pageContext.setVariable(structName, source.info());
}
private Struct doActionInfo(railo.runtime.img.Image source) throws PageException {
return source.info();
}
// Read an image into memory
private void doActionRead() throws PageException {
required("source",source);
required("name",name);
pageContext.setVariable(name, source);
}
// Resize an image
private void doActionResize() throws PageException, IOException {
required("source", source);
Info i = new Info(source);
/*int w = toDimension("width",width,i);
int h = toDimension("height",height,i);
if(w==-1 && h==-1)
throw new ApplicationException("Missing attribute [width or height]. The action ["+strAction+"] requires the attribute [width or height].");
*/
source.resize(width,height,"highestquality",1D);
write();
}
// Rotate an image
private void doActionRotate() throws PageException, IOException {
required("source",source);
required("angle",angle,-1);
source.rotate(-1, -1, angle, railo.runtime.img.Image.INTERPOLATION_NONE);
write();
}
// Write an image to a file
private void doActionWrite() throws ApplicationException, IOException, ExpressionException {
required("source", source);
required("destination", destination);
source.writeOut(destination,overwrite,quality);
}
// Write an image to the browser
private void doActionWriteToBrowser() throws IOException, PageException {
required("source", source);
String path=null;
// create destination
if(!base64 || !StringUtil.isEmpty(name)) {
path=touchDestination();
write();
}
// link destination
if(StringUtil.isEmpty(name))writeLink(path);
}
private void required(String label, Object value) throws ApplicationException {
if(value==null)
throw new ApplicationException("Missing attribute ["+label+"]. The action ["+strAction+"] requires the attribute [" + label + "].");
//throw new ApplicationException("missing attribute ["+label+"], for the action ["+strAction+"] this attribute is required but was not passed in");
}
private void required(String label, int value,int nullValue) throws ApplicationException {
if(value==nullValue)
throw new ApplicationException("Missing attribute ["+label+"]. The action ["+strAction+"] requires the attribute [" + label + "].");
//throw new ApplicationException("missing attribute ["+label+"], for the action ["+strAction+"] this attribute is required but was not passed in");
}
private void write() throws IOException, PageException {
if(destination!=null) {
doActionWrite();
}
if(!StringUtil.isEmpty(name)) {
required("source", source);
pageContext.setVariable(name, source);// TODO ist das so gut
}
//if(writeToResponseWhenNoOtherTarget) doActionWriteToBrowser();
}
class Info {
Struct struct;
private railo.runtime.img.Image img;
public Info(railo.runtime.img.Image img) {
this.img=img;
}
/**
* @return the sct
* @throws PageException
*/
public Struct getStruct() throws PageException {
if(struct==null)
struct=doActionInfo(img);
return struct;
}
}
private int toDimension(String label, String dimension, Info info) throws PageException {
if(StringUtil.isEmpty(dimension)) return -1;
dimension=dimension.trim();
// int value
int i=Caster.toIntValue(dimension,-1);
if(i>-1) return i;
// percent value
if(StringUtil.endsWith(dimension, '%')) {
float pro=Caster.toIntValue(
dimension.substring(0,dimension.length()-1).trim(),
-1);
if(pro<0 || pro>100)
throw new ExpressionException("attribute ["+label+"] value has a invalid percent definition ["+dimension+"]");
pro/=100F;
return (int)(Caster.toFloatValue(info.getStruct().get(label))*pro);
}
throw new ExpressionException("attribute ["+label+"] value has a invalid definition ["+dimension+"]");
}
}