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.beans.config.ContentRepository;
37 import info.magnolia.cms.core.NodeData;
38 import info.magnolia.cms.core.HierarchyManager;
39 import info.magnolia.context.MgnlContext;
40 import info.magnolia.module.admininterface.PageMVCHandler;
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 public void renderHtml(String view) throws IOException {
111
112 if (src == null) {
113 return;
114 }
115
116 response.setContentType("image/jpeg");
117
118 HierarchyManager hm = MgnlContext.getHierarchyManager(ContentRepository.WEBSITE);
119
120 InputStream in = null;
121
122 NodeData data;
123 try {
124 data = hm.getNodeData(src);
125 in = data.getValue().getStream();
126 }
127 catch (Exception e) {
128 log.error(e.getMessage(), e);
129 return;
130 }
131
132 Image image = ImageIO.read(in);
133
134 int thumbHeight;
135 int thumbWidth;
136 if (size != null) {
137 thumbHeight = image.getHeight(null);
138 thumbWidth = image.getWidth(null);
139 }
140 else {
141 thumbWidth = 150;
142 int w = image.getWidth(null);
143 int h = image.getHeight(null);
144 if (w == 0) {
145 w = 1;
146 }
147 if (h == 0) {
148 h = 1;
149 }
150
151 if (w > thumbWidth) {
152 thumbHeight = thumbWidth * h / w;
153 }
154 else {
155 thumbWidth = w;
156 thumbHeight = h;
157 }
158
159 if (thumbHeight > 120) {
160 thumbHeight = 100;
161 thumbWidth = thumbHeight * w / h;
162 }
163
164 }
165
166
167 BufferedImage thumbImage = new BufferedImage(thumbWidth, thumbHeight, BufferedImage.TYPE_INT_RGB);
168 Graphics2D graphics2D = thumbImage.createGraphics();
169
170 graphics2D.drawImage(image, 0, 0, thumbWidth, thumbHeight, null);
171
172 ServletOutputStream sout = response.getOutputStream();
173 BufferedOutputStream output = new BufferedOutputStream(sout);
174 JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(output);
175 JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(thumbImage);
176 param.setQuality(0.8f, false);
177
178 encoder.setJPEGEncodeParam(param);
179 encoder.encode(thumbImage);
180 }
181
182 }