View Javadoc
1   /**
2    * This file Copyright (c) 2012-2018 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.templating.jsp.taglib;
35  
36  import info.magnolia.cms.core.Content;
37  import info.magnolia.cms.core.HierarchyManager;
38  import info.magnolia.cms.core.ItemType;
39  import info.magnolia.cms.util.ContentUtil;
40  import info.magnolia.cms.util.HierarchyManagerUtil;
41  import info.magnolia.cms.util.NodeDataUtil;
42  import info.magnolia.context.MgnlContext;
43  import info.magnolia.jcr.util.NodeTypes;
44  import info.magnolia.repository.RepositoryConstants;
45  
46  import java.awt.Graphics2D;
47  import java.awt.Image;
48  import java.awt.image.BufferedImage;
49  import java.io.File;
50  import java.io.FileNotFoundException;
51  import java.io.IOException;
52  import java.io.InputStream;
53  import java.util.Calendar;
54  
55  import javax.imageio.ImageIO;
56  import javax.jcr.AccessDeniedException;
57  import javax.jcr.PathNotFoundException;
58  import javax.jcr.RepositoryException;
59  import javax.servlet.jsp.JspException;
60  import javax.servlet.jsp.JspWriter;
61  
62  import org.slf4j.Logger;
63  import org.slf4j.LoggerFactory;
64  import org.tldgen.annotations.BodyContent;
65  import org.tldgen.annotations.Tag;
66  
67  /**
68   * Creates a scaled copy of an image. The maximum width and height of the images can be specified via the
69   * attributes. <br />
70   * <br />
71   * If the scaled image with the specified name does not exist in the repository, then this tag will create it and save
72   * it. If the scaled image already exists, then it will not be recreated. <br />
73   * <br />
74   * The name of the node that contains the original image is set by the attribute 'parentContentNode', and the name of
75   * the nodeData for the image is set by the attribute 'parentNodeDataName'. If 'parentContentNode' is null, the local
76   * content node is used. <br />
77   * <br />
78   * The name of the content node that contains the new scaled image is set by the attribute 'imageContentNodeName'. This
79   * node is created under the original image node. This ensures that, if the original images is deleted, so are all the
80   * scaled versions. <br />
81   * <br />
82   * This tag writes out the handle of the content node that contains the image. <br />
83   * <br />
84   *
85   * @jsp.tag name="scaleImage" body-content="empty"
86   */
87  @Tag(name = "scaleImage", bodyContent = BodyContent.EMPTY)
88  public class ScaleImageTag extends BaseImageTag {
89      private static final Logger log = LoggerFactory.getLogger(ScaleImageTag.class);
90  
91      /**
92       * Location for folder for temporary image creation.
93       */
94      private static final String TEMP_IMAGE_NAME = "tmp-img";
95  
96      /**
97       * The value of the extension nodeData in the properties node.
98       */
99      private static final String PROPERTIES_EXTENSION_VALUE = "PNG";
100 
101     /**
102      * Attribute: Image maximum height.
103      */
104     private int maxHeight = 0;
105 
106     /**
107      * Attribute: Image maximum width.
108      */
109     private int maxWidth = 0;
110 
111     /**
112      * Attribute: Allow resizing images beyond their original dimensions.
113      * Enabled by default for backwards compatibility but keep in mind this can
114      * result in images of very poor quality.
115      */
116     private boolean allowOversize = true;
117 
118     /**
119      * Attribute: The name of the new content node to create.
120      */
121     private String imageContentNodeName;
122 
123     /**
124      * Attribute: The name of the data node that contains the existing image.
125      */
126     private String parentNodeDataName;
127 
128     /**
129      * The maximum height of the image in pixels.
130      *
131      * @jsp.attribute required="false" rtexprvalue="true" type="int"
132      */
133     public void setMaxHeight(int maxHeight) {
134         this.maxHeight = maxHeight;
135     }
136 
137     /**
138      * The maximum width of the image in pixels.
139      *
140      * @jsp.attribute required="false" rtexprvalue="true" type="int"
141      */
142     public void setMaxWidth(int maxWidth) {
143         this.maxWidth = maxWidth;
144     }
145 
146     /**
147      * Allow resizing images beyond their original dimensions?
148      *
149      * @jsp.attribute required="false" rtexprvalue="true" type="boolean"
150      */
151     public void setAllowOversize(boolean allowOversize) {
152         this.allowOversize = allowOversize;
153     }
154 
155     /**
156      * The name of the content node that contains the image to be copied and scaled.
157      *
158      * @jsp.attribute required="false" rtexprvalue="true"
159      */
160     @Override
161     public void setParentContentNodeName(String parentContentNodeName) {
162         this.parentContentNodeName = parentContentNodeName;
163     }
164 
165     /**
166      * The name of the data node that contains the image data to be copied and scaled.
167      *
168      * @jsp.attribute required="true" rtexprvalue="true"
169      */
170     public void setParentNodeDataName(String parentNodeDataName) {
171         this.parentNodeDataName = parentNodeDataName;
172     }
173 
174     /**
175      * The name of the new contentNode that will contain the scaled version of the image.
176      *
177      * @jsp.attribute required="true" rtexprvalue="true"
178      */
179     @Override
180     public void setImageContentNodeName(String imageContentNodeName) {
181         this.imageContentNodeName = imageContentNodeName;
182     }
183 
184     @Override
185     public void doTag() throws JspException {
186         // initialize everything
187         Content parentContentNode;
188         Content imageContentNode;
189         JspWriter out = this.getJspContext().getOut();
190 
191         try {
192 
193             // set the parent node that contains the original image
194             if ((this.parentContentNodeName == null) || (this.parentContentNodeName.equals(""))) {
195                 parentContentNode = ContentUtil.asContent(MgnlContext.getAggregationState().getCurrentContentNode());
196             } else {
197                 HierarchyManager hm = HierarchyManagerUtil.asHierarchyManager(MgnlContext.getJCRSession(RepositoryConstants.WEBSITE));
198                 // if this name starts with a '/', then assume it is a node handle
199                 // otherwise assume that its is a path relative to the local content node
200                 if (this.parentContentNodeName.startsWith("/")) {
201                     parentContentNode = hm.getContent(this.parentContentNodeName);
202                 } else {
203                     String handle = MgnlContext.getAggregationState().getCurrentContentNode().getPath();
204                     parentContentNode = hm.getContent(handle + "/" + this.parentContentNodeName);
205                 }
206             }
207 
208             // check if the new image node exists, if not then create it
209             if (parentContentNode.hasContent(this.imageContentNodeName)) {
210                 imageContentNode = parentContentNode.getContent(this.imageContentNodeName);
211             } else {
212                 // create the node
213                 imageContentNode = parentContentNode.createContent(this.imageContentNodeName, ItemType.CONTENTNODE);
214                 parentContentNode.save();
215             }
216             // if the node does not have the image data or should be rescaled (i.e., something has c
217             // then create the image data
218             if (!imageContentNode.hasNodeData(this.parentNodeDataName) || rescale(parentContentNode, imageContentNode)) {
219                 this.createImageNodeData(parentContentNode, imageContentNode);
220             }
221             // write out the handle for the new image and exit
222             StringBuffer handle = new StringBuffer(imageContentNode.getHandle());
223             handle.append("/");
224             handle.append(getFilename());
225             out.write(handle.toString());
226         } catch (PathNotFoundException e) {
227             log.error("PathNotFoundException occured in ScaleImage tag: {}", e.getMessage(), e);
228         } catch (AccessDeniedException e) {
229             log.error("AccessDeniedException occured in ScaleImage tag: {}", e.getMessage(), e);
230         } catch (RepositoryException e) {
231             log.error("RepositoryException occured in ScaleImage tag: {}", e.getMessage(), e);
232         } catch (FileNotFoundException e) {
233             log.error("FileNotFoundException occured in ScaleImage tag: {}", e.getMessage(), e);
234         } catch (IOException e) {
235             log.error("IOException occured in ScaleImage tag: {}", e.getMessage(), e);
236         }
237         this.cleanUp();
238     }
239 
240     /**
241      * Checks to see if the previously scaled image needs to be rescaled. This is true when the parent content node has
242      * been updated or the height or width parameters have changed.
243      *
244      * @deprecated since 5.6.
245      */
246     @Deprecated
247     protected boolean rescale(Content parentContentNode, Content imageContentNode) {
248 
249         try {
250             Calendar parentModified = NodeTypes.LastModified.getLastModified(parentContentNode.getJCRNode());
251             Calendar imageModified = NodeTypes.LastModified.getLastModified(imageContentNode.getJCRNode());
252 
253             if (parentModified.after(imageModified)) {
254                 return true;
255             }
256         } catch (RepositoryException e) {
257             // if we can't determine last modification of the nodes then do the safe thing and go for rescaling
258             return true;
259         }
260 
261         int originalHeight = (int) imageContentNode.getNodeData("maxHeight").getLong();
262         int originalWidth = (int) imageContentNode.getNodeData("maxWidth").getLong();
263 
264         return originalHeight != maxHeight || originalWidth != maxWidth;
265     }
266 
267     /**
268      * Set objects to null.
269      */
270     public void cleanUp() {
271         this.parentNodeDataName = null;
272         this.imageContentNodeName = null;
273         this.maxWidth = 0;
274         this.maxHeight = 0;
275     }
276 
277     /**
278      * Create an image file that is a scaled version of the original image.
279      */
280     private void createImageNodeData(Content parentContentNode, Content imageContentNode) throws PathNotFoundException,
281             RepositoryException, IOException {
282 
283         // get the original image, as a buffered image
284         InputStream oriImgStr = parentContentNode.getNodeData(this.parentNodeDataName).getStream();
285         BufferedImage oriImgBuff = ImageIO.read(oriImgStr);
286         oriImgStr.close();
287         // create the new image file
288         File newImgFile = this.scaleImage(oriImgBuff);
289 
290         NodeDataUtil.getOrCreate(imageContentNode, "maxHeight").setValue(maxHeight);
291         NodeDataUtil.getOrCreate(imageContentNode, "maxWidth").setValue(maxWidth);
292 
293         createImageNode(newImgFile, imageContentNode);
294 
295         newImgFile.delete();
296     }
297 
298     /**
299      * Create an image file that is a scaled version of the original image.
300      */
301     private File scaleImage(BufferedImage oriImgBuff) throws IOException {
302         // get the dimesnions of the original image
303         int oriWidth = oriImgBuff.getWidth();
304         int oriHeight = oriImgBuff.getHeight();
305         // get scale factor for the new image
306         double scaleFactor = this.scaleFactor(oriWidth, oriHeight);
307         // get the width and height of the new image
308         int newWidth = new Double(oriWidth * scaleFactor).intValue();
309         int newHeight = new Double(oriHeight * scaleFactor).intValue();
310         // create the thumbnail as a buffered image
311         Image newImg = oriImgBuff.getScaledInstance(newWidth, newHeight, Image.SCALE_AREA_AVERAGING);
312         BufferedImage newImgBuff = new BufferedImage(
313                 newImg.getWidth(null),
314                 newImg.getHeight(null),
315                 BufferedImage.TYPE_INT_RGB);
316         Graphics2D g = newImgBuff.createGraphics();
317         g.drawImage(newImg, 0, 0, null);
318         g.dispose();
319         // create the new image file in the temporary dir
320         File newImgFile = File.createTempFile(TEMP_IMAGE_NAME, PROPERTIES_EXTENSION_VALUE);
321 
322         ImageIO.write(newImgBuff, PROPERTIES_EXTENSION_VALUE, newImgFile);
323         // return the file
324         return newImgFile;
325     }
326 
327     /**
328      * Calculate the scale factor for the image.
329      */
330     private double scaleFactor(int originalWidth, int originalHeight) {
331         double scaleFactor;
332 
333         int scaleWidth = this.maxWidth;
334         int scaleHeight = this.maxHeight;
335 
336         if (!this.allowOversize) {
337             scaleWidth = Math.min(this.maxWidth, originalWidth);
338             scaleHeight = Math.min(this.maxHeight, originalHeight);
339         }
340 
341         if (scaleWidth <= 0 && scaleHeight <= 0) {
342             // may a copy at the same size
343             scaleFactor = 1;
344         } else if (scaleWidth <= 0) {
345             // use height
346             scaleFactor = (double) scaleHeight / (double) originalHeight;
347         } else if (scaleHeight <= 0) {
348             // use width
349             scaleFactor = (double) scaleWidth / (double) originalWidth;
350         } else {
351             // create two scale factors, and see which is smaller
352             double scaleFactorWidth = (double) scaleWidth / (double) originalWidth;
353             double scaleFactorHeight = (double) scaleHeight / (double) originalHeight;
354             scaleFactor = Math.min(scaleFactorWidth, scaleFactorHeight);
355         }
356         return scaleFactor;
357     }
358 
359     @Override
360     protected String getFilename() {
361         return this.parentNodeDataName;
362     }
363 }