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()) {
|
switch (values.getType()) {
|
||||||
case FORWARD:
|
case FORWARD:
|
||||||
doForward(vreq, response, values);
|
doForward(vreq, response, values);
|
||||||
|
break;
|
||||||
case TEMPLATE:
|
case TEMPLATE:
|
||||||
doTemplate(vreq, response, values);
|
doTemplate(vreq, response, values);
|
||||||
|
break;
|
||||||
case EXCEPTION:
|
case EXCEPTION:
|
||||||
doException(vreq, response, values);
|
doException(vreq, response, values);
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
log.error("Could not produce response page", e);
|
log.error("Could not produce response page", e);
|
||||||
|
@ -341,27 +344,30 @@ public class ImageUploadController extends FreeMarkerHttpServlet {
|
||||||
* Did we get the cropping coordinates?
|
* Did we get the cropping coordinates?
|
||||||
*/
|
*/
|
||||||
private CropRectangle validateCropCoordinates(VitroRequest vreq) {
|
private CropRectangle validateCropCoordinates(VitroRequest vreq) {
|
||||||
int x = getRequiredIntegerParameter(vreq, "x");
|
int x = getIntegerParameter(vreq, "x", 0);
|
||||||
int y = getRequiredIntegerParameter(vreq, "y");
|
int y = getIntegerParameter(vreq, "y", 0);
|
||||||
int h = getRequiredIntegerParameter(vreq, "h");
|
int h = getIntegerParameter(vreq, "h", THUMBNAIL_HEIGHT);
|
||||||
int w = getRequiredIntegerParameter(vreq, "w");
|
int w = getIntegerParameter(vreq, "w", THUMBNAIL_WIDTH);
|
||||||
return new CropRectangle(x, y, h, w);
|
return new CropRectangle(x, y, h, w);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* We need this parameter on the request, and it must be a valid integer.
|
* 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);
|
String string = req.getParameter(key);
|
||||||
if (string == null) {
|
if ((string == null) || (string.isEmpty())) {
|
||||||
throw new IllegalStateException(
|
log.debug("No value for '" + key + "'; using default value = "
|
||||||
"Request did not contain a value for '" + key + "'");
|
+ defaultValue);
|
||||||
|
return defaultValue;
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
return Integer.parseInt(string);
|
return Integer.parseInt(string);
|
||||||
} catch (NumberFormatException e) {
|
} catch (NumberFormatException e) {
|
||||||
throw new IllegalStateException("Value for '" + key
|
log.warn("Value for '" + key + "' was not a valid integer: '"
|
||||||
+ "' was not a valid integer: '" + string + "'");
|
+ string + "'; using default value = " + defaultValue);
|
||||||
|
return defaultValue;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -329,8 +329,8 @@ public class ImageUploadHelper {
|
||||||
BufferedImage bsrc = ImageIO.read(source);
|
BufferedImage bsrc = ImageIO.read(source);
|
||||||
|
|
||||||
// Insure that x and y fall within the image dimensions.
|
// Insure that x and y fall within the image dimensions.
|
||||||
int x = Math.max(0, Math.min(bsrc.getWidth(), crop.x));
|
int x = Math.max(0, Math.min(bsrc.getWidth(), Math.abs(crop.x)));
|
||||||
int y = Math.max(0, Math.min(bsrc.getHeight(), crop.y));
|
int y = Math.max(0, Math.min(bsrc.getHeight(), Math.abs(crop.y)));
|
||||||
|
|
||||||
// Insure that width and height are reasonable.
|
// Insure that width and height are reasonable.
|
||||||
int w = Math.max(5, Math.min(bsrc.getWidth() - x, crop.width));
|
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 scaleWidth = ((double) THUMBNAIL_WIDTH) / ((double) w);
|
||||||
double scaleHeight = ((double) THUMBNAIL_HEIGHT) / ((double) h);
|
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.
|
// Create the transform.
|
||||||
AffineTransform at = new AffineTransform();
|
AffineTransform at = new AffineTransform();
|
||||||
at.translate(-x, -y);
|
|
||||||
at.scale(scaleWidth, scaleHeight);
|
at.scale(scaleWidth, scaleHeight);
|
||||||
|
at.translate(-x, -y);
|
||||||
|
|
||||||
// Apply the transform.
|
// Apply the transform.
|
||||||
BufferedImage bdest = new BufferedImage(crop.width, crop.height,
|
BufferedImage bdest = new BufferedImage(THUMBNAIL_WIDTH,
|
||||||
BufferedImage.TYPE_INT_RGB);
|
THUMBNAIL_HEIGHT, BufferedImage.TYPE_INT_RGB);
|
||||||
Graphics2D g = bdest.createGraphics();
|
Graphics2D g = bdest.createGraphics();
|
||||||
g.drawRenderedImage(bsrc, at);
|
g.drawRenderedImage(bsrc, at);
|
||||||
|
|
||||||
|
|
|
@ -25,21 +25,14 @@
|
||||||
marginLeft: '-' + Math.round(rx * coords.x) + 'px',
|
marginLeft: '-' + Math.round(rx * coords.x) + 'px',
|
||||||
marginTop: '-' + Math.round(ry * coords.y) + '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));
|
}(jQuery));
|
Loading…
Add table
Add a link
Reference in a new issue