001    /**
002     * Copyright (c) 2000-2011 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.ImageProcessorImpl;
018    import com.liferay.portal.kernel.image.ImageBag;
019    import com.liferay.portal.kernel.util.GetterUtil;
020    
021    import java.awt.image.RenderedImage;
022    
023    import java.io.File;
024    
025    import java.util.Map;
026    
027    import javax.imageio.ImageIO;
028    
029    /**
030     * @author Brian Wing Shun Chan
031     */
032    public class ThumbnailBuilder {
033    
034            public static void main(String[] args) {
035                    Map<String, String> arguments = ArgumentsUtil.parseArguments(args);
036    
037                    File originalFile = new File(arguments.get("thumbnail.original.file"));
038                    File thumbnailFile = new File(
039                            arguments.get("thumbnail.thumbnail.file"));
040                    int height = GetterUtil.getInteger(arguments.get("thumbnail.height"));
041                    int width = GetterUtil.getInteger(arguments.get("thumbnail.width"));
042                    boolean overwrite = GetterUtil.getBoolean(
043                            arguments.get("thumbnail.overwrite"));
044    
045                    new ThumbnailBuilder(
046                            originalFile, thumbnailFile, height, width, overwrite);
047            }
048    
049            public ThumbnailBuilder(
050                    File originalFile, File thumbnailFile, int height, int width,
051                    boolean overwrite) {
052    
053                    try {
054                            if (!originalFile.exists()) {
055                                    return;
056                            }
057    
058                            if (!overwrite) {
059                                    if (thumbnailFile.lastModified() >
060                                                    originalFile.lastModified()) {
061    
062                                            return;
063                                    }
064                            }
065    
066                            ImageBag imageBag = _imageProcessorUtil.read(originalFile);
067    
068                            RenderedImage renderedImage = _imageProcessorUtil.scale(
069                                    imageBag.getRenderedImage(), height, width);
070    
071                            ImageIO.write(renderedImage, imageBag.getType(), thumbnailFile);
072                    }
073                    catch (Exception e) {
074                            e.printStackTrace();
075                    }
076            }
077    
078            private static ImageProcessorImpl _imageProcessorUtil =
079                    ImageProcessorImpl.getInstance();
080    
081    }