String imgExtension = filenameToUse.substring(index + 1);
// paths
String imageServerPath = FlexibleStringExpander.expandString(UtilProperties.getPropertyValue("catalog", "image.server.path"), context);
String imageUrlPrefix = UtilProperties.getPropertyValue("catalog", "image.url.prefix");
FlexibleStringExpander filenameExpander;
String fileLocation = null;
String type = null;
String id = null;
if (viewType.toLowerCase().contains("main")) {
String filenameFormat = UtilProperties.getPropertyValue("catalog", "image.filename.format");
filenameExpander = FlexibleStringExpander.getInstance(filenameFormat);
id = (String) context.get("productId");
fileLocation = filenameExpander.expandString(UtilMisc.toMap("location", "products", "id", id, "type", "original"));
} else if (viewType.toLowerCase().contains("additional") && viewNumber != null && !viewNumber.equals("0")) {
String filenameFormat = UtilProperties.getPropertyValue("catalog", "image.filename.additionalviewsize.format");
filenameExpander = FlexibleStringExpander.getInstance(filenameFormat);
id = (String) context.get("productId");
if (filenameFormat.endsWith("${id}")) {
id = id + "_View_" + viewNumber;
} else {
viewType = "additional" + viewNumber;
}
fileLocation = filenameExpander.expandString(UtilMisc.toMap("location", "products", "id", id, "viewtype", viewType, "sizetype", "original"));
} else {
return ServiceUtil.returnError(UtilProperties.getMessage(resource, "ProductImageViewType", UtilMisc.toMap("viewType", type), locale));
}
if (fileLocation.lastIndexOf("/") != -1) {
fileLocation.substring(0, fileLocation.lastIndexOf("/") + 1); // adding 1 to include the trailing slash
}
/* get original BUFFERED IMAGE */
resultBufImgMap.putAll(ImageTransform.getBufferedImage(imageServerPath + "/" + fileLocation + "." + imgExtension, locale));
if (resultBufImgMap.containsKey("responseMessage") && resultBufImgMap.get("responseMessage").equals("success")) {
bufImg = (BufferedImage) resultBufImgMap.get("bufferedImage");
// get Dimensions
imgHeight = bufImg.getHeight();
imgWidth = bufImg.getWidth();
if (imgHeight == 0.0 || imgWidth == 0.0) {
String errMsg = UtilProperties.getMessage(resource, "ScaleImage.one_current_image_dimension_is_null", locale) + " : imgHeight = " + imgHeight + " ; imgWidth = " + imgWidth;
Debug.logError(errMsg, module);
result.put("errorMessage", errMsg);
return result;
}
/* Scale image for each size from ImageProperties.xml */
for (Map.Entry<String, Map<String, String>> entry : imgPropertyMap.entrySet()) {
String sizeType = entry.getKey();
// Scale
resultScaleImgMap.putAll(ImageTransform.scaleImage(bufImg, imgHeight, imgWidth, imgPropertyMap, sizeType, locale));
/* Write the new image file */
if (resultScaleImgMap.containsKey("responseMessage") && resultScaleImgMap.get("responseMessage").equals("success")) {
bufNewImg = (BufferedImage) resultScaleImgMap.get("bufferedImage");
// Build full path for the new scaled image
String newFileLocation = null;
filenameToUse = sizeType + filenameToUse.substring(filenameToUse.lastIndexOf("."));
if (viewType.toLowerCase().contains("main")) {
newFileLocation = filenameExpander.expandString(UtilMisc.toMap("location", "products", "id", id, "type", sizeType));
} else if (viewType.toLowerCase().contains("additional")) {
newFileLocation = filenameExpander.expandString(UtilMisc.toMap("location", "products", "id", id, "viewtype", viewType, "sizetype", sizeType));
}
String newFilePathPrefix = "";
if (newFileLocation.lastIndexOf("/") != -1) {
newFilePathPrefix = newFileLocation.substring(0, newFileLocation.lastIndexOf("/") + 1); // adding 1 to include the trailing slash
}