KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > riotfamily > common > image > ImageMagickThumbnailer


1 /* ***** BEGIN LICENSE BLOCK *****
2  * Version: MPL 1.1
3  * The contents of this file are subject to the Mozilla Public License Version
4  * 1.1 (the "License"); you may not use this file except in compliance with
5  * the License. You may obtain a copy of the License at
6  * http://www.mozilla.org/MPL/
7  *
8  * Software distributed under the License is distributed on an "AS IS" basis,
9  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
10  * for the specific language governing rights and limitations under the
11  * License.
12  *
13  * The Original Code is Riot.
14  *
15  * The Initial Developer of the Original Code is
16  * Neteye GmbH.
17  * Portions created by the Initial Developer are Copyright (C) 2006
18  * the Initial Developer. All Rights Reserved.
19  *
20  * Contributor(s):
21  * Felix Gnass [fgnass at neteye dot de]
22  *
23  * ***** END LICENSE BLOCK ***** */

24 package org.riotfamily.common.image;
25
26 import java.io.File JavaDoc;
27 import java.io.IOException JavaDoc;
28 import java.util.ArrayList JavaDoc;
29
30 import org.riotfamily.common.io.CommandUtils;
31
32 /**
33  * Thumbnailer that uses ImageMagick.
34  *
35  * @author Felix Gnass [fgnass at neteye dot de]
36  */

37 public class ImageMagickThumbnailer implements Thumbnailer {
38
39     private String JavaDoc convertCommand = "convert";
40         
41     private boolean crop;
42     
43     private String JavaDoc backgroundColor;
44     
45     public void setConvertCommand(String JavaDoc convertCommand) {
46         this.convertCommand = convertCommand;
47     }
48
49     public void setCrop(boolean crop) {
50         this.crop = crop;
51     }
52     
53     /**
54      * If a background color is specified, the image will be exactly resized to
55      * <code>maxWidth x maxHeight</code>, unused space will be filled with the
56      * given color. If <code>crop</code> is enabled, the value is ignored.
57      * <p>
58      * Colors are represented in ImageMagick in the same form used by SVG:
59      * <pre>
60      * name (color name like red, blue, white, etc.)
61      * #RGB (R,G,B are hex numbers, 4 bits each)
62      * #RRGGBB (8 bits each)
63      * #RRRGGGBBB (12 bits each)
64      * #RRRRGGGGBBBB (16 bits each)
65      * #RGBA (4 bits each)
66      * #RRGGBBOO (8 bits each)
67      * #RRRGGGBBBOOO (12 bits each)
68      * #RRRRGGGGBBBBOOOO (16 bits each)
69      * rgb(r,g,b) 0-255 for each of rgb
70      * rgba(r,g,b,a) 0-255 for each of rgb and 0-1 for alpha
71      * cmyk(c,m,y,k) 0-255 for each of cmyk
72      * cmyka(c,m,y,k,a) 0-255 for each of cmyk and 0-1 for alpha
73      * </pre>
74      * </p>
75      * For a transparent background specify an alpha value,
76      * like in <code>#ffff</code>.
77      */

78     public void setBackgroundColor(String JavaDoc backgroundColor) {
79         this.backgroundColor = backgroundColor;
80     }
81
82     public void renderThumbnail(File JavaDoc source, File JavaDoc dest, int width, int height)
83             throws IOException JavaDoc {
84         
85         ArrayList JavaDoc cmd = new ArrayList JavaDoc();
86         cmd.add(convertCommand);
87         cmd.add(source.getAbsolutePath());
88         cmd.add("-resize");
89         if (crop) {
90             cmd.add("x" + height * 2);
91             cmd.add("-resize");
92             cmd.add(width * 2 + "x<");
93             cmd.add("-resize");
94             cmd.add("50%");
95             cmd.add("-crop");
96             cmd.add(width + "x" + height + "+0+0");
97             cmd.add("+repage");
98         }
99         else {
100             cmd.add(width + "x" + height + ">");
101             if (backgroundColor != null) {
102                 cmd.add("-size");
103                 cmd.add(width + "x" + height);
104                 cmd.add("xc:" + backgroundColor);
105                 cmd.add("+swap");
106                 cmd.add("-gravity");
107                 cmd.add("center");
108                 cmd.add("-composite");
109             }
110         }
111         cmd.add("-colorspace");
112         cmd.add("RGB");
113         cmd.add(dest.getAbsolutePath());
114         CommandUtils.exec(cmd);
115     }
116
117 }
118
Popular Tags