com.day.cq.wcm.foundation
Class ImageHelper

java.lang.Object
  extended by com.day.cq.wcm.foundation.ImageHelper

public class ImageHelper
extends Object

ImageHelper...


Constructor Summary
ImageHelper()
           
 
Method Summary
static com.day.image.Layer createLayer(Item item)
          Creates a layer of the given item.
static com.day.image.Layer createLayer(Node node, String imageName, String refName)
          Creates a layer either by the node addressed by imageName or the referenced image addressed by the refName property.
static com.day.image.Layer createLayer(org.apache.sling.api.resource.Resource resource)
          Creates a layer from the given resource.
static com.day.image.Layer createLayer(Session session, String path)
          Creates a layer of the given item addressed by the path.
static Rectangle getCropRect(String rectCSV, String path)
          Parses a CSV string of the form "x1,y1,x2,y2" and returns the respective rectangle.
static Color parseColor(String s)
          Converts a String to an integer and returns the specified Color.
static Color parseColor(String s, int alpha)
          Converts a String to an integer and returns the specified Color.
static int parseFontStyle(String... styles)
          Parses the font style from the given string(s).
static com.day.image.Layer resize(com.day.image.Layer layer, Dimension d, Dimension min, Dimension max)
          Resizes the given layer according to the given dimensions.
static Node saveLayer(com.day.image.Layer layer, String type, double quality, Node parent, String filename, boolean replace)
          Saves the layer as nt:file below the given node.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ImageHelper

public ImageHelper()
Method Detail

getCropRect

public static Rectangle getCropRect(String rectCSV,
                                    String path)
Parses a CSV string of the form "x1,y1,x2,y2" and returns the respective rectangle. if the string could not be parsed, null is returned.

Parameters:
rectCSV - the rectangle coordinates
path - optional path for debugging
Returns:
a rectangle or null

createLayer

public static com.day.image.Layer createLayer(Node node,
                                              String imageName,
                                              String refName)
                                       throws RepositoryException,
                                              IOException
Creates a layer either by the node addressed by imageName or the referenced image addressed by the refName property.

Parameters:
node - the current node
imageName - the name of the image node
refName - the name of the reference property
Returns:
a layer or null
Throws:
RepositoryException - if a repository error occurs
IOException - if a I/O error occurs

createLayer

public static com.day.image.Layer createLayer(Session session,
                                              String path)
                                       throws RepositoryException,
                                              IOException
Creates a layer of the given item addressed by the path. the item can be a binary property, a nt:file node or a nt:resource node.

Parameters:
session - to use for retrieving the item
path - to the item
Returns:
a layer or null
Throws:
RepositoryException - if a repository error occurs
IOException - if a I/O error occurs

createLayer

public static com.day.image.Layer createLayer(org.apache.sling.api.resource.Resource resource)
Creates a layer from the given resource. If the resource is not adaptable to InputStream null is returned.

Parameters:
resource - resource
Returns:
layer or null

createLayer

public static com.day.image.Layer createLayer(Item item)
                                       throws RepositoryException,
                                              IOException
Creates a layer of the given item. the item can be a binary property, a nt:file node or a nt:resource node.

Parameters:
item - the item
Returns:
a layer or null
Throws:
RepositoryException - if a repository error occurs
IOException - if a I/O error occurs

parseFontStyle

public static int parseFontStyle(String... styles)
Parses the font style from the given string(s). the styles can be combined by by separating them by spaces, comas or pipes, or specifying them multiple.
style string return
bold AbstractFont.BOLD
italic AbstractFont.ITALIC
underline AbstractFont.UNDERLINE
strikeout AbstractFont.STRIKEOUT
all otherAbstractFont.PLAIN

Parameters:
styles - the styles
Returns:
style constant

resize

public static com.day.image.Layer resize(com.day.image.Layer layer,
                                         Dimension d,
                                         Dimension min,
                                         Dimension max)
Resizes the given layer according to the given dimensions. If both width and height are defined a non-proportional resizing is done. if one of the dimensions is missing or 0, a proportional resizing performed. the non-empty dimensions are trimmed to respect the minimal and maximal dimension constraints. for proportional resizing the constraints apply to the recalculated dimensions. when no dimensions are specified, the layer is resized to fit into the constraint dimensions accordingly. if the constraints cannot be applied to the dimensions, eg if no values can be found that match the constraints, the layer is left untouched. In any case, if no resizing is performed, null is returned. Examples: | lW | lH | dW | dH | minW | maxW | minH | maxH | w | h | comment | | | | 400 | 200 | 0 | 0 | 0 | 0 | 400 | 200 | no resizing | | | | 400 | 200 | 0 | 200 | 0 | 0 | 200 | 100 | 200 max width applies | | 120 | 80 | 0 | 0 | 100 | 400 | 50 | 100 | 120 | 80 | within bounds | | 120 | 80 | 0 | 0 | 0 | 100 | 50 | 100 | 100 | 60 | 100 max width applies | | 400 | 100 | 300 | 0 | 0 | 0 | 0 | 0 | 300 | 75 | resize proportional | | 400 | 100 | 300 | 0 | 0 | 0 | 0 | 50 | 200 | 50 | max height applies |

Parameters:
layer - layer to resize.
d - dimension
min - minimal dimension constraints
max - maximal dimension constraints
Returns:
the resized layer or null if untouched.

parseColor

public static Color parseColor(String s)
Converts a String to an integer and returns the specified Color. This method handles string formats that are used to represent octal and hexadecimal numbers. If the string cannot be converted an transparent black is returned.

Parameters:
s - a String that represents an RGBA color as a 32-bit integer
Returns:
the new Color object.
See Also:
Integer.decode(java.lang.String)

parseColor

public static Color parseColor(String s,
                               int alpha)
Converts a String to an integer and returns the specified Color. This method handles string formats that are used to represent octal and hexadecimal numbers. If the string cannot be converted an transparent black is returned.

Parameters:
s - a String that represents an RGB color as a 24-bit integer
alpha - override the alpha setting
Returns:
the new Color object.
See Also:
Integer.decode(java.lang.String)

saveLayer

public static Node saveLayer(com.day.image.Layer layer,
                             String type,
                             double quality,
                             Node parent,
                             String filename,
                             boolean replace)
                      throws RepositoryException,
                             IOException
Saves the layer as nt:file below the given node.

Parameters:
layer - the layer to save
type - image type. eg "image/png"
quality - image quality. eg 1.0
parent - parent node
filename - file name
replace - if true existing node are replaced rather than updated.
Returns:
the newly created and saved file node
Throws:
RepositoryException - if a repository error occurrs
IOException - if an I/O error occurrs


Copyright © 2009 Day Management AG. All Rights Reserved.