PageRenderTime 22ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/framework/Core/lib/Horde/Themes/Image.php

https://github.com/imr/horde
PHP | 150 lines | 69 code | 18 blank | 63 comment | 15 complexity | e707558771750fde9e48f4e0a8698b23 MD5 | raw file
  1. <?php
  2. /**
  3. * Copyright 2010-2014 Horde LLC (http://www.horde.org/)
  4. *
  5. * See the enclosed file COPYING for license information (LGPL). If you
  6. * did not receive this file, see http://www.horde.org/licenses/lgpl21.
  7. *
  8. * @category Horde
  9. * @copyright 2010-2014 Horde LLC
  10. * @license http://www.horde.org/licenses/lgpl21 LGPL
  11. * @package Core
  12. */
  13. /**
  14. * An object-oriented interface to a themed image.
  15. *
  16. * @author Michael Slusarz <slusarz@horde.org>
  17. * @category Horde
  18. * @copyright 2010-2014 Horde LLC
  19. * @license http://www.horde.org/licenses/lgpl21 LGPL
  20. * @package Core
  21. *
  22. * @property-read string $base64img See Horde_Themes_Image::base64ImgData()
  23. * (since 2.10.0).
  24. */
  25. class Horde_Themes_Image extends Horde_Themes_Element
  26. {
  27. /**
  28. * The default directory name for this element type.
  29. *
  30. * @var string
  31. */
  32. protected $_dirname = 'graphics';
  33. /**
  34. */
  35. public function __get($name)
  36. {
  37. switch ($name) {
  38. case 'base64img':
  39. return self::base64ImgData($this);
  40. default:
  41. return parent::__get($name);
  42. }
  43. }
  44. /**
  45. * Constructs a correctly-pathed tag to an image.
  46. *
  47. * @param mixed $src The image file (either a string or a
  48. * Horde_Themes_Image object).
  49. * @param array $opts Additional options:
  50. * - alt: (string) Text describing the image.
  51. * - attr: (mixed) Any additional attributes for the image tag. Can be a
  52. * pre-built string or an array of key/value pairs that will be
  53. * assembled and html-encoded.
  54. * - fullsrc: (boolean) TODO
  55. * - imgopts: (array) TODO
  56. *
  57. * @return string The full image tag.
  58. */
  59. static public function tag($src, array $opts = array())
  60. {
  61. global $browser, $conf;
  62. $opts = array_merge(array(
  63. 'alt' => '',
  64. 'attr' => array(),
  65. 'fullsrc' => false,
  66. 'imgopts' => array()
  67. ), $opts);
  68. /* If browser does not support images, simply return the ALT text. */
  69. if (!$browser->hasFeature('images')) {
  70. return htmlspecialchars($opts['alt']);
  71. }
  72. $xml = new SimpleXMLElement('<root><img ' . (is_array($opts['attr']) ? '' : $opts['attr']) . '/></root>');
  73. $img = $xml->img;
  74. if (is_array($opts['attr'])) {
  75. foreach ($opts['attr'] as $key => $val) {
  76. $img->addAttribute($key, $val);
  77. }
  78. }
  79. if (strlen($opts['alt'])) {
  80. $img->addAttribute('alt', $opts['alt']);
  81. }
  82. /* If no directory has been specified, get it from the registry. */
  83. if (!($src instanceof Horde_Themes_Image) &&
  84. (substr($src, 0, 1) != '/')) {
  85. $src = Horde_Themes::img($src, $opts['imgopts']);
  86. }
  87. if (empty($conf['nobase64_img'])) {
  88. $src = self::base64ImgData($src);
  89. }
  90. if ($opts['fullsrc'] && (substr($src, 0, 10) != 'data:image')) {
  91. $src = Horde::url($src, true, array('append_session' => -1));
  92. }
  93. $img->addAttribute('src', $src);
  94. return $img->asXML();
  95. }
  96. /*
  97. * Generate RFC 2397-compliant image data strings.
  98. *
  99. * @param mixed $in URI or Horde_Themes_Image object containing
  100. * image data.
  101. * @param integer $limit Sets a hard size limit for image data; if
  102. * exceeded, will not string encode.
  103. *
  104. * @return string The string to use in the image 'src' attribute; either
  105. * the image data if the browser supports, or the URI
  106. * if not.
  107. */
  108. static public function base64ImgData($in, $limit = null)
  109. {
  110. if (!($dataurl = $GLOBALS['browser']->hasFeature('dataurl'))) {
  111. return $in;
  112. }
  113. if (!is_null($limit) &&
  114. (is_bool($dataurl) || ($limit < $dataurl))) {
  115. $dataurl = $limit;
  116. }
  117. /* Only encode image files if they are below the dataurl limit. */
  118. if (!($in instanceof Horde_Themes_Image)) {
  119. $in = self::fromUri($in);
  120. }
  121. if (!file_exists($in->fs)) {
  122. return $in->uri;
  123. }
  124. /* Delete approx. 50 chars from the limit to account for the various
  125. * data/base64 header text. Multiply by 0.75 to determine the
  126. * base64 encoded size. */
  127. return (($dataurl === true) ||
  128. (filesize($in->fs) <= (($dataurl * 0.75) - 50)))
  129. ? strval(Horde_Url_Data::create(Horde_Mime_Magic::extToMime(substr($in->uri, strrpos($in->uri, '.') + 1)), file_get_contents($in->fs)))
  130. : $in->uri;
  131. }
  132. }