NIHVIVO-161 Tweak the javascript and the arithmetic for cropping the image.
This commit is contained in:
parent
61714819b0
commit
15bcf62f7f
3 changed files with 33 additions and 27 deletions
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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);
|
||||
|
||||
|
|
|
@ -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));
|
Loading…
Add table
Reference in a new issue