use of org.olat.core.commons.services.image.ImageService in project OpenOLAT by OpenOLAT.
the class CourseLayoutGeneratorController method processUploadedImage.
// process uploaded file according to image size and persist in <course>/layout/logo.xy
private boolean processUploadedImage(File image) {
int height = 0;
int width = 0;
int[] size = customCMgr.getImageSize(image);
if (size != null) {
width = size[0];
height = size[1];
} else {
return false;
}
// target file:
String fileType = logoUpl.getUploadFileName().substring(logoUpl.getUploadFileName().lastIndexOf("."));
VFSContainer base = (VFSContainer) courseEnvironment.getCourseBaseContainer().resolve(CourseLayoutHelper.LAYOUT_COURSE_SUBFOLDER);
if (base == null) {
base = courseEnvironment.getCourseBaseContainer().createChildContainer(CourseLayoutHelper.LAYOUT_COURSE_SUBFOLDER);
}
VFSContainer customBase = (VFSContainer) base.resolve("/" + CourseLayoutHelper.CONFIG_KEY_CUSTOM);
if (customBase == null) {
customBase = base.createChildContainer(CourseLayoutHelper.CONFIG_KEY_CUSTOM);
}
if (customBase.resolve("logo" + fileType) != null)
customBase.resolve("logo" + fileType).delete();
VFSLeaf targetFile = customBase.createChildLeaf("logo" + fileType);
int maxHeight = CourseLayoutHelper.getLogoMaxHeight();
int maxWidth = CourseLayoutHelper.getLogoMaxWidth();
if (height > maxHeight || width > maxWidth) {
// scale image
try {
ImageService helper = CourseLayoutHelper.getImageHelperToUse();
String extension = FileUtils.getFileSuffix(logoUpl.getUploadFileName());
helper.scaleImage(image, extension, targetFile, maxWidth, maxHeight);
} catch (Exception e) {
logError("could not find to be scaled image", e);
return false;
}
} else {
// only persist without scaling
InputStream in = null;
OutputStream out = null;
try {
in = new FileInputStream(image);
out = targetFile.getOutputStream(false);
FileUtils.copy(in, out);
} catch (FileNotFoundException e) {
logError("Problem reading uploaded image to copy", e);
return false;
} finally {
FileUtils.closeSafely(in);
FileUtils.closeSafely(out);
}
}
return true;
}
Aggregations