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 package info.magnolia.module.admininterface.pages;
35
36 import info.magnolia.cms.core.NodeData;
37 import info.magnolia.cms.core.HierarchyManager;
38 import info.magnolia.context.MgnlContext;
39 import info.magnolia.module.admininterface.PageMVCHandler;
40 import info.magnolia.repository.RepositoryConstants;
41
42 import java.awt.Graphics2D;
43 import java.awt.Image;
44 import java.awt.image.BufferedImage;
45 import java.io.BufferedOutputStream;
46 import java.io.IOException;
47 import java.io.InputStream;
48
49 import javax.imageio.ImageIO;
50 import javax.servlet.ServletOutputStream;
51 import javax.servlet.http.HttpServletRequest;
52 import javax.servlet.http.HttpServletResponse;
53
54 import org.slf4j.Logger;
55 import org.slf4j.LoggerFactory;
56
57 import com.sun.image.codec.jpeg.JPEGCodec;
58 import com.sun.image.codec.jpeg.JPEGEncodeParam;
59 import com.sun.image.codec.jpeg.JPEGImageEncoder;
60
61
62
63
64
65
66 public class FileThumbnailDialogPage extends PageMVCHandler {
67
68
69
70
71 private static final long serialVersionUID = 222L;
72
73
74
75
76 private static Logger log = LoggerFactory.getLogger(FileThumbnailDialogPage.class);
77
78 private String src;
79
80 private String size;
81
82
83
84
85
86
87 public FileThumbnailDialogPage(String name, HttpServletRequest request, HttpServletResponse response) {
88 super(name, request, response);
89 }
90
91
92
93
94
95 public void setSize(String size) {
96 this.size = size;
97 }
98
99
100
101
102
103 public void setSrc(String src) {
104 this.src = src;
105 }
106
107
108
109
110 @Override
111 public void renderHtml(String view) throws IOException {
112
113 if (src == null) {
114 return;
115 }
116
117 response.setContentType("image/jpeg");
118
119 HierarchyManager hm = MgnlContext.getHierarchyManager(RepositoryConstants.WEBSITE);
120
121 InputStream in = null;
122
123 NodeData data;
124 try {
125 data = hm.getNodeData(src);
126 in = data.getValue().getStream();
127 }
128 catch (Exception e) {
129 log.error(e.getMessage(), e);
130 return;
131 }
132
133 Image image = ImageIO.read(in);
134
135 int thumbHeight;
136 int thumbWidth;
137 if (size != null) {
138 thumbHeight = image.getHeight(null);
139 thumbWidth = image.getWidth(null);
140 }
141 else {
142 thumbWidth = 150;
143 int w = image.getWidth(null);
144 int h = image.getHeight(null);
145 if (w == 0) {
146 w = 1;
147 }
148 if (h == 0) {
149 h = 1;
150 }
151
152 if (w > thumbWidth) {
153 thumbHeight = thumbWidth * h / w;
154 }
155 else {
156 thumbWidth = w;
157 thumbHeight = h;
158 }
159
160 if (thumbHeight > 120) {
161 thumbHeight = 100;
162 thumbWidth = thumbHeight * w / h;
163 }
164
165 }
166
167
168 BufferedImage thumbImage = new BufferedImage(thumbWidth, thumbHeight, BufferedImage.TYPE_INT_RGB);
169 Graphics2D graphics2D = thumbImage.createGraphics();
170
171 graphics2D.drawImage(image, 0, 0, thumbWidth, thumbHeight, null);
172
173 ServletOutputStream sout = response.getOutputStream();
174 BufferedOutputStream output = new BufferedOutputStream(sout);
175 JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(output);
176 JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(thumbImage);
177 param.setQuality(0.8f, false);
178
179 encoder.setJPEGEncodeParam(param);
180 encoder.encode(thumbImage);
181 }
182
183 }