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 javax.imageio.ImageIO;
026    
027    /**
028     * @author Brian Wing Shun Chan
029     */
030    public class ThumbnailBuilder {
031    
032            public static void main(String[] args) {
033                    File originalFile = new File(
034                            System.getProperty("thumbnail.original.file"));
035                    File thumbnailFile = new File(
036                            System.getProperty("thumbnail.thumbnail.file"));
037                    int height = GetterUtil.getInteger(
038                            System.getProperty("thumbnail.height"));
039                    int width = GetterUtil.getInteger(
040                            System.getProperty("thumbnail.width"));
041                    boolean overwrite = GetterUtil.getBoolean(
042                            System.getProperty("thumbnail.overwrite"));
043    
044                    new ThumbnailBuilder(
045                            originalFile, thumbnailFile, height, width, overwrite);
046            }
047    
048            public ThumbnailBuilder(
049                    File originalFile, File thumbnailFile, int height, int width,
050                    boolean overwrite) {
051    
052                    try {
053                            if (!originalFile.exists()) {
054                                    return;
055                            }
056    
057                            if (!overwrite) {
058                                    if (thumbnailFile.lastModified() >
059                                                    originalFile.lastModified()) {
060    
061                                            return;
062                                    }
063                            }
064    
065                            ImageBag imageBag = _imageProcessorUtil.read(originalFile);
066    
067                            RenderedImage thumbnail = _imageProcessorUtil.scale(
068                                    imageBag.getRenderedImage(), height, width);
069    
070                            ImageIO.write(thumbnail, imageBag.getType(), thumbnailFile);
071                    }
072                    catch (Exception e) {
073                            e.printStackTrace();
074                    }
075            }
076    
077            private static ImageProcessorImpl _imageProcessorUtil =
078                    ImageProcessorImpl.getInstance();
079    
080    }