Class ImageHelper

java.lang.Object
com.day.cq.commons.ImageHelper
Direct Known Subclasses:
ImageHelper

public class ImageHelper extends Object
ImageHelper...
  • Constructor Details

    • ImageHelper

      public ImageHelper()
  • Method Details

    • getCropRect

      public static Rectangle getCropRect(String rectCSV, String path)
      d 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. The method can deal with an aspect ratio that is appended by "/" (e.g. "x1,y1,x2,y2/ratioX,ratioY".
      Parameters:
      rectCSV - the rectangle coordinates
      path - optional path for debugging
      Returns:
      a rectangle or null
    • createLayer

      public static 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 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 Layer createLayer(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 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 Layer resize(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:
    • 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:
    • saveLayer

      public static Node saveLayer(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
    • getTypeFromExtension

      public static String getTypeFromExtension(String ext)
      Returns the image type for the given extension. currently there are only "png", "gif", "jpeg" and "jpg" supported.
      Parameters:
      ext - the extension
      Returns:
      the image type or null.
    • getExtensionFromType

      public static String getExtensionFromType(String type)
      Returns the extension from the given image type. currently there are only "png", "gif", "jpeg" and "jpg" supported.
      Parameters:
      type - the mime type
      Returns:
      the extension or null.