View Javadoc
1   /**
2    * This file Copyright (c) 2003-2014 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.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   * @author Fabrizio Giustina
64   * @version $Id$
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     @Override
111     public void renderHtml(String view) throws IOException {
112 
113         if (src == null) {
114             return;
115         }
116 
117         response.setContentType("image/jpeg"); //$NON-NLS-1$
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         // draw original image to thumbnail image object and scale it to the new size on-the-fly
168         BufferedImage thumbImage = new BufferedImage(thumbWidth, thumbHeight, BufferedImage.TYPE_INT_RGB);
169         Graphics2D graphics2D = thumbImage.createGraphics();
170         // graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION,RenderingHints.VALUE_INTERPOLATION_BILINEAR);
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 }