1 23 24 package org.infoglue.cms.util.graphics; 25 26 import java.awt.Color ; 27 import java.awt.Graphics2D ; 28 import java.awt.Image ; 29 import java.awt.RenderingHints ; 30 import java.awt.image.BufferedImage ; 31 import java.io.BufferedReader ; 32 import java.io.File ; 33 import java.io.InputStreamReader ; 34 35 import org.apache.log4j.Logger; 36 import org.infoglue.cms.util.CmsPropertyHandler; 37 38 public class ThumbnailGenerator 39 { 40 private final static Logger logger = Logger.getLogger(ThumbnailGenerator.class.getName()); 41 42 public ThumbnailGenerator() 43 { 44 } 45 46 private void execCmd(String command) throws Exception 47 { 48 logger.error(command); 49 String line; 50 Process p = Runtime.getRuntime().exec(command); 51 BufferedReader input = new BufferedReader (new InputStreamReader (p.getInputStream())); 52 53 while ((line = input.readLine()) != null) 54 { 55 logger.error(line); 56 } 57 input.close(); 58 } 59 60 public void transform(String originalFile, String thumbnailFile, int thumbWidth, int thumbHeight, int quality) throws Exception 61 { 62 Image image = javax.imageio.ImageIO.read(new File (originalFile)); 63 64 double thumbRatio = (double)thumbWidth / (double)thumbHeight; 65 int imageWidth = image.getWidth(null); 66 int imageHeight = image.getHeight(null); 67 double imageRatio = (double)imageWidth / (double)imageHeight; 68 if (thumbRatio < imageRatio) 69 { 70 thumbHeight = (int)(thumbWidth / imageRatio); 71 } 72 else 73 { 74 thumbWidth = (int)(thumbHeight * imageRatio); 75 } 76 77 if(imageWidth < thumbWidth && imageHeight < thumbHeight) 78 { 79 thumbWidth = imageWidth; 80 thumbHeight = imageHeight; 81 } 82 else if(imageWidth < thumbWidth) 83 thumbWidth = imageWidth; 84 else if(imageHeight < thumbHeight) 85 thumbHeight = imageHeight; 86 87 if(thumbWidth < 1) 88 thumbWidth = 1; 89 if(thumbHeight < 1) 90 thumbHeight = 1; 91 92 if(CmsPropertyHandler.getExternalThumbnailGeneration() != null && !CmsPropertyHandler.getExternalThumbnailGeneration().equalsIgnoreCase("") && !CmsPropertyHandler.getExternalThumbnailGeneration().equalsIgnoreCase("@externalThumbnailGeneration@")) 93 { 94 String [] args = new String [5]; 95 96 args[0] = CmsPropertyHandler.getExternalThumbnailGeneration(); 97 args[1] = "-resize"; 98 args[2] = String.valueOf(thumbWidth) + "x" + String.valueOf(thumbHeight); 99 args[3] = originalFile; 100 args[4] = thumbnailFile; 101 102 try 103 { 104 Process p = Runtime.getRuntime().exec(args); 105 p.waitFor(); 106 } 107 catch(InterruptedException e) 108 { 109 new Exception ("Error resizing image for thumbnail", e); 110 } 111 } 112 else 113 { 114 BufferedImage thumbImage = new BufferedImage (thumbWidth, thumbHeight, BufferedImage.TYPE_INT_RGB); 115 Graphics2D graphics2D = thumbImage.createGraphics(); 116 graphics2D.setBackground(Color.WHITE); 117 graphics2D.setPaint(Color.WHITE); 118 graphics2D.fillRect(0, 0, thumbWidth, thumbHeight); 119 graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); 120 graphics2D.drawImage(image, 0, 0, thumbWidth, thumbHeight, null); 121 122 javax.imageio.ImageIO.write(thumbImage, "JPG", new File (thumbnailFile)); 123 } 124 } 125 } 126 | Popular Tags |