String format = MimetypesFileTypeMap.getDefaultFileTypeMap().getContentType(newFileName).split("/")[1];
try {
// Read file form disk
bsrc = FileManipulation.bitmapToImage(inputStream, format);
} catch (IOException e1) {
error.fire(new ErrorEvent("Error", "error reading file<br/>" + e1.getMessage()));
return false;
}
int resizedParam = bsrc.getWidth() > bsrc.getHeight() ? bsrc.getWidth() : bsrc.getHeight();
double scale = (double) size / resizedParam;
Double widthInDouble = ((Double) scale * bsrc.getWidth());
int width = widthInDouble.intValue();
Double heightInDouble = ((Double) scale * bsrc.getHeight());
int height = heightInDouble.intValue();
// Too small picture or original size
if (width > bsrc.getWidth() || height > bsrc.getHeight() || size == 0) {
width = bsrc.getWidth();
height = bsrc.getHeight();
}
// scale image if need
BufferedImage bdest = FileManipulation
.getScaledInstance(bsrc, width, height, RenderingHints.VALUE_INTERPOLATION_BICUBIC, true);
// Determine new path of image file
String dest = includeUploadRoot ? this.uploadRootPath + transformPath(newFileName, pattern) : transformPath(
newFileName, pattern);
try {
// save to disk
FileManipulation.imageToBitmap(bdest, dest, format);
} catch (IOException ex) {
error.fire(new ErrorEvent("Error", "error saving image to disc: " + ex.getMessage()));
return false;
}
return true;
}