001    /**
002     * Copyright (c) 2000-2010 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 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    }