001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.tools;
016    
017    import com.liferay.portal.image.ImageToolImpl;
018    import com.liferay.portal.kernel.image.ImageBag;
019    import com.liferay.portal.kernel.image.ImageTool;
020    import com.liferay.portal.kernel.util.GetterUtil;
021    
022    import java.awt.image.RenderedImage;
023    
024    import java.io.File;
025    
026    import java.util.Map;
027    
028    import javax.imageio.ImageIO;
029    
030    /**
031     * @author Brian Wing Shun Chan
032     */
033    public class ThumbnailBuilder {
034    
035            public static void main(String[] args) throws Exception {
036                    Map<String, String> arguments = ArgumentsUtil.parseArguments(args);
037    
038                    File originalFile = new File(arguments.get("thumbnail.original.file"));
039                    File thumbnailFile = new File(
040                            arguments.get("thumbnail.thumbnail.file"));
041                    int height = GetterUtil.getInteger(arguments.get("thumbnail.height"));
042                    int width = GetterUtil.getInteger(arguments.get("thumbnail.width"));
043                    boolean overwrite = GetterUtil.getBoolean(
044                            arguments.get("thumbnail.overwrite"));
045    
046                    try {
047                            new ThumbnailBuilder(
048                                    originalFile, thumbnailFile, height, width, overwrite);
049                    }
050                    catch (Exception e) {
051                            ArgumentsUtil.processMainException(arguments, e);
052                    }
053            }
054    
055            public ThumbnailBuilder(
056                            File originalFile, File thumbnailFile, int height, int width,
057                            boolean overwrite)
058                    throws Exception {
059    
060                    if (!originalFile.exists()) {
061                            return;
062                    }
063    
064                    if (!overwrite) {
065                            if (thumbnailFile.lastModified() >
066                                            originalFile.lastModified()) {
067    
068                                    return;
069                            }
070                    }
071    
072                    ImageBag imageBag = _imageToolUtil.read(originalFile);
073    
074                    RenderedImage renderedImage = _imageToolUtil.scale(
075                            imageBag.getRenderedImage(), height, width);
076    
077                    ImageIO.write(renderedImage, imageBag.getType(), thumbnailFile);
078            }
079    
080            private static final ImageTool _imageToolUtil = ImageToolImpl.getInstance();
081    
082    }