BrightSide Workbench Full Report + Source Code
Thumbs.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2011 Lluis TurrĂ³ Cutiller <http://www.turro.org/>
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Affero General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU Affero General Public License for more details.
14  *
15  * You should have received a copy of the GNU Affero General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 package org.turro.elephant.impl.util;
19 
20 import java.awt.Graphics2D;
21 import java.awt.RenderingHints;
22 import java.awt.geom.AffineTransform;
23 import java.awt.image.BufferedImage;
24 import java.io.File;
25 import java.util.logging.Level;
26 import java.util.logging.Logger;
27 import javax.imageio.ImageIO;
28 import javax.servlet.ServletContext;
29 import javax.servlet.http.HttpServletRequest;
30 import org.turro.elephant.context.ElephantContext;
31 
36 public class Thumbs {
37  HttpServletRequest request;
38  ServletContext context;
39 
40  public Thumbs(HttpServletRequest request) {
41  this.request = request;
42  this.context = request.getSession().getServletContext();
43  }
44 
45  // rotate = R:right L:left N:none
46  public String getThumb(String file, int maxDim, String rotate) {
47  String contextPath = request.getContextPath(),
48  fileNoContext = file;
49 
50  if(contextPath.length() > 1 && file.startsWith(contextPath)) {
51  fileNoContext = file.substring(contextPath.length());
52  }
53  String filePath = fileNoContext,
54  thumbPath = insertThumb(fileNoContext);
55 
56  if(filePath.indexOf("_thumb") > -1) return file;
57 
58  createThumb(rotate, filePath, thumbPath, maxDim);
59 
60  return insertThumb(file);
61  }
62 
63  public void createThumb(String rotate, String file, String thumb, int maxDim) {
64 
65  String filePath = context.getRealPath(file),
66  thumbPath = context.getRealPath(thumb);
67 
68  try {
69 
70  File fi = new File(filePath);
71 
72  // maximum dimension
73  if(maxDim <= 0) maxDim = 200;
74 
75  if(needsRecreateThumb(thumbPath, maxDim)) {
76 
77  BufferedImage inImage = ImageIO.read(fi);
78 
79  // Determine the scale.
80  double scale = (double)maxDim/(double)inImage.getWidth(null);
81 // if (inImage.getHeight(null) > inImage.getWidth(null)) {
82 // scale = (double)maxDim/(double)inImage.getHeight(null);
83 // }
84 
85  if(inImage.getWidth() <= maxDim/* && inImage.getHeight() <= maxDim*/) {
86  scale = 1;
87  }
88 
89  // Determine size of new image.
90  //One of them should equal maxDim.
91  int scaledW = (int)(scale*inImage.getWidth(null));
92  int scaledH = (int)(scale*inImage.getHeight(null));
93 
94  // Create an image buffer in
95  //which to paint on.
96  BufferedImage outImage;
97  if(":L".equals(rotate) || ":R".equals(rotate))
98  outImage = new BufferedImage(
99  scaledH, scaledW, BufferedImage.TYPE_INT_RGB);
100  else
101  outImage = new BufferedImage(
102  scaledW, scaledH, BufferedImage.TYPE_INT_RGB);
103 
104  // Set the scale.
105  AffineTransform tx = new AffineTransform();
106 
107  // If the image is smaller than
108  //the desired image size,
109  // don't bother scaling.
110  if (scale < 1.0d) {
111  tx.scale(scale, scale);
112  }
113 
114  if(":L".equals(rotate)) {
115  // 90 to left
116  tx.translate(inImage.getHeight(), 0);
117  tx.rotate(Math.PI/2);
118  }
119  else if(":R".equals(rotate)) {
120  // 90 to right
121  tx.translate(0, inImage.getWidth());
122  tx.rotate(-(Math.PI/2));
123  }
124 
125  // Paint image.
126  Graphics2D g2d = outImage.createGraphics();
127  g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
128  g2d.drawImage(inImage, tx, null);
129  g2d.dispose();
130 
131  File fo = new File(thumbPath);
132  ImageIO.write(outImage, "jpg", fo);
133 
134 
135  }
136 
137  } catch(Exception ex) {
138  Logger.getLogger(Thumbs.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
139  }
140  }
141 
142  private String insertThumb(String file) {
143  int p = file.lastIndexOf('.');
144  if(p > -1)
145  return file.substring(0, p) + "_thumb.jpg";
146  else
147  return file + "_thumb";
148  }
149 
150  private boolean needsRecreateThumb(String thumbPath, int maxDim) throws java.io.IOException {
151  File thumb = new File(thumbPath);
152  if(thumb.exists()) {
153  BufferedImage thumbImage = ImageIO.read(thumb);
154  boolean isSmaller =
155  thumbImage.getWidth() <= maxDim &&
156  thumbImage.getHeight() <= maxDim;
157  boolean hasCoincidence = false;
158 // thumbImage.getWidth() == maxDim ||
159 // thumbImage.getHeight() == maxDim;
160  return !isSmaller && !hasCoincidence;
161  }
162  else
163  return true;
164  }
165 }
String getThumb(String file, int maxDim, String rotate)
Definition: Thumbs.java:46
void createThumb(String rotate, String file, String thumb, int maxDim)
Definition: Thumbs.java:63
Thumbs(HttpServletRequest request)
Definition: Thumbs.java:40