web/lib/Zend/View/Helper/TinySrc.php
changeset 807 877f952ae2bd
parent 207 621fa6caec0c
equal deleted inserted replaced
805:5e7a0fedabdf 807:877f952ae2bd
    13  * to license@zend.com so we can send you a copy immediately.
    13  * to license@zend.com so we can send you a copy immediately.
    14  *
    14  *
    15  * @category   Zend
    15  * @category   Zend
    16  * @package    Zend_View
    16  * @package    Zend_View
    17  * @subpackage Helper
    17  * @subpackage Helper
    18  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
    18  * @copyright  Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
    19  * @license    http://framework.zend.com/license/new-bsd     New BSD License
    19  * @license    http://framework.zend.com/license/new-bsd     New BSD License
    20  */
    20  */
    21 
    21 
    22 /** Zend_View_Helper_HtmlElement */
    22 /** Zend_View_Helper_HtmlElement */
    23 require_once 'Zend/View/Helper/HtmlElement.php';
    23 require_once 'Zend/View/Helper/HtmlElement.php';
    24 
    24 
    25 /**
    25 /**
    26  * Helper for generating urls and/or image tags for use with tinysrc.net
    26  * Helper for generating urls and/or image tags for use with tinysrc.net
    27  *
    27  *
    28  * tinysrc.net provides an API for generating scaled, browser device-specific 
    28  * tinysrc.net provides an API for generating scaled, browser device-specific
    29  * images. In essence, you pass the API the URL to an image on your own server,
    29  * images. In essence, you pass the API the URL to an image on your own server,
    30  * and tinysrc.net then provides the appropriate image based on the device that
    30  * and tinysrc.net then provides the appropriate image based on the device that
    31  * accesses it.
    31  * accesses it.
    32  *
    32  *
    33  * Additionally, tinysrc.net allows you to specify additional configuration via 
    33  * Additionally, tinysrc.net allows you to specify additional configuration via
    34  * the API:
    34  * the API:
    35  *
    35  *
    36  * - image size. You may define this as:
    36  * - image size. You may define this as:
    37  *   - explicit size
    37  *   - explicit size
    38  *   - subtractive size (size of screen minus specified number of pixels)
    38  *   - subtractive size (size of screen minus specified number of pixels)
    39  *   - percentage size (percentage of screen size))
    39  *   - percentage size (percentage of screen size))
    40  * - image format. This will convert the image to the given format; allowed 
    40  * - image format. This will convert the image to the given format; allowed
    41  *   values are "png" or "jpeg". By default, gif images are converted to png.
    41  *   values are "png" or "jpeg". By default, gif images are converted to png.
    42  *
    42  *
    43  * This helper allows you to specify all configuration options, as well as:
    43  * This helper allows you to specify all configuration options, as well as:
    44  *
    44  *
    45  * - whether or not to generate the full image tag (or just the URL)
    45  * - whether or not to generate the full image tag (or just the URL)
    46  * - base url to images (which should include the protocol, server, and 
    46  * - base url to images (which should include the protocol, server, and
    47  *   optionally port and base path)
    47  *   optionally port and base path)
    48  *
    48  *
    49  * @see        http://tinysrc.net/
    49  * @see        http://tinysrc.net/
    50  * @package    Zend_View
    50  * @package    Zend_View
    51  * @subpackage Helper
    51  * @subpackage Helper
    52  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
    52  * @copyright  Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
    53  * @license    http://framework.zend.com/license/new-bsd     New BSD License
    53  * @license    http://framework.zend.com/license/new-bsd     New BSD License
    54  */
    54  */
    55 class Zend_View_Helper_TinySrc extends Zend_View_Helper_HtmlElement
    55 class Zend_View_Helper_TinySrc extends Zend_View_Helper_HtmlElement
    56 {
    56 {
    57     const TINYSRC_BASE = 'http://i.tinysrc.mobi';
    57     const TINYSRC_BASE = 'http://i.tinysrc.mobi';
    72     protected $_dimensions = '';
    72     protected $_dimensions = '';
    73 
    73 
    74     /**
    74     /**
    75      * Default options
    75      * Default options
    76      *
    76      *
    77      * Used when determining what options were passed, and needing to merge 
    77      * Used when determining what options were passed, and needing to merge
    78      * them with default options.
    78      * them with default options.
    79      * 
    79      *
    80      * @var array
    80      * @var array
    81      */
    81      */
    82     protected $_defaultOptions = array(
    82     protected $_defaultOptions = array(
    83         'base_url'   => null,
    83         'base_url'   => null,
    84         'format'     => null,
    84         'format'     => null,
    92      */
    92      */
    93     protected $_format = '';
    93     protected $_format = '';
    94 
    94 
    95     /**
    95     /**
    96      * Generate a link or image tag pointing to tinysrc.net
    96      * Generate a link or image tag pointing to tinysrc.net
    97      * 
    97      *
    98      * @param mixed $image 
    98      * @param mixed $image
    99      * @param array $options 
    99      * @param array $options
   100      * @return void
   100      * @return void
   101      */
   101      */
   102     public function tinySrc($image = null, array $options = array())
   102     public function tinySrc($image = null, array $options = array())
   103     {
   103     {
   104         if (null === $image) {
   104         if (null === $image) {
   109         $defaultOptions['create_tag'] = $this->createTag();
   109         $defaultOptions['create_tag'] = $this->createTag();
   110         $options = array_merge($defaultOptions, $options);
   110         $options = array_merge($defaultOptions, $options);
   111 
   111 
   112         $url = '/' . $this->_mergeBaseUrl($options) . ltrim($image, '/');
   112         $url = '/' . $this->_mergeBaseUrl($options) . ltrim($image, '/');
   113 
   113 
   114         $src = self::TINYSRC_BASE 
   114         $src = self::TINYSRC_BASE
   115              . $this->_mergeFormat($options) 
   115              . $this->_mergeFormat($options)
   116              . $this->_mergeDimensions($options)
   116              . $this->_mergeDimensions($options)
   117              . $url;
   117              . $url;
   118 
   118 
   119         if (!$options['create_tag']) {
   119         if (!$options['create_tag']) {
   120             return $src;
   120             return $src;
   140         return $tag;
   140         return $tag;
   141     }
   141     }
   142 
   142 
   143     /**
   143     /**
   144      * Set base URL for images
   144      * Set base URL for images
   145      * 
   145      *
   146      * @param  string $url 
   146      * @param  string $url
   147      * @return Zend_View_Helper_TinySrc
   147      * @return Zend_View_Helper_TinySrc
   148      */
   148      */
   149     public function setBaseUrl($url)
   149     public function setBaseUrl($url)
   150     {
   150     {
   151         $this->_baseUrl = rtrim($url, '/') . '/';
   151         $this->_baseUrl = rtrim($url, '/') . '/';
   155     /**
   155     /**
   156      * Get base URL for images
   156      * Get base URL for images
   157      *
   157      *
   158      * If none already set, uses the ServerUrl and BaseUrl view helpers to
   158      * If none already set, uses the ServerUrl and BaseUrl view helpers to
   159      * determine the base URL to images.
   159      * determine the base URL to images.
   160      * 
   160      *
   161      * @return string
   161      * @return string
   162      */
   162      */
   163     public function getBaseUrl()
   163     public function getBaseUrl()
   164     {
   164     {
   165         if (null === $this->_baseUrl) {
   165         if (null === $this->_baseUrl) {
   170 
   170 
   171     /**
   171     /**
   172      * Set default image format
   172      * Set default image format
   173      *
   173      *
   174      * If set, this will set the default format to use on all images.
   174      * If set, this will set the default format to use on all images.
   175      * 
   175      *
   176      * @param  null|string $format 
   176      * @param  null|string $format
   177      * @return Zend_View_Helper_TinySrc
   177      * @return Zend_View_Helper_TinySrc
   178      * @throws Zend_View_Exception
   178      * @throws Zend_View_Exception
   179      */
   179      */
   180     public function setDefaultFormat($format = null)
   180     public function setDefaultFormat($format = null)
   181     {
   181     {
   194     }
   194     }
   195 
   195 
   196     /**
   196     /**
   197      * Set default dimensions
   197      * Set default dimensions
   198      *
   198      *
   199      * If null is specified for width, default dimensions will be cleared. If 
   199      * If null is specified for width, default dimensions will be cleared. If
   200      * only width is specified, only width will be used. If either dimension
   200      * only width is specified, only width will be used. If either dimension
   201      * fails validation, an exception is raised.
   201      * fails validation, an exception is raised.
   202      * 
   202      *
   203      * @param  null|int|string $width 
   203      * @param  null|int|string $width
   204      * @param  null|int|string $height 
   204      * @param  null|int|string $height
   205      * @return Zend_View_Helper_TinySrc
   205      * @return Zend_View_Helper_TinySrc
   206      * @throws Zend_View_Exception
   206      * @throws Zend_View_Exception
   207      */
   207      */
   208     public function setDefaultDimensions($width = null, $height = null)
   208     public function setDefaultDimensions($width = null, $height = null)
   209     {
   209     {
   230         return $this;
   230         return $this;
   231     }
   231     }
   232 
   232 
   233     /**
   233     /**
   234      * Set state of "create tag" flag
   234      * Set state of "create tag" flag
   235      * 
   235      *
   236      * @param  bool $flag 
   236      * @param  bool $flag
   237      * @return Zend_View_Helper_TinySrc
   237      * @return Zend_View_Helper_TinySrc
   238      */
   238      */
   239     public function setCreateTag($flag)
   239     public function setCreateTag($flag)
   240     {
   240     {
   241         $this->_createTagFlag = (bool) $flag;
   241         $this->_createTagFlag = (bool) $flag;
   242         return $this;
   242         return $this;
   243     }
   243     }
   244 
   244 
   245     /**
   245     /**
   246      * Should the helper create an image tag?
   246      * Should the helper create an image tag?
   247      * 
   247      *
   248      * @return bool
   248      * @return bool
   249      */
   249      */
   250     public function createTag()
   250     public function createTag()
   251     {
   251     {
   252         return $this->_createTagFlag;
   252         return $this->_createTagFlag;
   254 
   254 
   255     /**
   255     /**
   256      * Validate a dimension
   256      * Validate a dimension
   257      *
   257      *
   258      * Dimensions may be integers, optionally preceded by '-' or 'x'.
   258      * Dimensions may be integers, optionally preceded by '-' or 'x'.
   259      * 
   259      *
   260      * @param  string $dim 
   260      * @param  string $dim
   261      * @return bool
   261      * @return bool
   262      */
   262      */
   263     protected function _validateDimension($dim)
   263     protected function _validateDimension($dim)
   264     {
   264     {
   265         if (!is_scalar($dim) || is_bool($dim)) {
   265         if (!is_scalar($dim) || is_bool($dim)) {
   268         return preg_match('/^(-|x)?\d+$/', (string) $dim);
   268         return preg_match('/^(-|x)?\d+$/', (string) $dim);
   269     }
   269     }
   270 
   270 
   271     /**
   271     /**
   272      * Determine whether to use default base URL, or base URL from options
   272      * Determine whether to use default base URL, or base URL from options
   273      * 
   273      *
   274      * @param  array $options 
   274      * @param  array $options
   275      * @return string
   275      * @return string
   276      */
   276      */
   277     protected function _mergeBaseUrl(array $options)
   277     protected function _mergeBaseUrl(array $options)
   278     {
   278     {
   279         if (null === $options['base_url']) {
   279         if (null === $options['base_url']) {
   282         return rtrim($options['base_url'], '/') . '/';
   282         return rtrim($options['base_url'], '/') . '/';
   283     }
   283     }
   284 
   284 
   285     /**
   285     /**
   286      * Determine whether to use default format or format provided in options.
   286      * Determine whether to use default format or format provided in options.
   287      * 
   287      *
   288      * @param  array $options 
   288      * @param  array $options
   289      * @return string
   289      * @return string
   290      */
   290      */
   291     protected function _mergeFormat(array $options) 
   291     protected function _mergeFormat(array $options)
   292     {
   292     {
   293         if (in_array($options['format'], array('png', 'jpeg'))) {
   293         if (in_array($options['format'], array('png', 'jpeg'))) {
   294             return '/' . $options['format'];
   294             return '/' . $options['format'];
   295         }
   295         }
   296         return $this->_format;
   296         return $this->_format;
   297     }
   297     }
   298 
   298 
   299     /**
   299     /**
   300      * Determine whether to use default dimensions, or those passed in options.
   300      * Determine whether to use default dimensions, or those passed in options.
   301      * 
   301      *
   302      * @param  array $options 
   302      * @param  array $options
   303      * @return string
   303      * @return string
   304      */
   304      */
   305     protected function _mergeDimensions(array $options)
   305     protected function _mergeDimensions(array $options)
   306     {
   306     {
   307         if (!$this->_validateDimension($options['width'])) {
   307         if (!$this->_validateDimension($options['width'])) {