18 package org.turro.elephant.impl.util;
20 import java.awt.Graphics2D;
21 import java.awt.RenderingHints;
22 import java.awt.geom.AffineTransform;
23 import java.awt.image.BufferedImage;
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;
37 HttpServletRequest request;
38 ServletContext context;
40 public Thumbs(HttpServletRequest request) {
41 this.request = request;
42 this.context = request.getSession().getServletContext();
46 public String
getThumb(String file,
int maxDim, String rotate) {
47 String contextPath = request.getContextPath(),
50 if(contextPath.length() > 1 && file.startsWith(contextPath)) {
51 fileNoContext = file.substring(contextPath.length());
53 String filePath = fileNoContext,
54 thumbPath = insertThumb(fileNoContext);
56 if(filePath.indexOf(
"_thumb") > -1)
return file;
60 return insertThumb(file);
63 public void createThumb(String rotate, String file, String thumb,
int maxDim) {
65 String filePath = context.getRealPath(file),
66 thumbPath = context.getRealPath(thumb);
70 File fi =
new File(filePath);
73 if(maxDim <= 0) maxDim = 200;
75 if(needsRecreateThumb(thumbPath, maxDim)) {
77 BufferedImage inImage = ImageIO.read(fi);
80 double scale = (double)maxDim/(
double)inImage.getWidth(
null);
85 if(inImage.getWidth() <= maxDim) {
91 int scaledW = (int)(scale*inImage.getWidth(
null));
92 int scaledH = (int)(scale*inImage.getHeight(
null));
96 BufferedImage outImage;
97 if(
":L".equals(rotate) ||
":R".equals(rotate))
98 outImage =
new BufferedImage(
99 scaledH, scaledW, BufferedImage.TYPE_INT_RGB);
101 outImage =
new BufferedImage(
102 scaledW, scaledH, BufferedImage.TYPE_INT_RGB);
105 AffineTransform tx =
new AffineTransform();
111 tx.scale(scale, scale);
114 if(
":L".equals(rotate)) {
116 tx.translate(inImage.getHeight(), 0);
117 tx.rotate(Math.PI/2);
119 else if(
":R".equals(rotate)) {
121 tx.translate(0, inImage.getWidth());
122 tx.rotate(-(Math.PI/2));
126 Graphics2D g2d = outImage.createGraphics();
127 g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
128 g2d.drawImage(inImage, tx,
null);
131 File fo =
new File(thumbPath);
132 ImageIO.write(outImage,
"jpg", fo);
137 }
catch(Exception ex) {
142 private String insertThumb(String file) {
143 int p = file.lastIndexOf(
'.');
145 return file.substring(0, p) +
"_thumb.jpg";
147 return file +
"_thumb";
150 private boolean needsRecreateThumb(String thumbPath,
int maxDim)
throws java.io.IOException {
151 File thumb =
new File(thumbPath);
153 BufferedImage thumbImage = ImageIO.read(thumb);
155 thumbImage.getWidth() <= maxDim &&
156 thumbImage.getHeight() <= maxDim;
157 boolean hasCoincidence =
false;
160 return !isSmaller && !hasCoincidence;
static String logMsg(String msg)
String getThumb(String file, int maxDim, String rotate)
void createThumb(String rotate, String file, String thumb, int maxDim)
Thumbs(HttpServletRequest request)