Search in sources :

Example 1 with ImageService

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;
}
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)

Example 2 with ImageService

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);
    }
}
Also used : Size(org.olat.core.commons.services.image.Size) IOException(java.io.IOException) File(java.io.File) ImageService(org.olat.core.commons.services.image.ImageService)

Example 3 with ImageService

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);
}
Also used : FileMediaResource(org.olat.core.gui.media.FileMediaResource) File(java.io.File) ImageService(org.olat.core.commons.services.image.ImageService)

Example 4 with ImageService

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);
    }
}
Also used : Size(org.olat.core.commons.services.image.Size) IOException(java.io.IOException) File(java.io.File) ImageService(org.olat.core.commons.services.image.ImageService)

Example 5 with ImageService

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);
}
Also used : FileMediaResource(org.olat.core.gui.media.FileMediaResource) File(java.io.File) ImageService(org.olat.core.commons.services.image.ImageService)

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