Package com.day.cq.commons
Class ImageResource
- java.lang.Object
-
- org.apache.sling.api.resource.ResourceWrapper
-
- com.day.cq.commons.DownloadResource
-
- com.day.cq.commons.ImageResource
-
- Direct Known Subclasses:
Image
public class ImageResource extends DownloadResource
Provides convenience methods for displaying images.
-
-
Field Summary
Fields Modifier and Type Field Description static java.lang.String
DEFAULT_IMAGE_PATH
the default image pathstatic java.lang.String
PN_ALT
name of the alt name propertystatic java.lang.String
PN_DEFAULT_IMAGE_PATH
name of the default image path propertystatic java.lang.String
PN_HEIGHT
name of the height propertystatic java.lang.String
PN_HTML_HEIGHT
name of the html height propertystatic java.lang.String
PN_HTML_WIDTH
name of the html width propertystatic java.lang.String
PN_IMAGE_CROP
name of the image crop propertystatic java.lang.String
PN_IMAGE_ROTATE
name of the image rotation propertystatic java.lang.String
PN_LINK_URL
name of the link URL propertystatic java.lang.String
PN_MAX_HEIGHT
name of the maximal height property.static java.lang.String
PN_MAX_WIDTH
name of the maximal width property.static java.lang.String
PN_MIN_HEIGHT
name of the minimal height property.static java.lang.String
PN_MIN_WIDTH
name of the minimal width property.static java.lang.String
PN_WIDTH
name of the width property-
Fields inherited from class com.day.cq.commons.DownloadResource
NN_FILE, PN_DESCRIPTION, PN_FILE_NAME, PN_REFERENCE, PN_TITLE
-
Fields inherited from interface org.apache.sling.api.resource.Resource
RESOURCE_TYPE_NON_EXISTING
-
-
Constructor Summary
Constructors Constructor Description ImageResource(Resource resource)
Creates a new image based on the given resource.ImageResource(Resource resource, java.lang.String imageName)
Creates a new image based on the given resource.
-
Method Summary
All Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description Layer
crop(Layer layer)
Crops the layer using the internal crop rectangle.void
draw(java.io.Writer w)
Writes this image as tag to the given writer by invokingdoDraw(PrintWriter)
ifcanDraw()
returnstrue
.java.lang.String
getAlt()
Returns the image alt name as defined by the "alt" or overridden bysetAlt(String)
.java.awt.Rectangle
getCropRect()
Returns the cropping rectangle as defined by the "imageCrop".Property
getData()
Returns the property that contains the binary data of this download.Doctype
getDoctype()
Deprecated.java.lang.String
getExtension()
Tries to calculate the extension from the mime-type of the underlying image.Doctype
getImageDoctype()
Returns the doctype that is used when generating the HTML.Layer
getLayer(boolean cropped, boolean resized, boolean rotated)
Returns the layer addressed by this image.int
getRotation()
Returns the rotation angle as defined by the "imageRotate".java.lang.String
getSrc()
Returns the source attribute of this image.java.lang.String
getTitle(boolean escape)
Returns the image title as defined by 'getItemName(PN_TITLE)' or overridden byDownloadResource.setTitle(String)
.Layer
resize(Layer layer)
Resizes the given layer according to the dimensions defined in this image.Layer
rotate(Layer layer)
Rotates the layer using the internal rotation angle.void
setAlt(java.lang.String alt)
Sets the alt name.void
setDoctype(Doctype doctype)
Deprecated.void
setExtension(java.lang.String extension)
Sets the extension.void
setImageDoctype(Doctype doctype)
Sets the doctype that is used when generating the HTML.void
setSrc(java.lang.String src)
Sets the source attribute-
Methods inherited from class com.day.cq.commons.DownloadResource
addAttribute, addCssClass, addQueryParam, get, get, getAttributes, getDescription, getDescription, getFileName, getFileNodePath, getFileReference, getHref, getIconPath, getIconType, getInnerHtml, getItemName, getLastModified, getMimeType, getQuery, getSelector, getString, getSuffix, getTitle, hasContent, init, isTouchAuthoringUIMode, set, setDescription, setFileName, setFileNodePath, setFileReference, setHref, setInnerHtml, setIsInUITouchMode, setItemName, setQuery, setSelector, setSuffix, setTitle
-
Methods inherited from class org.apache.sling.api.resource.ResourceWrapper
adaptTo, getChild, getChildren, getName, getParent, getPath, getResource, getResourceMetadata, getResourceResolver, getResourceSuperType, getResourceType, getValueMap, hasChildren, isResourceType, listChildren, toString
-
-
-
-
Field Detail
-
PN_HTML_WIDTH
public static final java.lang.String PN_HTML_WIDTH
name of the html width property- See Also:
- Constant Field Values
-
PN_HTML_HEIGHT
public static final java.lang.String PN_HTML_HEIGHT
name of the html height property- See Also:
- Constant Field Values
-
PN_WIDTH
public static final java.lang.String PN_WIDTH
name of the width property- See Also:
- Constant Field Values
-
PN_HEIGHT
public static final java.lang.String PN_HEIGHT
name of the height property- See Also:
- Constant Field Values
-
PN_MIN_WIDTH
public static final java.lang.String PN_MIN_WIDTH
name of the minimal width property. used for resizing.- See Also:
- Constant Field Values
-
PN_MIN_HEIGHT
public static final java.lang.String PN_MIN_HEIGHT
name of the minimal height property. used for resizing.- See Also:
- Constant Field Values
-
PN_MAX_WIDTH
public static final java.lang.String PN_MAX_WIDTH
name of the maximal width property. used for resizing.- See Also:
- Constant Field Values
-
PN_MAX_HEIGHT
public static final java.lang.String PN_MAX_HEIGHT
name of the maximal height property. used for resizing.- See Also:
- Constant Field Values
-
PN_ALT
public static final java.lang.String PN_ALT
name of the alt name property- See Also:
- Constant Field Values
-
PN_IMAGE_CROP
public static final java.lang.String PN_IMAGE_CROP
name of the image crop property- See Also:
- Constant Field Values
-
PN_IMAGE_ROTATE
public static final java.lang.String PN_IMAGE_ROTATE
name of the image rotation property- See Also:
- Constant Field Values
-
PN_LINK_URL
public static final java.lang.String PN_LINK_URL
name of the link URL property- See Also:
- Constant Field Values
-
PN_DEFAULT_IMAGE_PATH
public static final java.lang.String PN_DEFAULT_IMAGE_PATH
name of the default image path property- See Also:
- Constant Field Values
-
DEFAULT_IMAGE_PATH
public static final java.lang.String DEFAULT_IMAGE_PATH
the default image path- See Also:
- Constant Field Values
-
-
Constructor Detail
-
ImageResource
public ImageResource(Resource resource)
Creates a new image based on the given resource. the image properties are considered to 'on' the given resource.- Parameters:
resource
- resource of the image- Throws:
java.lang.IllegalArgumentException
- if the given resource is not adaptable to node.
-
ImageResource
public ImageResource(Resource resource, java.lang.String imageName)
Creates a new image based on the given resource. the image properties are considered to 'on' the given resource unlessimageName
is specified. then the respective child resource holds the image properties.- Parameters:
resource
- current resourceimageName
- name of the image resource- Throws:
java.lang.IllegalArgumentException
- if the given resource is not adaptable to node.
-
-
Method Detail
-
getTitle
public java.lang.String getTitle(boolean escape)
Returns the image title as defined by 'getItemName(PN_TITLE)' or overridden byDownloadResource.setTitle(String)
.- Overrides:
getTitle
in classDownloadResource
- Parameters:
escape
- iftrue
the string is HTML escaped- Returns:
- the title
-
getAlt
public java.lang.String getAlt()
Returns the image alt name as defined by the "alt" or overridden bysetAlt(String)
.- Returns:
- the alt name
- See Also:
PN_ALT
-
setAlt
public void setAlt(java.lang.String alt)
Sets the alt name.- Parameters:
alt
- the alt name.
-
getSrc
public java.lang.String getSrc()
Returns the source attribute of this image. the source is computed as follows:- if a selector is defined the path of the current resource concatenated with the selector, extension, and suffix is used.
- if a file node path is defined it is concatenated with the extension and suffix.
- if a file reference is defined it is concatenated with the extension and suffix.
- Returns:
- the source attribute
-
getExtension
public java.lang.String getExtension()
Tries to calculate the extension from the mime-type of the underlying image.- Overrides:
getExtension
in classDownloadResource
- Returns:
- the mime-type dependant extension or ".png" if not determinable
-
setExtension
public void setExtension(java.lang.String extension)
Description copied from class:DownloadResource
Sets the extension.- Overrides:
setExtension
in classDownloadResource
- Parameters:
extension
- the extension.
-
setSrc
public void setSrc(java.lang.String src)
Sets the source attribute- Parameters:
src
- the source attribute
-
getDoctype
@Deprecated public Doctype getDoctype()
Deprecated.Returns the doctype that is used when generating the HTML. Defaults toDoctype.HTML_401_STRICT
.- Returns:
- the doctype
- Since:
- 5.3
-
getImageDoctype
public Doctype getImageDoctype()
Returns the doctype that is used when generating the HTML. Defaults toDoctype.HTML_401_STRICT
.- Returns:
- the doctype
-
setDoctype
@Deprecated public void setDoctype(Doctype doctype)
Deprecated.Sets the doctype that is used when generating the HTML. If the given argument isnull
the current doctype is not overridden.- Parameters:
doctype
- the doctype- Since:
- 5.3
-
setImageDoctype
public void setImageDoctype(Doctype doctype)
Sets the doctype that is used when generating the HTML. If the given argument isnull
the current doctype is not overridden.- Parameters:
doctype
- the doctype
-
draw
public void draw(java.io.Writer w) throws java.io.IOException
Writes this image as tag to the given writer by invokingdoDraw(PrintWriter)
ifcanDraw()
returnstrue
.- Overrides:
draw
in classDownloadResource
- Parameters:
w
- the writer- Throws:
java.io.IOException
- if an I/O error occurs
-
getCropRect
public java.awt.Rectangle getCropRect()
Returns the cropping rectangle as defined by the "imageCrop".- Returns:
- the cropping rectangle or
null
-
getRotation
public int getRotation()
Returns the rotation angle as defined by the "imageRotate".- Returns:
- the rotation angle (in degrees)
-
resize
public Layer resize(Layer layer)
Resizes the given layer according to the dimensions defined in this image. SeeImageHelper.resize(Layer, Dimension, Dimension, Dimension)
for more details about the resizing algorithm.- Parameters:
layer
- the layer to resize- Returns:
- the layer or
null
if the layer is untouched
-
crop
public Layer crop(Layer layer)
Crops the layer using the internal crop rectangle. if the crop rectangle is empty no cropping is performed andnull
is returned.- Parameters:
layer
- the layer- Returns:
- cropped layer or
null
-
rotate
public Layer rotate(Layer layer)
Rotates the layer using the internal rotation angle. If no rotation other than 0 is defined, no rotation is performed andnull
is returned.- Parameters:
layer
- the layer- Returns:
- cropped layer or
null
-
getLayer
public Layer getLayer(boolean cropped, boolean resized, boolean rotated) throws java.io.IOException, RepositoryException
Returns the layer addressed by this image.- Parameters:
cropped
- apply cropping iftrue
resized
- apply resizing iftrue
rotated
- apply rotation iftrue
- Returns:
- the layer
- Throws:
java.io.IOException
- if an I/O error occurs.RepositoryException
- if a repository error occurs.
-
getData
public Property getData() throws RepositoryException
Description copied from class:DownloadResource
Returns the property that contains the binary data of this download. This can either by a property addressed by the internal file resource or a property addressed by an external file reference.- Overrides:
getData
in classDownloadResource
- Returns:
- binary property or
null
- Throws:
RepositoryException
- if an error accessing the repository occurs.
-
-