if (!(event.getContext() instanceof DocumentEventContext)) {
return;
}
DocumentEventContext ctx = (DocumentEventContext) event.getContext();
DocumentModel doc = ctx.getSourceDocument();
if (!ARTICLE_TYPE.equals(doc.getType())) {
return;
}
Blob image = (Blob) doc.getPropertyValue(CONTENT_PICTURE_PICTURE_PROPERTY);
if (image == null) {
return;
}
ImagingService service;
try {
service = Framework.getService(ImagingService.class);
} catch (Exception e) {
throw new ClientException("Failed to get ImagingService", e);
}
ImageInfo info = service.getImageInfo(image);
int width = info.getWidth();
int height = info.getHeight();
float wScale = (float) RESIZED_IMAGE_WIDTH / width;
float hscale = (float) RESIZED_IMAGE_HEIGHT / height;
float scale = Math.min(wScale, hscale);
if (scale < 1) {
image = service.resize(image, "jpg", (int) (width * scale),
(int) (height * scale), info.getDepth());
image.setMimeType("image/jpeg"); // XXX : Should be automatic
doc.setPropertyValue(CONTENT_PICTURE_PICTURE_PROPERTY,
(Serializable) image);
}
}