1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
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  }