/** Returns all the attributes that will get placed on the img tag. */
public Map<String, String> toAttributes() {
// set all the attributes
Map<String, String> attributes = new LinkedHashMap<String, String>();
ImageEditor editor = this.editor;
StandardImageSize standardImageSize = this.standardImageSize;
Integer width = this.width;
Integer height = this.height;
CropOption cropOption = this.cropOption;
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");
}
// get the crop coordinates
ImageCrop crop;
if (crops != null && (crop = crops.get(standardImageSize.getId().toString())) != null &&
originalWidth != null && originalHeight != null) {
cropX = (int) (crop.getX() * originalWidth);
cropY = (int) (crop.getY() * originalHeight);
cropWidth = (int) (crop.getWidth() * originalWidth);
if (standardAspectRatio != null) {
cropHeight = (int) Math.round(cropWidth / standardAspectRatio);
} else {
cropHeight = (int) (crop.getHeight() * originalHeight);
}
}
}
// if the crop info is unavailable, assume that the image
// dimensions are the crop dimensions in case the image editor
// knows how to crop without the x & y coordinates
if (cropWidth == null) {
cropWidth = width;
}
if (cropHeight == null) {
cropHeight = height;
}
// set the options
if (cropOption != null) {
options.put(ImageEditor.CROP_OPTION, cropOption.getImageEditorOption());
}
if (resizeOption != null) {
options.put(ImageEditor.RESIZE_OPTION, resizeOption.getImageEditorOption());
}
if (isEdits()) {
@SuppressWarnings("unchecked")
Map<String, Object> edits = (Map<String, Object>) item.getMetadata().get("cms.edits");
if (edits != null) {
ImageEditor realEditor = editor;
if (realEditor == null) {
realEditor = ImageEditor.Static.getDefault();
}
//rotate first
Set<Map.Entry<String, Object>> entrySet = new TreeMap<String, Object>(edits).entrySet();
for (Map.Entry<String, Object> entry : entrySet) {
if (entry.getKey().equals("rotate")) {
item = realEditor.edit(item, entry.getKey(), null, entry.getValue());
}
}
for (Map.Entry<String, Object> entry : entrySet) {
if (!entry.getKey().equals("rotate")) {
item = realEditor.edit(item, entry.getKey(), null, entry.getValue());
}
}
}
}