001    /**
002     * Copyright (c) 2000-2013 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) {
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                    new ThumbnailBuilder(
047                            originalFile, thumbnailFile, height, width, overwrite);
048            }
049    
050            public ThumbnailBuilder(
051                    File originalFile, File thumbnailFile, int height, int width,
052                    boolean overwrite) {
053    
054                    try {
055                            if (!originalFile.exists()) {
056                                    return;
057                            }
058    
059                            if (!overwrite) {
060                                    if (thumbnailFile.lastModified() >
061                                                    originalFile.lastModified()) {
062    
063                                            return;
064                                    }
065                            }
066    
067                            ImageBag imageBag = _imageToolUtil.read(originalFile);
068    
069                            RenderedImage renderedImage = _imageToolUtil.scale(
070                                    imageBag.getRenderedImage(), height, width);
071    
072                            ImageIO.write(renderedImage, imageBag.getType(), thumbnailFile);
073                    }
074                    catch (Exception e) {
075                            e.printStackTrace();
076                    }
077            }
078    
079            private static ImageTool _imageToolUtil = ImageToolImpl.getInstance();
080    
081    }