| 1 | |
package org.webslinger.ext.image; |
| 2 | |
|
| 3 | |
import java.awt.Rectangle; |
| 4 | |
import java.io.File; |
| 5 | |
import java.io.IOException; |
| 6 | |
import javax.servlet.ServletException; |
| 7 | |
|
| 8 | |
import org.apache.commons.vfs.FileObject; |
| 9 | |
|
| 10 | |
import static org.webslinger.lang.NumberUtil.parseFloat; |
| 11 | |
import static org.webslinger.lang.NumberUtil.parseInt; |
| 12 | |
|
| 13 | |
public class CroppingTransformerFactory implements ImageTransformerFactory { |
| 14 | |
protected final String prefix; |
| 15 | |
protected final boolean preview; |
| 16 | |
|
| 17 | |
public CroppingTransformerFactory() { |
| 18 | 0 | this("crop", false); |
| 19 | 0 | } |
| 20 | |
|
| 21 | |
public CroppingTransformerFactory(boolean preview) { |
| 22 | 0 | this("crop", preview); |
| 23 | 0 | } |
| 24 | |
|
| 25 | |
public CroppingTransformerFactory(String prefix) { |
| 26 | 0 | this(prefix, false); |
| 27 | 0 | } |
| 28 | |
|
| 29 | 0 | public CroppingTransformerFactory(String prefix, boolean preview) { |
| 30 | 0 | this.prefix = prefix; |
| 31 | 0 | this.preview = preview; |
| 32 | 0 | } |
| 33 | |
|
| 34 | |
public String getPrefix() { |
| 35 | 0 | return prefix; |
| 36 | |
} |
| 37 | |
|
| 38 | |
public boolean getPreview() { |
| 39 | 0 | return preview; |
| 40 | |
} |
| 41 | |
|
| 42 | |
public boolean isEnabled(ImageDescriptor image) throws IOException { |
| 43 | 0 | Object enabledValue = image.get(getPrefix(), "enabled"); |
| 44 | 0 | return "true".equals(enabledValue) || "constrain".equals(enabledValue); |
| 45 | |
} |
| 46 | |
|
| 47 | |
private String getValue(Object value, String def) throws IOException { |
| 48 | 0 | if (value instanceof String) return (String) value; |
| 49 | 0 | if (value == null) return def; |
| 50 | 0 | return value.toString(); |
| 51 | |
} |
| 52 | |
|
| 53 | |
public ImageTransformer getTransformer(final ImageDescriptor image) throws IOException { |
| 54 | 0 | final Rectangle cropArea = new Rectangle( |
| 55 | |
parseInt(image.get(getPrefix(), "x"), 0), |
| 56 | |
parseInt(image.get(getPrefix(), "y"), 0), |
| 57 | |
parseInt(image.get(getPrefix(), "width"), Integer.MAX_VALUE), |
| 58 | |
parseInt(image.get(getPrefix(), "height"), Integer.MAX_VALUE) |
| 59 | |
); |
| 60 | 0 | final Object cropRatioValue = image.get(getPrefix(), "ratio"); |
| 61 | 0 | final float cropRatio = parseFloat(cropRatioValue, 1F); |
| 62 | 0 | final String cropFill = getValue(image.get(getPrefix(), "fill"), "white"); |
| 63 | 0 | return new ImageTransformer() { |
| 64 | |
public boolean isEnabled() throws IOException { |
| 65 | 0 | Object enabledValue = image.get(getPrefix(), "enabled"); |
| 66 | 0 | return "true".equals(enabledValue) || "constrain".equals(enabledValue); |
| 67 | |
} |
| 68 | |
|
| 69 | |
public Rectangle transform(File file, Rectangle dimensions) throws InterruptedException, IOException { |
| 70 | 0 | Object enabledValue = image.get(getPrefix(), "enabled"); |
| 71 | 0 | if ("true".equals(enabledValue)) { |
| 72 | 0 | return ImageUtils.cropImage(file, cropArea, preview); |
| 73 | 0 | } else if ("constrain".equals(enabledValue)) { |
| 74 | 0 | return ImageUtils.constrainImage(file, cropFill, cropRatio); |
| 75 | |
} else { |
| 76 | 0 | return dimensions; |
| 77 | |
} |
| 78 | |
} |
| 79 | |
|
| 80 | |
public String toString() { |
| 81 | 0 | return "" + ((int) cropArea.getX()) + '-' + ((int) cropArea.getY()) + '-' + ((int) cropArea.getWidth()) + '-' + ((int) cropArea.getHeight()) + "-" + preview + '-' + cropRatio + '-' + cropFill; |
| 82 | |
} |
| 83 | |
}; |
| 84 | |
} |
| 85 | |
} |