1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portal.tools;
16  
17  import com.liferay.portal.image.ImageProcessorImpl;
18  import com.liferay.portal.kernel.image.ImageBag;
19  import com.liferay.portal.kernel.util.GetterUtil;
20  
21  import java.awt.image.RenderedImage;
22  
23  import java.io.File;
24  
25  import javax.imageio.ImageIO;
26  
27  /**
28   * <a href="ThumbnailBuilder.java.html"><b><i>View Source</i></b></a>
29   *
30   * @author Brian Wing Shun Chan
31   */
32  public class ThumbnailBuilder {
33  
34      public static void main(String[] args) {
35          File originalFile = new File(
36              System.getProperty("thumbnail.original.file"));
37          File thumbnailFile = new File(
38              System.getProperty("thumbnail.thumbnail.file"));
39          int height = GetterUtil.getInteger(
40              System.getProperty("thumbnail.height"));
41          int width = GetterUtil.getInteger(
42              System.getProperty("thumbnail.width"));
43          boolean overwrite = GetterUtil.getBoolean(
44              System.getProperty("thumbnail.overwrite"));
45  
46          new ThumbnailBuilder(
47              originalFile, thumbnailFile, height, width, overwrite);
48      }
49  
50      public ThumbnailBuilder(
51          File originalFile, File thumbnailFile, int height, int width,
52          boolean overwrite) {
53  
54          try {
55              if (!originalFile.exists()) {
56                  return;
57              }
58  
59              if (!overwrite) {
60                  if (thumbnailFile.lastModified() >
61                          originalFile.lastModified()) {
62  
63                      return;
64                  }
65              }
66  
67              ImageBag imageBag = _imageProcessorUtil.read(originalFile);
68  
69              RenderedImage thumbnail = _imageProcessorUtil.scale(
70                  imageBag.getRenderedImage(), height, width);
71  
72              ImageIO.write(thumbnail, imageBag.getType(), thumbnailFile);
73          }
74          catch (Exception e) {
75              e.printStackTrace();
76          }
77      }
78  
79      private static ImageProcessorImpl _imageProcessorUtil =
80          ImageProcessorImpl.getInstance();
81  
82  }