001    /**
002     * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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    }