View Javadoc
1   /**
2    * This file Copyright (c) 2011-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.dam.app.ui.field.upload;
35  
36  import java.io.File;
37  import java.io.FileInputStream;
38  import java.io.FileNotFoundException;
39  import java.io.InputStream;
40  
41  import org.apache.commons.io.IOUtils;
42  import org.devlib.schmidt.imageinfo.ImageInfo;
43  
44  /**
45   * Represents the size of an image. This class is immutable.
46   */
47  final class ImageSize {
48  
49      private final long width;
50      private final long height;
51  
52      ImageSize(long width, long height) {
53          this.width = width;
54          this.height = height;
55      }
56  
57      public long getWidth() {
58          return width;
59      }
60  
61      public long getHeight() {
62          return height;
63      }
64  
65      /**
66       * Scales the size to fit inside a bounding rectangle keeping the aspect ratio as is. If this image is smaller than
67       * the bounding rectangle no change occurs.
68       */
69      public ImageSize scaleToFitIfLarger(long boundingWidth, long boundingHeight) {
70  
71          if (this.width >= this.height && this.width > boundingWidth) {
72              double scaleFactor = ((double) boundingWidth) / this.width;
73              long scaledHeight = (long) (scaleFactor * this.height);
74              return new ImageSize(boundingWidth, scaledHeight);
75          }
76  
77          if (this.height > this.width && this.height > boundingHeight) {
78              double scaleFactor = ((double) boundingHeight) / this.height;
79              long scaledWidth = (long) (scaleFactor * this.width);
80              return new ImageSize(scaledWidth, boundingHeight);
81          }
82  
83          return this;
84      }
85  
86      @Override
87      public boolean equals(Object o) {
88          if (this == o) {
89              return true;
90          }
91          if (!(o instanceof ImageSize)) {
92              return false;
93          }
94  
95          final ImageSize../../../../../info/magnolia/dam/app/ui/field/upload/ImageSize.html#ImageSize">ImageSize imageSize = (ImageSize) o;
96  
97          return height == imageSize.height && width == imageSize.width;
98      }
99  
100     @Override
101     public int hashCode() {
102         int result = (int) (width ^ (width >>> 32));
103         result = 31 * result + (int) (height ^ (height >>> 32));
104         return result;
105     }
106 
107     public static ImageSize valueOf(File file) throws FileNotFoundException {
108         return valueOf(new FileInputStream(file));
109     }
110 
111     public static ImageSize valueOf(InputStream inputStream) {
112         InputStream stream = null;
113         try {
114             ImageInfo imageInfo = new ImageInfo();
115             stream = inputStream;
116             imageInfo.setInput(stream);
117             return imageInfo.check() ? new ImageSize(imageInfo.getWidth(), imageInfo.getHeight()) : null;
118         } finally {
119             IOUtils.closeQuietly(stream);
120         }
121     }
122 }