1 package info.magnolia.module.admininterface.dialogpages; 2 3 import info.magnolia.cms.core.HierarchyManager; 4 import info.magnolia.cms.core.NodeData; 5 import info.magnolia.cms.security.SessionAccessControl; 6 7 import java.awt.Graphics2D ; 8 import java.awt.Image ; 9 import java.awt.image.BufferedImage ; 10 import java.io.BufferedOutputStream ; 11 import java.io.IOException ; 12 import java.io.InputStream ; 13 14 import javax.imageio.ImageIO ; 15 import javax.servlet.ServletOutputStream ; 16 import javax.servlet.http.HttpServlet ; 17 import javax.servlet.http.HttpServletRequest ; 18 import javax.servlet.http.HttpServletResponse ; 19 20 import org.apache.log4j.Logger; 21 22 import com.sun.image.codec.jpeg.JPEGCodec; 23 import com.sun.image.codec.jpeg.JPEGEncodeParam; 24 import com.sun.image.codec.jpeg.JPEGImageEncoder; 25 26 27 31 public class FileThumbnailDialogPage extends HttpServlet { 32 33 36 private static final long serialVersionUID = 222L; 37 38 41 private static Logger log = Logger.getLogger(FileThumbnailDialogPage.class); 42 43 46 public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException { 47 response.setContentType("image/jpeg"); 49 String src = request.getParameter("src"); String size = request.getParameter("size"); 52 HierarchyManager hm = SessionAccessControl.getHierarchyManager(request); 53 54 InputStream in = null; 55 56 NodeData data; 57 try { 58 data = hm.getNodeData(src); 59 in = data.getValue().getStream(); 60 } 61 catch (Exception e) { 62 log.error(e.getMessage(), e); 63 return; 64 } 65 66 Image image = ImageIO.read(in); 67 68 int thumbHeight; 69 int thumbWidth; 70 if (size != null) { 71 thumbHeight = image.getHeight(null); 72 thumbWidth = image.getWidth(null); 73 } 74 else { 75 thumbWidth = 150; 76 int w = image.getWidth(null); 77 int h = image.getHeight(null); 78 if (w == 0) { 79 w = 1; 80 } 81 if (h == 0) { 82 h = 1; 83 } 84 85 if (w > thumbWidth) { 86 thumbHeight = thumbWidth * h / w; 87 } 88 else { 89 thumbWidth = w; 90 thumbHeight = h; 91 } 92 93 if (thumbHeight > 120) { 94 thumbHeight = 100; 95 thumbWidth = thumbHeight * w / h; 96 } 97 98 } 99 100 BufferedImage thumbImage = new BufferedImage (thumbWidth, thumbHeight, BufferedImage.TYPE_INT_RGB); 102 Graphics2D graphics2D = thumbImage.createGraphics(); 103 graphics2D.drawImage(image, 0, 0, thumbWidth, thumbHeight, null); 105 106 ServletOutputStream sout = response.getOutputStream(); 107 BufferedOutputStream output = new BufferedOutputStream (sout); 108 JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(output); 109 JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(thumbImage); 110 param.setQuality(0.8f, false); 111 112 encoder.setJPEGEncodeParam(param); 113 encoder.encode(thumbImage); 114 115 } 116 117 } 118 | Popular Tags |