View Javadoc
1   /*
2    * Copyright (C) 2003-2007 eXo Platform SAS.
3    *
4    * This program is free software; you can redistribute it and/or
5    * modify it under the terms of the GNU Affero General Public License
6    * as published by the Free Software Foundation; either version 3
7    * of the License, or (at your option) any later version.
8    *
9    * This program is distributed in the hope that it will be useful,
10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12   * GNU General Public License for more details.
13   *
14   * You should have received a copy of the GNU General Public License
15   * along with this program; if not, see<http://www.gnu.org/licenses/>.
16   */
17  package org.exoplatform.services.cms.impl;
18  
19  import java.awt.image.BufferedImage;
20  import java.io.ByteArrayInputStream;
21  import java.io.ByteArrayOutputStream;
22  import java.io.InputStream;
23  
24  import javax.imageio.ImageIO;
25  
26  import org.imgscalr.Scalr;
27  
28  /**
29   * Created by The eXo Platform SARL
30   * Author : Dang Van Minh
31   *          minh.dang@exoplatform.com
32   * Oct 15, 2008 2:18:02 PM
33   */
34  /**
35   * Process images class
36   */
37  public class ImageUtils {
38  
39    /**
40     * Return the image which resized(support JPG, PNG, GIF)
41     * @param image The BufferedImage which based on InputStream
42     * BufferedImage image = ImageIO.read(InputStream)
43     * @param maxWidth Max width of thumbnail will be resized
44     * @param maxHeight Max height of thumbnail will be resized
45     * @return InputStream
46     * @throws Exception
47     */
48    public static InputStream scaleImage(BufferedImage image, int maxWidth, int maxHeight) throws Exception {
49      return scaleImage(image, maxWidth, maxHeight, false);
50    }
51  
52    /**
53     * Return the image which resized(support JPG, PNG, GIF)
54     * @param image The BufferedImage which based on InputStream
55     * BufferedImage image = ImageIO.read(InputStream)
56     * @param maxWidth Max width of thumbnail will be resized
57     * @param maxHeight Max height of thumbnail will be resized
58     * @return InputStream
59     * @throws Exception
60     */  
61    public static InputStream scaleImage(BufferedImage image, int maxWidth, int maxHeight, boolean crop) throws Exception {
62      // Make sure the aspect ratio is maintained, so the image is not skewed
63      BufferedImage thumbImage = null;
64      if(crop) {
65        thumbImage = Scalr.crop(image, maxWidth, maxHeight);
66      } else {
67        if (maxHeight == 0) {
68          //Just used in custom size case. When 0 as specified as height then it will be calculated automatically to fit 
69          //with expected width.
70          thumbImage = Scalr.resize(image, Scalr.Mode.FIT_TO_WIDTH, maxWidth);
71        } else if(maxWidth == 0) {
72          //Just used in custom size case. When 0 as specified as width then it will be calculated automatically to fit 
73          //with expected height.
74          thumbImage = Scalr.resize(image, Scalr.Mode.FIT_TO_HEIGHT, maxHeight);
75        } else {
76          //BALANCED: Used to indicate that the scaling implementation should use a scaling operation balanced 
77          //between SPEED and QUALITY
78          thumbImage = Scalr.resize(image, Scalr.Method.BALANCED, maxWidth, maxHeight);      
79        }
80      }
81      ByteArrayOutputStream outStream = new ByteArrayOutputStream();
82      ImageIO.write(thumbImage, "png", outStream);
83      InputStream is = new ByteArrayInputStream(outStream.toByteArray());
84      return is;
85    }
86  
87  }