Search in sources :

Example 11 with FinalSize

use of org.olat.core.commons.services.thumbnail.FinalSize in project OpenOLAT by OpenOLAT.

the class ImageMagickHelper method extractSizeFromOutput.

/**
 * Extract informations from the process:<br/>
 * The image was scaled:
 * /HotCoffee/olatdatas/openolat/bcroot/tmp/ryomou.jpg JPEG 579x579 579x579+0+0 8-bit DirectClass 327KB 0.020u 0:00.020/HotCoffee/olatdatas/openolat/bcroot/tmp/ryomou.jpg=>/HotCoffee/olatdatas/openolat_mysql/bcroot/repository/27394049.png JPEG 579x579=>570x570 8-bit DirectClass 803KB 0.150u 0:00.160<br/>
 * The image wasn't scaled:
 * /HotCoffee/olatdatas/openolat/bcroot/tmp/yukino.jpg JPEG 184x184 184x184+0+0 8-bit DirectClass 17.5KB 0.000u 0:00.009/HotCoffee/olatdatas/openolat/bcroot/tmp/yukino.jpg=>/HotCoffee/olatdatas/openolat_mysql/bcroot/repository/27394049.png JPEG 184x184 8-bit DirectClass 49.2KB 0.060u 0:00.060
 *
 * @param thumbnailBaseFile
 * @param output
 * @return
 */
private final FinalSize extractSizeFromOutput(File thumbnailBaseFile, StringBuilder output) {
    try {
        String verbose = output.toString();
        int lastIndex = verbose.lastIndexOf(thumbnailBaseFile.getName());
        if (lastIndex > 0) {
            int sizeIndex = verbose.indexOf("=>", lastIndex);
            // => appears if the image is downscaled
            if (sizeIndex > 0) {
                int stopIndex = verbose.indexOf(' ', sizeIndex);
                if (stopIndex > sizeIndex) {
                    String sizeStr = verbose.substring(sizeIndex + 2, stopIndex);
                    FinalSize size = extractSizeFromChuck(sizeStr);
                    return size;
                }
            // no scaling apparently, try to find the size
            } else {
                String ending = verbose.substring(lastIndex + thumbnailBaseFile.getName().length());
                String[] endings = ending.split(" ");
                for (String chuck : endings) {
                    FinalSize size = extractSizeFromChuck(chuck);
                    if (size != null) {
                        return size;
                    }
                }
            }
        }
    } catch (NumberFormatException e) {
        log.error("Error parsing output: " + output, null);
    }
    return null;
}
Also used : FinalSize(org.olat.core.commons.services.thumbnail.FinalSize)

Example 12 with FinalSize

use of org.olat.core.commons.services.thumbnail.FinalSize in project OpenOLAT by OpenOLAT.

the class ImageMagickHelper method scaleImage.

@Override
public Size scaleImage(File image, String imgExt, VFSLeaf scaledImage, int maxWidth, int maxHeight) {
    File scaledBaseFile = extractIOFile(scaledImage);
    FinalSize finalSize = generateThumbnail(image, scaledBaseFile, false, maxWidth, maxHeight, false);
    if (finalSize != null) {
        return new Size(finalSize.getWidth(), finalSize.getHeight(), true);
    }
    return null;
}
Also used : FinalSize(org.olat.core.commons.services.thumbnail.FinalSize) Size(org.olat.core.commons.services.image.Size) FinalSize(org.olat.core.commons.services.thumbnail.FinalSize) File(java.io.File)

Example 13 with FinalSize

use of org.olat.core.commons.services.thumbnail.FinalSize in project openolat by klemens.

the class MetaInfoFileImpl method getThumbnailInfo.

