@Test public void testTransformImage() throws IOException { Image img = ImageLoader.fromFile("src/test/resources/images/bmw.jpg"); compareImage(img,"32"); compareImage(img,"x64"); compareImage(img,"500x500"); compareImage(img,"320-c"); compareImage(img,"320x500-c"); compareImage(img,"100x100-c@500x400"); compareImage(img,"700x100"); compareImage(img,"320x320"); }
public static final Image transformImage(Image img, String command) { if (StringUtil.isEmpty(command) || img == null) { return img; } if (!isValidCommand(command)) { _log.error("Cannot recognize transform command ["+command+"]. " + "Check Javadoc for instructions."); return img; } int oWidth = img.getWidth(); int oHeight = img.getHeight(); if (_log.isDebugEnabled()) { _log.debug("Command is " + command); _log.debug("Image width is " + oWidth); _log.debug("Image height is " + oHeight); } if (command.contains("-c")) { String[] c1 = command.split("-c"); int t = -1, l = -1, w = 0, h = 0; String[] c2 = c1[0].split("x"); w = StringUtil.convertToInt(c2[0], DEFAULT_SIZE); if (c2.length > 1) { h = StringUtil.convertToInt(c2[1], DEFAULT_SIZE); } else { h = w; } if ((c1.length > 1) && c1[1].startsWith("@")) { String[] c3 = c1[1].split("x"); l = StringUtil.convertToInt(c3[0].substring(1), DEFAULT_SIZE); t = StringUtil.convertToInt(c3[1], DEFAULT_SIZE); } if (l < 0) { l = (oWidth / 2) - (w / 2); } if (t < 0) { t = (oHeight / 2) - (h / 2); } if(_log.isDebugEnabled()) { _log.debug("Cropping using these parameters, l: " + l + ", t: " + t + ", w: " + w + ", h: " + h ); } return img.crop(l, t, l+w, t+h); } else { String[] c2 = command.split("x"); int h = 0, w = 0; if (c2.length==1) { if (command.startsWith("x")) { w = h * oWidth / oHeight; } else { w = StringUtil.convertToInt(c2[0], DEFAULT_SIZE); } } else { w = StringUtil.convertToInt(c2[0], DEFAULT_SIZE); h = StringUtil.convertToInt(c2[1], DEFAULT_SIZE); double ar1 = oWidth / (double) oHeight; double ar2 = w / (double) h; if (ar1 > ar2) { int w2 = (int) (oHeight * ar2); int l = (oWidth / 2) - (w2 / 2); img = img.crop(l, 0, l+w2, oHeight); } else if (ar1 < ar2) { int h2 = (int) (oWidth / ar2); int t = (oHeight / 2) - (h2 / 2); img = img.crop(0, t, oWidth, t+h2); } } return img.getResizedToWidth(w); } }
ImageUtil { public static final Image transformImage(Image img, String command) { if (StringUtil.isEmpty(command) || img == null) { return img; } if (!isValidCommand(command)) { _log.error("Cannot recognize transform command ["+command+"]. " + "Check Javadoc for instructions."); return img; } int oWidth = img.getWidth(); int oHeight = img.getHeight(); if (_log.isDebugEnabled()) { _log.debug("Command is " + command); _log.debug("Image width is " + oWidth); _log.debug("Image height is " + oHeight); } if (command.contains("-c")) { String[] c1 = command.split("-c"); int t = -1, l = -1, w = 0, h = 0; String[] c2 = c1[0].split("x"); w = StringUtil.convertToInt(c2[0], DEFAULT_SIZE); if (c2.length > 1) { h = StringUtil.convertToInt(c2[1], DEFAULT_SIZE); } else { h = w; } if ((c1.length > 1) && c1[1].startsWith("@")) { String[] c3 = c1[1].split("x"); l = StringUtil.convertToInt(c3[0].substring(1), DEFAULT_SIZE); t = StringUtil.convertToInt(c3[1], DEFAULT_SIZE); } if (l < 0) { l = (oWidth / 2) - (w / 2); } if (t < 0) { t = (oHeight / 2) - (h / 2); } if(_log.isDebugEnabled()) { _log.debug("Cropping using these parameters, l: " + l + ", t: " + t + ", w: " + w + ", h: " + h ); } return img.crop(l, t, l+w, t+h); } else { String[] c2 = command.split("x"); int h = 0, w = 0; if (c2.length==1) { if (command.startsWith("x")) { w = h * oWidth / oHeight; } else { w = StringUtil.convertToInt(c2[0], DEFAULT_SIZE); } } else { w = StringUtil.convertToInt(c2[0], DEFAULT_SIZE); h = StringUtil.convertToInt(c2[1], DEFAULT_SIZE); double ar1 = oWidth / (double) oHeight; double ar2 = w / (double) h; if (ar1 > ar2) { int w2 = (int) (oHeight * ar2); int l = (oWidth / 2) - (w2 / 2); img = img.crop(l, 0, l+w2, oHeight); } else if (ar1 < ar2) { int h2 = (int) (oWidth / ar2); int t = (oHeight / 2) - (h2 / 2); img = img.crop(0, t, oWidth, t+h2); } } return img.getResizedToWidth(w); } } }
ImageUtil { public static final Image transformImage(Image img, String command) { if (StringUtil.isEmpty(command) || img == null) { return img; } if (!isValidCommand(command)) { _log.error("Cannot recognize transform command ["+command+"]. " + "Check Javadoc for instructions."); return img; } int oWidth = img.getWidth(); int oHeight = img.getHeight(); if (_log.isDebugEnabled()) { _log.debug("Command is " + command); _log.debug("Image width is " + oWidth); _log.debug("Image height is " + oHeight); } if (command.contains("-c")) { String[] c1 = command.split("-c"); int t = -1, l = -1, w = 0, h = 0; String[] c2 = c1[0].split("x"); w = StringUtil.convertToInt(c2[0], DEFAULT_SIZE); if (c2.length > 1) { h = StringUtil.convertToInt(c2[1], DEFAULT_SIZE); } else { h = w; } if ((c1.length > 1) && c1[1].startsWith("@")) { String[] c3 = c1[1].split("x"); l = StringUtil.convertToInt(c3[0].substring(1), DEFAULT_SIZE); t = StringUtil.convertToInt(c3[1], DEFAULT_SIZE); } if (l < 0) { l = (oWidth / 2) - (w / 2); } if (t < 0) { t = (oHeight / 2) - (h / 2); } if(_log.isDebugEnabled()) { _log.debug("Cropping using these parameters, l: " + l + ", t: " + t + ", w: " + w + ", h: " + h ); } return img.crop(l, t, l+w, t+h); } else { String[] c2 = command.split("x"); int h = 0, w = 0; if (c2.length==1) { if (command.startsWith("x")) { w = h * oWidth / oHeight; } else { w = StringUtil.convertToInt(c2[0], DEFAULT_SIZE); } } else { w = StringUtil.convertToInt(c2[0], DEFAULT_SIZE); h = StringUtil.convertToInt(c2[1], DEFAULT_SIZE); double ar1 = oWidth / (double) oHeight; double ar2 = w / (double) h; if (ar1 > ar2) { int w2 = (int) (oHeight * ar2); int l = (oWidth / 2) - (w2 / 2); img = img.crop(l, 0, l+w2, oHeight); } else if (ar1 < ar2) { int h2 = (int) (oWidth / ar2); int t = (oHeight / 2) - (h2 / 2); img = img.crop(0, t, oWidth, t+h2); } } return img.getResizedToWidth(w); } } private  ImageUtil(); }
ImageUtil { public static final Image transformImage(Image img, String command) { if (StringUtil.isEmpty(command) || img == null) { return img; } if (!isValidCommand(command)) { _log.error("Cannot recognize transform command ["+command+"]. " + "Check Javadoc for instructions."); return img; } int oWidth = img.getWidth(); int oHeight = img.getHeight(); if (_log.isDebugEnabled()) { _log.debug("Command is " + command); _log.debug("Image width is " + oWidth); _log.debug("Image height is " + oHeight); } if (command.contains("-c")) { String[] c1 = command.split("-c"); int t = -1, l = -1, w = 0, h = 0; String[] c2 = c1[0].split("x"); w = StringUtil.convertToInt(c2[0], DEFAULT_SIZE); if (c2.length > 1) { h = StringUtil.convertToInt(c2[1], DEFAULT_SIZE); } else { h = w; } if ((c1.length > 1) && c1[1].startsWith("@")) { String[] c3 = c1[1].split("x"); l = StringUtil.convertToInt(c3[0].substring(1), DEFAULT_SIZE); t = StringUtil.convertToInt(c3[1], DEFAULT_SIZE); } if (l < 0) { l = (oWidth / 2) - (w / 2); } if (t < 0) { t = (oHeight / 2) - (h / 2); } if(_log.isDebugEnabled()) { _log.debug("Cropping using these parameters, l: " + l + ", t: " + t + ", w: " + w + ", h: " + h ); } return img.crop(l, t, l+w, t+h); } else { String[] c2 = command.split("x"); int h = 0, w = 0; if (c2.length==1) { if (command.startsWith("x")) { w = h * oWidth / oHeight; } else { w = StringUtil.convertToInt(c2[0], DEFAULT_SIZE); } } else { w = StringUtil.convertToInt(c2[0], DEFAULT_SIZE); h = StringUtil.convertToInt(c2[1], DEFAULT_SIZE); double ar1 = oWidth / (double) oHeight; double ar2 = w / (double) h; if (ar1 > ar2) { int w2 = (int) (oHeight * ar2); int l = (oWidth / 2) - (w2 / 2); img = img.crop(l, 0, l+w2, oHeight); } else if (ar1 < ar2) { int h2 = (int) (oWidth / ar2); int t = (oHeight / 2) - (h2 / 2); img = img.crop(0, t, oWidth, t+h2); } } return img.getResizedToWidth(w); } } private  ImageUtil(); static byte[] loadImage(String fullPath, String command); static byte[] loadImage(String fullPath, String command, boolean overrideOld); static byte[] loadImage(URL url, String command); static void cropImage(String fullPath, String command, int resizedWidth, boolean replaceOriginal); static byte[] getDefaultImage(); static boolean isLargerThan(InputStream inputStream, int width, int height); static final Image transformImage(Image img, String command); static final boolean isValidCommand(String command); static String createCropCommand(Integer newWidth, Integer newHeight,
			Integer fromX, Integer fromY); static String convertImageFiletoBase64(File imageFile); }
