function.html_image.php 4.4 KB

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