1 13 package info.magnolia.cms.taglibs.util; 14 15 import info.magnolia.cms.beans.config.ContentRepository; 16 import info.magnolia.cms.core.Content; 17 import info.magnolia.cms.core.HierarchyManager; 18 import info.magnolia.cms.core.ItemType; 19 import info.magnolia.cms.core.NodeData; 20 import info.magnolia.cms.util.Resource; 21 import info.magnolia.context.MgnlContext; 22 23 import java.awt.Graphics2D ; 24 import java.awt.Image ; 25 import java.awt.image.BufferedImage ; 26 import java.io.File ; 27 import java.io.FileInputStream ; 28 import java.io.FileNotFoundException ; 29 import java.io.IOException ; 30 import java.io.InputStream ; 31 32 import javax.imageio.ImageIO ; 33 import javax.jcr.AccessDeniedException; 34 import javax.jcr.PathNotFoundException; 35 import javax.jcr.RepositoryException; 36 import javax.servlet.http.HttpServletRequest ; 37 import javax.servlet.jsp.JspException ; 38 import javax.servlet.jsp.JspWriter ; 39 import javax.servlet.jsp.PageContext ; 40 import javax.servlet.jsp.tagext.SimpleTagSupport ; 41 42 import org.apache.log4j.Logger; 43 44 45 66 public class ScaleImageTag extends SimpleTagSupport { 67 68 71 private static final String TEMP_IMAGE_NAME = "tmp-img"; 72 73 76 private static final String PROPERTIES_NODE_NAME = "_properties"; 77 78 81 private static final String PROPERTIES_FILENAME = "fileName"; 82 83 86 private static final String PROPERTIES_SIZE = "size"; 87 88 91 private static final String PROPERTIES_EXTENSION = "extension"; 92 93 96 private static final String PROPERTIES_EXTENSION_VALUE = "PNG"; 97 98 101 private static final String PROPERTIES_CONTENTTYPE = "contentType"; 102 103 106 private static final String PROPERTIES_CONTENTTYPE_VALUE = "image/png"; 107 108 111 private int maxHeight = 0; 112 113 116 private int maxWidth = 0; 117 118 121 private String imageContentNodeName; 122 123 126 private String parentContentNodeName; 127 128 131 private String parentNodeDataName; 132 133 136 private static Logger log = Logger.getLogger(ScaleImageTag.class); 137 138 142 public void setMaxHeight(int maxHeight) { 143 this.maxHeight = maxHeight; 144 } 145 146 150 public void setMaxWidth(int maxWidth) { 151 this.maxWidth = maxWidth; 152 } 153 154 158 public void setParentContentNodeName(String parentContentNodeName) { 159 this.parentContentNodeName = parentContentNodeName; 160 } 161 162 166 public void setParentNodeDataName(String parentNodeDataName) { 167 this.parentNodeDataName = parentNodeDataName; 168 } 169 170 174 public void setImageContentNodeName(String imageContentNodeName) { 175 this.imageContentNodeName = imageContentNodeName; 176 } 177 178 181 public void doTag() throws JspException { 182 HttpServletRequest req = (HttpServletRequest ) ((PageContext ) this.getJspContext()).getRequest(); 184 Content parentContentNode; 185 Content imageContentNode; 186 JspWriter out = this.getJspContext().getOut(); 187 188 try { 189 190 if ((this.parentContentNodeName == null) || (this.parentContentNodeName.equals(""))) { 192 parentContentNode = Resource.getLocalContentNode(req); 193 } 194 else { 195 HierarchyManager hm = MgnlContext.getHierarchyManager(ContentRepository.WEBSITE); 196 if (this.parentContentNodeName.startsWith("/")) { 199 parentContentNode = hm.getContent(this.parentContentNodeName); 200 } 201 else { 202 String handle = Resource.getLocalContentNode(req).getHandle(); 203 parentContentNode = hm.getContent(handle + "/" + this.parentContentNodeName); 204 } 205 } 206 207 if (parentContentNode.hasContent(this.imageContentNodeName)) { 209 imageContentNode = parentContentNode.getContent(this.imageContentNodeName); 210 } 211 else { 212 imageContentNode = parentContentNode.createContent(this.imageContentNodeName, ItemType.CONTENTNODE); 214 parentContentNode.save(); 215 } 216 if (!imageContentNode.hasNodeData(this.parentNodeDataName)) { 218 this.createImageNodeData(parentContentNode, imageContentNode); 219 } 220 out.write(imageContentNode.getHandle()); 222 223 } 224 catch (PathNotFoundException e) { 225 log.error("PathNotFoundException occured in ScaleImage tag: " + e.getMessage(), e); 226 } 227 catch (AccessDeniedException e) { 228 log.error("AccessDeniedException occured in ScaleImage tag: " + e.getMessage(), e); 229 } 230 catch (RepositoryException e) { 231 log.error("RepositoryException occured in ScaleImage tag: " + e.getMessage(), e); 232 } 233 catch (FileNotFoundException e) { 234 log.error("FileNotFoundException occured in ScaleImage tag: " + e.getMessage(), e); 235 } 236 catch (IOException e) { 237 log.error("IOException occured in ScaleImage tag: " + e.getMessage(), e); 238 } 239 this.cleanUp(); 240 } 241 242 245 public void cleanUp() { 246 this.parentNodeDataName = null; 247 this.imageContentNodeName = null; 248 this.maxWidth = 0; 249 this.maxHeight = 0; 250 } 251 252 257 private void createImageNodeData(Content parentContentNode, Content imageContentNode) throws PathNotFoundException, 258 RepositoryException, IOException { 259 NodeData newNodeData = imageContentNode.createNodeData(this.parentNodeDataName); 261 InputStream oriImgStr = parentContentNode.getNodeData(this.parentNodeDataName).getStream(); 263 BufferedImage oriImgBuff = ImageIO.read(oriImgStr); 264 oriImgStr.close(); 265 File newImgFile = this.scaleImage(oriImgBuff); 267 long fileSize = newImgFile.length(); 268 InputStream newImgStr = new FileInputStream (newImgFile); 269 newNodeData.setValue(newImgStr); 270 newImgStr.close(); 271 newImgFile.delete(); 272 Content newPropsNode = imageContentNode.createContent( 274 this.parentNodeDataName + PROPERTIES_NODE_NAME, 275 ItemType.CONTENTNODE); 276 NodeData size = newPropsNode.createNodeData(PROPERTIES_SIZE); 277 size.setValue(fileSize); 278 NodeData extension = newPropsNode.createNodeData(PROPERTIES_EXTENSION); 279 extension.setValue(PROPERTIES_EXTENSION_VALUE); 280 NodeData contentType = newPropsNode.createNodeData(PROPERTIES_CONTENTTYPE); 281 contentType.setValue(PROPERTIES_CONTENTTYPE_VALUE); 282 NodeData fileName = newPropsNode.createNodeData(PROPERTIES_FILENAME); 283 fileName.setValue(this.imageContentNodeName); 284 imageContentNode.save(); newPropsNode.save(); 287 } 288 289 294 private File scaleImage(BufferedImage oriImgBuff) throws IOException { 295 int oriWidth = oriImgBuff.getWidth(); 297 int oriHeight = oriImgBuff.getHeight(); 298 double scaleFactor = this.scaleFactor(oriWidth, oriHeight); 300 int newWidth = new Double (oriWidth * scaleFactor).intValue(); 302 int newHeight = new Double (oriHeight * scaleFactor).intValue(); 303 Image newImg = oriImgBuff.getScaledInstance(newWidth, newHeight, Image.SCALE_AREA_AVERAGING); 305 BufferedImage newImgBuff = new BufferedImage ( 306 newImg.getWidth(null), 307 newImg.getHeight(null), 308 BufferedImage.TYPE_INT_RGB); 309 Graphics2D g = newImgBuff.createGraphics(); 310 g.drawImage(newImg, 0, 0, null); 311 g.dispose(); 312 File newImgFile = File.createTempFile(TEMP_IMAGE_NAME, PROPERTIES_EXTENSION_VALUE); 314 315 ImageIO.write(newImgBuff, PROPERTIES_EXTENSION_VALUE, newImgFile); 316 return newImgFile; 318 } 319 320 326 private double scaleFactor(int width, int height) { 327 double scaleFactor; 328 if (this.maxWidth <= 0 && this.maxHeight <= 0) { 329 scaleFactor = 1; 331 } 332 else if (this.maxWidth <= 0) { 333 scaleFactor = this.maxHeight / height; 335 } 336 else if (this.maxHeight <= 0) { 337 scaleFactor = (double) this.maxWidth / (double) width; 339 } 340 else { 341 double scaleFactorWidth = (double) this.maxWidth / (double) width; 343 double scaleFactorHeight = (double) this.maxHeight / (double) height; 344 scaleFactor = Math.min(scaleFactorWidth, scaleFactorHeight); 345 } 346 return scaleFactor; 347 } 348 } 349 | Popular Tags |