Perforce Chronicle 2012.2/486814
API Documentation

Content_Form_Decorator_DisplayImage Class Reference

Display image tag for associated image. More...

Inheritance diagram for Content_Form_Decorator_DisplayImage:
Content_Form_Decorator_DisplayFileLink

List of all members.

Protected Member Functions

 _computeSize ($actualWidth, $actualHeight, $fixedWidth, $fixedHeight, $maxWidth, $maxHeight)
 Compute the final size of the image according to the original dimensions and the given fixed or limited width/height options.
 _renderHtmlTag ($label, $params)
 Produce html tag for current element and given label.

Detailed Description

Display image tag for associated image.

Image src is taken to be the image URI for the element's associated content record. The element must be content type enhanced to get the associated content.

Several options are supported to influence the presentation:

width - fix the image width (in pixels) height - fix the image height maxWidth - limit the image width maxHeight - limit the image height link - link to the original image target - target for the image link

Copyright:
2011-2012 Perforce Software. All rights reserved
License:
Please see LICENSE.txt in top-level folder of this distribution.
Version:
2012.2/486814

Member Function Documentation

Content_Form_Decorator_DisplayImage::_computeSize ( actualWidth,
actualHeight,
fixedWidth,
fixedHeight,
maxWidth,
maxHeight 
) [protected]

Compute the final size of the image according to the original dimensions and the given fixed or limited width/height options.

Parameters:
int$actualWidththe actual width of the original image
int$actualHeightthe actual width of the original image
int$fixedWidthoptional - desired fixed width
int$fixedHeightoptional - desired fixed height
int$maxWidthoptional - width to limit image to
int$maxHeightoptional - height to limit image to
Returns:
array final width and height (width first, height second)
    {
        $ratio = $actualWidth / $actualHeight;

        //  - start with original size if no fixed dimensions specified
        //  - if only one dimension was specified, compute the other one
        //    to keep the aspect ration of the original image
        //  - if both given, use them as given
        if (!$fixedWidth && !$fixedHeight) {
            $width  = $actualWidth;
            $height = $actualHeight;
        } else if (!$fixedWidth) {
            $width  = round($fixedHeight * $ratio);
            $height = $fixedHeight;
        } else if (!$fixedHeight) {
            $width  = $fixedWidth;
            $height = round($fixedWidth / $ratio);
        } else {
            $width  = $fixedWidth;
            $height = $fixedHeight;
        }

        // lower image dimensions if they exceed maximum dimensions
        if ($maxHeight && $maxHeight < $height) {
            $width  = round($width * $maxHeight / $height);
            $height = $maxHeight;
        }
        if ($maxWidth && $maxWidth < $width) {
            $height = round($height * $maxWidth / $width);
            $width  = $maxWidth;
        }

        return array($width, $height);
    }
Content_Form_Decorator_DisplayImage::_renderHtmlTag ( label,
params 
) [protected]

Produce html tag for current element and given label.

Extends parent to produce an image tag.

Parameters:
string$labelthe label to include in the tag.
array$paramsthe paramaters to provide to the Uri function
Returns:
string the rendered html tag.

Reimplemented from Content_Form_Decorator_DisplayFileLink.

    {
        // some browsers have more aggressive cache, include the
        // version in the uri to avoid stale cache
        $record = $this->getElement()->getContentRecord();
        $params = (array) $params;
        if (!isset($params['version'])) {
            $params['v'] = $record->toP4File()->getStatus('headRev');
        }

        // image dimensions are routinely stored in content field metadata
        // if present, set them on image to optimize image loading/rendering.
        $info        = $record->getFieldMetadata($this->getElement()->getName());
        $size        = isset($info['dimensions']) ? $info['dimensions'] : array();
        $width       = isset($size['width'])      ? $size['width']      : null;
        $height      = isset($size['height'])     ? $size['height']     : null;

        // decorator options can influence the size of the displayed image.
        $fixedWidth  = $this->getOption('width');
        $fixedHeight = $this->getOption('height');
        $maxWidth    = $this->getOption('maxWidth');
        $maxHeight   = $this->getOption('maxHeight');

        // if we know the actual dimensions, we can compute the final size based on options.
        // otherwise, if fixed dimensions have been given, we'll use them as given.
        if ($width && $height) {
            list($width, $height) = $this->_computeSize(
                $width, $height, $fixedWidth, $fixedHeight, $maxWidth, $maxHeight
            );
        } else {
            $width  = $fixedWidth;
            $height = $fixedHeight;
        }

        // allow size options to influence request params (for server-side scaling).
        $options = array('width', 'height', 'maxWidth', 'maxHeight');
        foreach ($options as $option) {
            if ($this->getOption($option)) {
                $params[$option] = $this->getOption($option);
            }
        }

        // build the image tag.
        $html = '<img'
              . ' src="'    . htmlentities($record->getUri('image', $params)) . '"'
              . ' alt="'    . htmlentities($label) . '"'
              . ($width  ? ' width="'  . htmlentities($width)  . '"' : '')
              . ($height ? ' height="' . htmlentities($height) . '"' : '')
              . '>';

        // options can specify that the image is a link.
        // exclude sizing options when building image link.
        if ($this->getOption('link')) {
            $target = $this->getOption('target');
            if ($target == '_lightbox') {
                $target = "";
                $click  = "new p4cms.ui.LightBox({href:this.href, opener: this}).startup(); return false;";
            }
            $href = $record->getUri('image', array_diff_key($params, array_flip($options)));
            $html = '<a '
                  . '    href="' . htmlentities($href) . '"'
                  . '  target="' . $target . '"'
                  . ' onclick="' . (isset($click) ? $click : '') . '">'
                  . $html
                  . '</a>';
        }

        return $html;
    }

The documentation for this class was generated from the following file: