Clover icon

Magnolia Imaging Module 3.4.2-SUPPORT-10161

  1. Project Clover database Tue Jul 16 2019 23:33:19 EEST
  2. Package info.magnolia.imaging.operations.cropresize

File AutoCropAndResize.java

 

Coverage histogram

../../../../../img/srcFileCovDistChart10.png
0% of files have more coverage

Code metrics

6
20
2
1
112
49
7
0.35
10
2
3.5
22.2% of code in this file is excluded from these metrics.

Classes

Class Line # Actions
AutoCropAndResize 51 20 22.2% 7 2
0.928571492.9%
 

Contributing tests

This file is covered by 20 tests. .

Source view

1    /**
2    * This file Copyright (c) 2007-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.imaging.operations.cropresize;
35   
36    import info.magnolia.imaging.ImagingException;
37    import info.magnolia.imaging.ParameterProvider;
38   
39    import java.awt.image.BufferedImage;
40   
41    /**
42    * An {@link AbstractCropAndResize} implementation which will resize to the configured targetWidth and
43    * targetHeight, and crop if needed: while respecting the ratio of the target dimensions, it will crop
44    * the image such that the largest possible portion of the image is kept. It will keep the central part
45    * of the image and cut off the external parts (i.e centered crop).
46    *
47    * <p>If either targetWidth or targetHeight is <=0, the ratio of the source image will be preserved.
48    * If both targetWidth and targetHeight are >0, both will be used, even if they don't match the ratio of
49    * the source image (thus cropping it).</p>
50    */
 
51    public class AutoCropAndResize extends AbstractCropAndResize {
52    private int targetWidth;
53    private int targetHeight;
54   
 
55  34 toggle @Override
56    protected Coords getCroopCoords(BufferedImage source, ParameterProvider params) throws ImagingException {
57  34 final int sourceWidth = source.getWidth();
58  34 final int sourceHeight = source.getHeight();
59   
60  34 int width, height;
61  34 if (targetWidth <= 0 && targetHeight <= 0) {
62  0 throw new ImagingException("Please specify either or both targetWidth and targetHeight");
63  34 } else if (targetWidth <= 0 || targetHeight <= 0) {
64    // either target width or height is set, not both, so let's respect the source ratio
65  4 width = sourceWidth;
66  4 height = sourceHeight;
67    } else {
68    // both target dimensions are specified, let's respect targetRatio
69  30 final double targetRatio = (double) targetWidth / (double) targetHeight;
70    // this is a rather dumb and crude way of determining which side of the source to use for the largest possible result
71    // a seemingly smarter "algorithm" involved comparing the ratio of (sourceRatio/targetRatio) to 1. It ended up being less legible and comprehensible than this, so I gave in.
72  30 width = (int) (sourceHeight * targetRatio);
73  30 height = sourceHeight;
74  30 if (width > sourceWidth) {
75  17 width = sourceWidth;
76  17 height = (int) (sourceWidth / targetRatio);
77    }
78    }
79   
80  34 final int x1 = (sourceWidth - width) / 2;
81  34 final int x2 = sourceWidth - x1;
82  34 final int y1 = (sourceHeight - height) / 2;
83  34 final int y2 = sourceHeight - y1;
84  34 return new Coords(x1, y1, x2, y2);
85    }
86   
87    // TODO - this has been moved out of AbstractCropAndResize. Here is how it was originally documented:
88    // If targetWidth and targetHeight are <=0, no resizing will happen. (ie cropping only)
89    // If either targetWidth or targetHeight is <=0, the ratio of the cropped image will be preserved.
90    // If both targetWidth and targetHeight are >0, both will be used, even if they don't match the ratio of the cropped image (thus cropping it).
91   
 
92  32 toggle @Override
93    protected Size getEffectiveTargetSize(BufferedImage source, Coords cropCoords, ParameterProvider params) {
94  32 return Size.conformToCropRatio(cropCoords, targetWidth, targetHeight);
95    }
96   
 
97    toggle public int getTargetWidth() {
98    return targetWidth;
99    }
100   
 
101    toggle public void setTargetWidth(int targetWidth) {
102    this.targetWidth = targetWidth;
103    }
104   
 
105    toggle public int getTargetHeight() {
106    return targetHeight;
107    }
108   
 
109    toggle public void setTargetHeight(int targetHeight) {
110    this.targetHeight = targetHeight;
111    }
112    }