Search in sources :

Example 6 with ImageService

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;
}
Also used : VFSLeaf(org.olat.core.util.vfs.VFSLeaf) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) VFSContainer(org.olat.core.util.vfs.VFSContainer) OutputStream(java.io.OutputStream) FileNotFoundException(java.io.FileNotFoundException) ImageService(org.olat.core.commons.services.image.ImageService) AssertException(org.olat.core.logging.AssertException) FileNotFoundException(java.io.FileNotFoundException) FileInputStream(java.io.FileInputStream)

Aggregations

ImageService (org.olat.core.commons.services.image.ImageService)6 File (java.io.File)4 FileInputStream (java.io.FileInputStream)2 FileNotFoundException (java.io.FileNotFoundException)2 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 OutputStream (java.io.OutputStream)2 Size (org.olat.core.commons.services.image.Size)2 FileMediaResource (org.olat.core.gui.media.FileMediaResource)2 AssertException (org.olat.core.logging.AssertException)2 VFSContainer (org.olat.core.util.vfs.VFSContainer)2 VFSLeaf (org.olat.core.util.vfs.VFSLeaf)2