From 15bcf62f7f7f49a7306161815fe6dd60a2e128fe Mon Sep 17 00:00:00 2001 From: jeb228 Date: Thu, 1 Jul 2010 15:54:31 +0000 Subject: [PATCH] NIHVIVO-161 Tweak the javascript and the arithmetic for cropping the image. --- .../freemarker/ImageUploadController.java | 26 ++++++++++++------- .../freemarker/ImageUploadHelper.java | 17 ++++++++---- webapp/web/js/imageUpload/cropImage.js | 17 ++++-------- 3 files changed, 33 insertions(+), 27 deletions(-) diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/freemarker/ImageUploadController.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/freemarker/ImageUploadController.java index 6c1fecc86..052e8bb2f 100644 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/freemarker/ImageUploadController.java +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/freemarker/ImageUploadController.java @@ -151,10 +151,13 @@ public class ImageUploadController extends FreeMarkerHttpServlet { switch (values.getType()) { case FORWARD: doForward(vreq, response, values); + break; case TEMPLATE: doTemplate(vreq, response, values); + break; case EXCEPTION: doException(vreq, response, values); + break; } } catch (Exception e) { log.error("Could not produce response page", e); @@ -341,27 +344,30 @@ public class ImageUploadController extends FreeMarkerHttpServlet { * Did we get the cropping coordinates? */ private CropRectangle validateCropCoordinates(VitroRequest vreq) { - int x = getRequiredIntegerParameter(vreq, "x"); - int y = getRequiredIntegerParameter(vreq, "y"); - int h = getRequiredIntegerParameter(vreq, "h"); - int w = getRequiredIntegerParameter(vreq, "w"); + int x = getIntegerParameter(vreq, "x", 0); + int y = getIntegerParameter(vreq, "y", 0); + int h = getIntegerParameter(vreq, "h", THUMBNAIL_HEIGHT); + int w = getIntegerParameter(vreq, "w", THUMBNAIL_WIDTH); return new CropRectangle(x, y, h, w); } /** * We need this parameter on the request, and it must be a valid integer. */ - private int getRequiredIntegerParameter(HttpServletRequest req, String key) { + private int getIntegerParameter(HttpServletRequest req, String key, + int defaultValue) { String string = req.getParameter(key); - if (string == null) { - throw new IllegalStateException( - "Request did not contain a value for '" + key + "'"); + if ((string == null) || (string.isEmpty())) { + log.debug("No value for '" + key + "'; using default value = " + + defaultValue); + return defaultValue; } try { return Integer.parseInt(string); } catch (NumberFormatException e) { - throw new IllegalStateException("Value for '" + key - + "' was not a valid integer: '" + string + "'"); + log.warn("Value for '" + key + "' was not a valid integer: '" + + string + "'; using default value = " + defaultValue); + return defaultValue; } } diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/freemarker/ImageUploadHelper.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/freemarker/ImageUploadHelper.java index a329b93a5..633a52769 100644 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/freemarker/ImageUploadHelper.java +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/freemarker/ImageUploadHelper.java @@ -329,8 +329,8 @@ public class ImageUploadHelper { BufferedImage bsrc = ImageIO.read(source); // Insure that x and y fall within the image dimensions. - int x = Math.max(0, Math.min(bsrc.getWidth(), crop.x)); - int y = Math.max(0, Math.min(bsrc.getHeight(), crop.y)); + int x = Math.max(0, Math.min(bsrc.getWidth(), Math.abs(crop.x))); + int y = Math.max(0, Math.min(bsrc.getHeight(), Math.abs(crop.y))); // Insure that width and height are reasonable. int w = Math.max(5, Math.min(bsrc.getWidth() - x, crop.width)); @@ -340,14 +340,21 @@ public class ImageUploadHelper { double scaleWidth = ((double) THUMBNAIL_WIDTH) / ((double) w); double scaleHeight = ((double) THUMBNAIL_HEIGHT) / ((double) h); + log.debug("Generating a thumbnail, initial crop info: " + crop.x + ", " + + crop.y + ", " + crop.width + ", " + crop.height); + log.debug("Generating a thumbnail, bounded crop info: " + x + ", " + y + + ", " + w + ", " + h); + log.debug("Generating a thumbnail, scales: " + scaleWidth + ", " + + scaleHeight); + // Create the transform. AffineTransform at = new AffineTransform(); - at.translate(-x, -y); at.scale(scaleWidth, scaleHeight); + at.translate(-x, -y); // Apply the transform. - BufferedImage bdest = new BufferedImage(crop.width, crop.height, - BufferedImage.TYPE_INT_RGB); + BufferedImage bdest = new BufferedImage(THUMBNAIL_WIDTH, + THUMBNAIL_HEIGHT, BufferedImage.TYPE_INT_RGB); Graphics2D g = bdest.createGraphics(); g.drawRenderedImage(bsrc, at); diff --git a/webapp/web/js/imageUpload/cropImage.js b/webapp/web/js/imageUpload/cropImage.js index d3f3d315c..f9a6dc054 100644 --- a/webapp/web/js/imageUpload/cropImage.js +++ b/webapp/web/js/imageUpload/cropImage.js @@ -25,21 +25,14 @@ marginLeft: '-' + Math.round(rx * coords.x) + 'px', marginTop: '-' + Math.round(ry * coords.y) + 'px' }); + + $('input[name=x]').val(coords.x); + $('input[name=y]').val(coords.y); + $('input[name=w]').val(coords.w); + $('input[name=h]').val(coords.h); } }; - function removeAlpha(str) { - return str.replace(/[^\d-]/g,''); - }; - - $('#cropImage').submit(function() { - var preview = $('#preview'); - $('input[name=x]').val(removeAlpha(preview.css('marginLeft'))); - $('input[name=y]').val(removeAlpha(preview.css('marginTop'))); - $('input[name=w]').val(removeAlpha(preview.css('width'))); - $('input[name=h]').val(removeAlpha(preview.css('height'))); - }); - }); }(jQuery)); \ No newline at end of file