ResizeOption resizeOption = this.resizeOption;
String srcAttr = this.srcAttribute;
boolean hideDimensions = this.hideDimensions;
StorageItem item = null;
Integer originalWidth = null;
Integer originalHeight = null;
Map<String, ImageCrop> crops = null;
if (this.state != null) { // backwards compatibility path
State objectState = this.state;
String field = this.field;
if (ObjectUtils.isBlank(field)) {
field = findStorageItemField(objectState);
}
item = findStorageItem(objectState, field);
if (item != null) {
originalWidth = findDimension(objectState, field, "width");
originalHeight = findDimension(objectState, field, "height");
crops = findImageCrops(objectState, field);
}
} else { // new code path
item = this.item;
if (item != null) {
originalWidth = findDimension(item, "width");
originalHeight = findDimension(item, "height");
crops = findImageCrops(item);
}
}
// null out all dimensions that are less than or equal to zero
originalWidth = originalWidth != null && originalWidth <= 0 ? null : originalWidth;
originalHeight = originalHeight != null && originalHeight <= 0 ? null : originalHeight;
width = width != null && width <= 0 ? null : width;
height = height != null && height <= 0 ? null : height;
if (item != null) {
Map<String, Object> options = new LinkedHashMap<String, Object>();
Integer cropX = null, cropY = null, cropWidth = null, cropHeight = null;
// set fields from this standard size if they haven't already been set
if (standardImageSize != null) {
Integer standardWidth = standardImageSize.getWidth();
Integer standardHeight = standardImageSize.getHeight();
if (standardWidth <= 0) { standardWidth = null; }
if (standardHeight <= 0) { standardHeight = null; }
Double standardAspectRatio = null;
if (standardWidth != null && standardHeight != null) {
standardAspectRatio = (double) standardWidth / (double) standardHeight;
}
// if only one of either width or height is set then calculate
// the other dimension based on the standardImageSize aspect
// ratio rather than blindly taking the other standardImageSize
// dimension.
if (standardAspectRatio != null && (width != null || height != null)) {
if (width != null && height == null) {
height = (int) (width / standardAspectRatio);
} else if (width == null && height != null) {
width = (int) (height * standardAspectRatio);
}
} else {
// get the standard image dimensions
if (width == null) {
width = standardWidth;
}
if (height == null) {
height = standardHeight;
}
}
// get the crop and resize options
if (cropOption == null) {
cropOption = standardImageSize.getCropOption();
}
if (resizeOption == null) {
resizeOption = standardImageSize.getResizeOption();
}
// get a potentially smaller image from the StorageItem. This improves
// resize performance on large images.
StorageItem alternateItem = findStorageItemForSize(item, width, height);
if (alternateItem != item) {
item = alternateItem;
originalWidth = findDimension(item, "width");
originalHeight = findDimension(item, "height");
}