ImageUtil { public static final Image transformImage(Image img, String command) { if (StringUtil.isEmpty(command) || img == null) { return img; } if (!isValidCommand(command)) { _log.error("Cannot recognize transform command ["+command+"]. " + "Check Javadoc for instructions."); return img; } int oWidth = img.getWidth(); int oHeight = img.getHeight(); if (_log.isDebugEnabled()) { _log.debug("Command is " + command); _log.debug("Image width is " + oWidth); _log.debug("Image height is " + oHeight); } if (command.contains("-c")) { String[] c1 = command.split("-c"); int t = -1, l = -1, w = 0, h = 0; String[] c2 = c1[0].split("x"); w = StringUtil.convertToInt(c2[0], DEFAULT_SIZE); if (c2.length > 1) { h = StringUtil.convertToInt(c2[1], DEFAULT_SIZE); } else { h = w; } if ((c1.length > 1) && c1[1].startsWith("@")) { String[] c3 = c1[1].split("x"); l = StringUtil.convertToInt(c3[0].substring(1), DEFAULT_SIZE); t = StringUtil.convertToInt(c3[1], DEFAULT_SIZE); } if (l < 0) { l = (oWidth / 2) - (w / 2); } if (t < 0) { t = (oHeight / 2) - (h / 2); } if(_log.isDebugEnabled()) { _log.debug("Cropping using these parameters, l: " + l + ", t: " + t + ", w: " + w + ", h: " + h ); } return img.crop(l, t, l+w, t+h); } else { String[] c2 = command.split("x"); int h = 0, w = 0; if (c2.length==1) { if (command.startsWith("x")) { w = h * oWidth / oHeight; } else { w = StringUtil.convertToInt(c2[0], DEFAULT_SIZE); } } else { w = StringUtil.convertToInt(c2[0], DEFAULT_SIZE); h = StringUtil.convertToInt(c2[1], DEFAULT_SIZE); double ar1 = oWidth / (double) oHeight; double ar2 = w / (double) h; if (ar1 > ar2) { int w2 = (int) (oHeight * ar2); int l = (oWidth / 2) - (w2 / 2); img = img.crop(l, 0, l+w2, oHeight); } else if (ar1 < ar2) { int h2 = (int) (oWidth / ar2); int t = (oHeight / 2) - (h2 / 2); img = img.crop(0, t, oWidth, t+h2); } } return img.getResizedToWidth(w); } } private  ImageUtil(); static byte[] loadImage(String fullPath, String command); static byte[] loadImage(String fullPath, String command, boolean overrideOld); static byte[] loadImage(URL url, String command); static void cropImage(String fullPath, String command, int resizedWidth, boolean replaceOriginal); static byte[] getDefaultImage(); static boolean isLargerThan(InputStream inputStream, int width, int height); static final Image transformImage(Image img, String command); static final boolean isValidCommand(String command); static String createCropCommand(Integer newWidth, Integer newHeight,
			Integer fromX, Integer fromY); static String convertImageFiletoBase64(File imageFile);  }