use of org.olat.core.commons.services.image.ImageService in project openolat by klemens.
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;
}
use of org.olat.core.commons.services.image.ImageService in project openolat by klemens.
the class DisplayPortraitManager method setImage.
private void setImage(File file, String filename, String username, String prefix, String masterImagePrefix, String largeImagePrefix, String smallImagePrefix, int maxBigWidth, int maxSmallWidth) {
File directory = getPortraitDir(username);
if (directory != null) {
for (File currentImage : directory.listFiles()) {
if (currentImage.equals(file)) {
continue;
} else if (currentImage.getName().startsWith(prefix)) {
currentImage.delete();
}
}
}
String extension = FileUtils.getFileSuffix(file.getName());
if (!StringHelper.containsNonWhitespace(extension)) {
if (StringHelper.containsNonWhitespace(filename)) {
extension = FileUtils.getFileSuffix(filename);
}
if (!StringHelper.containsNonWhitespace(extension)) {
extension = "png";
}
}
try {
File masterFile = new File(directory, masterImagePrefix + "." + extension);
Files.copy(file.toPath(), masterFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
} catch (IOException e) {
log.error("", e);
}
File bigFile = new File(directory, largeImagePrefix + "." + extension);
File smallFile = new File(directory, smallImagePrefix + "." + extension);
ImageService imageHelper = CoreSpringFactory.getImpl(ImageService.class);
Size size = imageHelper.scaleImage(file, extension, bigFile, maxBigWidth, HEIGHT_BIG, false);
if (size != null) {
size = imageHelper.scaleImage(file, extension, smallFile, maxSmallWidth, HEIGHT_SMALL, false);
}
}
use of org.olat.core.commons.services.image.ImageService in project openolat by klemens.
the class LogoMapper method handle.
@Override
public MediaResource handle(String relPath, HttpServletRequest request) {
File logo = layoutModule.getLogo();
File dir = layoutModule.getLogoDirectory();
File scaledLogo = new File(dir, relPath);
if (lastModified < logo.lastModified() || !scaledLogo.exists()) {
ImageService imageService = CoreSpringFactory.getImpl(ImageService.class);
File logo1x = new File(dir, "oo-logo@1x.png");
imageService.scaleImage(logo, "png", logo1x, 104, 50, false);
File logo2x = new File(dir, "oo-logo@2x.png");
imageService.scaleImage(logo, "png", logo2x, 208, 50, false);
}
return new FileMediaResource(scaledLogo);
}
use of org.olat.core.commons.services.image.ImageService in project OpenOLAT by OpenOLAT.
the class DisplayPortraitManager method setImage.
private void setImage(File file, String filename, String username, String prefix, String masterImagePrefix, String largeImagePrefix, String smallImagePrefix, int maxBigWidth, int maxSmallWidth) {
File directory = getPortraitDir(username);
if (directory != null) {
for (File currentImage : directory.listFiles()) {
if (currentImage.equals(file)) {
continue;
} else if (currentImage.getName().startsWith(prefix)) {
currentImage.delete();
}
}
}
String extension = FileUtils.getFileSuffix(file.getName());
if (!StringHelper.containsNonWhitespace(extension)) {
if (StringHelper.containsNonWhitespace(filename)) {
extension = FileUtils.getFileSuffix(filename);
}
if (!StringHelper.containsNonWhitespace(extension)) {
extension = "png";
}
}
try {
File masterFile = new File(directory, masterImagePrefix + "." + extension);
Files.copy(file.toPath(), masterFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
} catch (IOException e) {
log.error("", e);
}
File bigFile = new File(directory, largeImagePrefix + "." + extension);
File smallFile = new File(directory, smallImagePrefix + "." + extension);
ImageService imageHelper = CoreSpringFactory.getImpl(ImageService.class);
Size size = imageHelper.scaleImage(file, extension, bigFile, maxBigWidth, HEIGHT_BIG, false);
if (size != null) {
size = imageHelper.scaleImage(file, extension, smallFile, maxSmallWidth, HEIGHT_SMALL, false);
}
}
use of org.olat.core.commons.services.image.ImageService in project OpenOLAT by OpenOLAT.
the class LogoMapper method handle.
@Override
public MediaResource handle(String relPath, HttpServletRequest request) {
File logo = layoutModule.getLogo();
File dir = layoutModule.getLogoDirectory();
File scaledLogo = new File(dir, relPath);
if (lastModified < logo.lastModified() || !scaledLogo.exists()) {
ImageService imageService = CoreSpringFactory.getImpl(ImageService.class);
File logo1x = new File(dir, "oo-logo@1x.png");
imageService.scaleImage(logo, "png", logo1x, 104, 50, false);
File logo2x = new File(dir, "oo-logo@2x.png");
imageService.scaleImage(logo, "png", logo2x, 208, 50, false);
}
return new FileMediaResource(scaledLogo);
}
Aggregations