private Thumbnail getThumbnailInfo(int maxWidth, int maxHeight, boolean fill) {
    for (Thumbnail thumbnail : thumbnails) {
        if (maxHeight == thumbnail.getMaxHeight() && maxWidth == thumbnail.getMaxWidth()) {
            if (thumbnail.exists()) {
                return thumbnail;
            }
        }
    }
    // generate a file name
    File metaLoc = metaFile.getParentFile();
    String name = originFile.getName();
    String extension = FileUtils.getFileSuffix(name);
    String nameOnly = name.substring(0, name.length() - extension.length() - 1);
    String randuuid = UUID.randomUUID().toString();
    String thumbnailExtension = preferedThumbnailType(extension);
    File thumbnailFile = new File(metaLoc, nameOnly + "_" + randuuid + "_" + maxHeight + "x" + maxWidth + (fill ? "xfill" : "") + "." + thumbnailExtension);
    // generate thumbnail
    long start = 0l;
    if (log.isDebug())
        start = System.currentTimeMillis();
    VFSLeaf thumbnailLeaf = new LocalFileImpl(thumbnailFile);
    VFSLeaf originLeaf = new LocalFileImpl(originFile);
    if (thumbnailService != null && thumbnailService.isThumbnailPossible(thumbnailLeaf)) {
        try {
            if (thumbnails.isEmpty()) {
                // be paranoid
                cannotGenerateThumbnail = true;
                write();
            }
            log.info("Start thumbnail: " + thumbnailLeaf);
            FinalSize finalSize = thumbnailService.generateThumbnail(originLeaf, thumbnailLeaf, maxWidth, maxHeight, fill);
            if (finalSize == null) {
                return null;
            } else {
                Thumbnail thumbnail = new Thumbnail();
                thumbnail.setMaxHeight(maxHeight);
                thumbnail.setMaxWidth(maxWidth);
                thumbnail.setFinalHeight(finalSize.getHeight());
                thumbnail.setFinalWidth(finalSize.getWidth());
                thumbnail.setFill(true);
                thumbnail.setThumbnailFile(thumbnailFile);
                thumbnails.add(thumbnail);
                cannotGenerateThumbnail = false;
                write();
                log.info("Create thumbnail: " + thumbnailLeaf);
                if (log.isDebug()) {
                    log.debug("Creation of thumbnail takes (ms): " + (System.currentTimeMillis() - start));
                }
                return thumbnail;
            }
        } catch (CannotGenerateThumbnailException e) {
            // don't try every time to create the thumbnail.
            cannotGenerateThumbnail = true;
            write();
            return null;
        }
    }
    return null;
}
Also used : VFSLeaf(org.olat.core.util.vfs.VFSLeaf) CannotGenerateThumbnailException(org.olat.core.commons.services.thumbnail.CannotGenerateThumbnailException) LocalFileImpl(org.olat.core.util.vfs.LocalFileImpl) FinalSize(org.olat.core.commons.services.thumbnail.FinalSize) File(java.io.File)

Example 14 with FinalSize

use of org.olat.core.commons.services.thumbnail.FinalSize in project openolat by klemens.

the class ImageMagickHelper method extractSizeFromOutput.

/**
 * Extract informations from the process:<br/>
 * The image was scaled:
 * /HotCoffee/olatdatas/openolat/bcroot/tmp/ryomou.jpg JPEG 579x579 579x579+0+0 8-bit DirectClass 327KB 0.020u 0:00.020/HotCoffee/olatdatas/openolat/bcroot/tmp/ryomou.jpg=>/HotCoffee/olatdatas/openolat_mysql/bcroot/repository/27394049.png JPEG 579x579=>570x570 8-bit DirectClass 803KB 0.150u 0:00.160<br/>
 * The image wasn't scaled:
 * /HotCoffee/olatdatas/openolat/bcroot/tmp/yukino.jpg JPEG 184x184 184x184+0+0 8-bit DirectClass 17.5KB 0.000u 0:00.009/HotCoffee/olatdatas/openolat/bcroot/tmp/yukino.jpg=>/HotCoffee/olatdatas/openolat_mysql/bcroot/repository/27394049.png JPEG 184x184 8-bit DirectClass 49.2KB 0.060u 0:00.060
 *
 * @param thumbnailBaseFile
 * @param output
 * @return
 */
private final FinalSize extractSizeFromOutput(File thumbnailBaseFile, StringBuilder output) {
    try {
        String verbose = output.toString();
        int lastIndex = verbose.lastIndexOf(thumbnailBaseFile.getName());
        if (lastIndex > 0) {
            int sizeIndex = verbose.indexOf("=>", lastIndex);
            // => appears if the image is downscaled
            if (sizeIndex > 0) {
                int stopIndex = verbose.indexOf(' ', sizeIndex);
                if (stopIndex > sizeIndex) {
                    String sizeStr = verbose.substring(sizeIndex + 2, stopIndex);
                    FinalSize size = extractSizeFromChuck(sizeStr);
                    return size;
                }
            // no scaling apparently, try to find the size
            } else {
                String ending = verbose.substring(lastIndex + thumbnailBaseFile.getName().length());
                String[] endings = ending.split(" ");
                for (String chuck : endings) {
                    FinalSize size = extractSizeFromChuck(chuck);
                    if (size != null) {
                        return size;
                    }
                }
            }
        }
    } catch (NumberFormatException e) {
        log.error("Error parsing output: " + output, null);
    }
    return null;
}
Also used : FinalSize(org.olat.core.commons.services.thumbnail.FinalSize)

Aggregations

FinalSize (org.olat.core.commons.services.thumbnail.FinalSize)14 File (java.io.File)8 Size (org.olat.core.commons.services.image.Size)6 CannotGenerateThumbnailException (org.olat.core.commons.services.thumbnail.CannotGenerateThumbnailException)4 LocalFileImpl (org.olat.core.util.vfs.LocalFileImpl)4 BufferedImage (java.awt.image.BufferedImage)2 BufferedReader (java.io.BufferedReader)2 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 InputStreamReader (java.io.InputStreamReader)2 RandomAccessFile (java.io.RandomAccessFile)2 VFSLeaf (org.olat.core.util.vfs.VFSLeaf)2