View Javadoc

1   /**
2    * This file Copyright (c) 2003-2011 Magnolia International
3    * Ltd.  (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10   * This file is distributed in the hope that it will be
11   * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12   * implied warranty of MERCHANTABILITY or FITNESS FOR A
13   * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14   * Redistribution, except as permitted by whichever of the GPL
15   * or MNA you select, is prohibited.
16   *
17   * 1. For the GPL license (GPL), you can redistribute and/or
18   * modify this file under the terms of the GNU General
19   * Public License, Version 3, as published by the Free Software
20   * Foundation.  You should have received a copy of the GNU
21   * General Public License, Version 3 along with this program;
22   * if not, write to the Free Software Foundation, Inc., 51
23   * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24   *
25   * 2. For the Magnolia Network Agreement (MNA), this file
26   * and the accompanying materials are made available under the
27   * terms of the MNA which accompanies this distribution, and
28   * is available at http://www.magnolia-cms.com/mna.html
29   *
30   * Any modifications to this file must keep this entire header
31   * intact.
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   * @author Fabrizio Giustina
64   * @version $Id: FileThumbnailDialogPage.java 41137 2011-01-06 18:19:25Z gjoseph $
65   */
66  public class FileThumbnailDialogPage extends PageMVCHandler {
67  
68      /**
69       * Stable serialVersionUID.
70       */
71      private static final long serialVersionUID = 222L;
72  
73      /**
74       * Logger.
75       */
76      private static Logger log = LoggerFactory.getLogger(FileThumbnailDialogPage.class);
77  
78      private String src;
79  
80      private String size;
81  
82      /**
83       * @param name
84       * @param request
85       * @param response
86       */
87      public FileThumbnailDialogPage(String name, HttpServletRequest request, HttpServletResponse response) {
88          super(name, request, response);
89      }
90  
91      /**
92       * Setter for <code>size</code>.
93       * @param size The size to set.
94       */
95      public void setSize(String size) {
96          this.size = size;
97      }
98  
99      /**
100      * Setter for <code>src</code>.
101      * @param src The src to set.
102      */
103     public void setSrc(String src) {
104         this.src = src;
105     }
106 
107     /**
108      * @see info.magnolia.cms.servlets.MVCServletHandler#renderHtml(java.lang.String)
109      */
110     public void renderHtml(String view) throws IOException {
111 
112         if (src == null) {
113             return;
114         }
115 
116         response.setContentType("image/jpeg"); //$NON-NLS-1$
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         // draw original image to thumbnail image object and scale it to the new size on-the-fly
167         BufferedImage thumbImage = new BufferedImage(thumbWidth, thumbHeight, BufferedImage.TYPE_INT_RGB);
168         Graphics2D graphics2D = thumbImage.createGraphics();
169         // graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION,RenderingHints.VALUE_INTERPOLATION_BILINEAR);
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 }