You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

function.html_image.php 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. <?php
  2. /**
  3. * Smarty plugin
  4. *
  5. * @package Smarty
  6. * @subpackage PluginsFunction
  7. */
  8. /**
  9. * Smarty {html_image} function plugin
  10. * Type: function<br>
  11. * Name: html_image<br>
  12. * Date: Feb 24, 2003<br>
  13. * Purpose: format HTML tags for the image<br>
  14. * Examples: {html_image file="/images/masthead.gif"}<br>
  15. * Output: <img src="/images/masthead.gif" width=400 height=23><br>
  16. * Params:
  17. * <pre>
  18. * - file - (required) - file (and path) of image
  19. * - height - (optional) - image height (default actual height)
  20. * - width - (optional) - image width (default actual width)
  21. * - basedir - (optional) - base directory for absolute paths, default is environment variable DOCUMENT_ROOT
  22. * - path_prefix - prefix for path output (optional, default empty)
  23. * </pre>
  24. *
  25. * @link http://www.smarty.net/manual/en/language.function.html.image.php {html_image}
  26. * (Smarty online manual)
  27. * @author Monte Ohrt <monte at ohrt dot com>
  28. * @author credits to Duda <duda@big.hu>
  29. * @version 1.0
  30. *
  31. * @param array $params parameters
  32. * @param Smarty_Internal_Template $template template object
  33. *
  34. * @throws SmartyException
  35. * @return string
  36. * @uses smarty_function_escape_special_chars()
  37. */
  38. function smarty_function_html_image($params, $template)
  39. {
  40. require_once(SMARTY_PLUGINS_DIR . 'shared.escape_special_chars.php');
  41. $alt = '';
  42. $file = '';
  43. $height = '';
  44. $width = '';
  45. $extra = '';
  46. $prefix = '';
  47. $suffix = '';
  48. $path_prefix = '';
  49. $basedir = isset($_SERVER['DOCUMENT_ROOT']) ? $_SERVER['DOCUMENT_ROOT'] : '';
  50. foreach ($params as $_key => $_val) {
  51. switch ($_key) {
  52. case 'file':
  53. case 'height':
  54. case 'width':
  55. case 'dpi':
  56. case 'path_prefix':
  57. case 'basedir':
  58. $$_key = $_val;
  59. break;
  60. case 'alt':
  61. if (!is_array($_val)) {
  62. $$_key = smarty_function_escape_special_chars($_val);
  63. } else {
  64. throw new SmartyException ("html_image: extra attribute '$_key' cannot be an array", E_USER_NOTICE);
  65. }
  66. break;
  67. case 'link':
  68. case 'href':
  69. $prefix = '<a href="' . $_val . '">';
  70. $suffix = '</a>';
  71. break;
  72. default:
  73. if (!is_array($_val)) {
  74. $extra .= ' ' . $_key . '="' . smarty_function_escape_special_chars($_val) . '"';
  75. } else {
  76. throw new SmartyException ("html_image: extra attribute '$_key' cannot be an array", E_USER_NOTICE);
  77. }
  78. break;
  79. }
  80. }
  81. if (empty($file)) {
  82. trigger_error("html_image: missing 'file' parameter", E_USER_NOTICE);
  83. return;
  84. }
  85. if ($file[0] == '/') {
  86. $_image_path = $basedir . $file;
  87. } else {
  88. $_image_path = $file;
  89. }
  90. // strip file protocol
  91. if (stripos($params['file'], 'file://') === 0) {
  92. $params['file'] = substr($params['file'], 7);
  93. }
  94. $protocol = strpos($params['file'], '://');
  95. if ($protocol !== false) {
  96. $protocol = strtolower(substr($params['file'], 0, $protocol));
  97. }
  98. if (isset($template->smarty->security_policy)) {
  99. if ($protocol) {
  100. // remote resource (or php stream, …)
  101. if (!$template->smarty->security_policy->isTrustedUri($params['file'])) {
  102. return;
  103. }
  104. } else {
  105. // local file
  106. if (!$template->smarty->security_policy->isTrustedResourceDir($_image_path)) {
  107. return;
  108. }
  109. }
  110. }
  111. if (!isset($params['width']) || !isset($params['height'])) {
  112. // FIXME: (rodneyrehm) getimagesize() loads the complete file off a remote resource, use custom [jpg,png,gif]header reader!
  113. if (!$_image_data = @getimagesize($_image_path)) {
  114. if (!file_exists($_image_path)) {
  115. trigger_error("html_image: unable to find '$_image_path'", E_USER_NOTICE);
  116. return;
  117. } elseif (!is_readable($_image_path)) {
  118. trigger_error("html_image: unable to read '$_image_path'", E_USER_NOTICE);
  119. return;
  120. } else {
  121. trigger_error("html_image: '$_image_path' is not a valid image file", E_USER_NOTICE);
  122. return;
  123. }
  124. }
  125. if (!isset($params['width'])) {
  126. $width = $_image_data[0];
  127. }
  128. if (!isset($params['height'])) {
  129. $height = $_image_data[1];
  130. }
  131. }
  132. if (isset($params['dpi'])) {
  133. if (strstr($_SERVER['HTTP_USER_AGENT'], 'Mac')) {
  134. // FIXME: (rodneyrehm) wrong dpi assumption
  135. // don't know who thought this up… even if it was true in 1998, it's definitely wrong in 2011.
  136. $dpi_default = 72;
  137. } else {
  138. $dpi_default = 96;
  139. }
  140. $_resize = $dpi_default / $params['dpi'];
  141. $width = round($width * $_resize);
  142. $height = round($height * $_resize);
  143. }
  144. return $prefix . '<img src="' . $path_prefix . $file . '" alt="' . $alt . '" width="' . $width . '" height="' . $height . '"' . $extra . ' />' . $suffix;
  145. }