function.html_image.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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"}
  16. * Output: <img src="/images/masthead.gif" width=400 height=23>
  17. *
  18. * @link http://smarty.php.net/manual/en/language.function.html.image.php {html_image}
  19. (Smarty online manual)
  20. * @author Monte Ohrt <monte at ohrt dot com>
  21. * @author credits to Duda <duda@big.hu>
  22. * @version 1.0
  23. * @param array $params parameters
  24. * Input:<br>
  25. * - file = file (and path) of image (required)
  26. * - height = image height (optional, default actual height)
  27. * - width = image width (optional, default actual width)
  28. * - basedir = base directory for absolute paths, default
  29. * is environment variable DOCUMENT_ROOT
  30. * - path_prefix = prefix for path output (optional, default empty)
  31. * @param object $smarty Smarty object
  32. * @param object $template template object
  33. * @return string
  34. * @uses smarty_function_escape_special_chars()
  35. */
  36. function smarty_function_html_image($params, $smarty, $template)
  37. {
  38. require_once(SMARTY_PLUGINS_DIR . 'shared.escape_special_chars.php');
  39. //$smarty->loadPlugin('Smarty_shared_escape_special_chars');
  40. $alt = '';
  41. $file = '';
  42. $height = '';
  43. $width = '';
  44. $extra = '';
  45. $prefix = '';
  46. $suffix = '';
  47. $path_prefix = '';
  48. $server_vars = ($smarty->request_use_auto_globals) ? $_SERVER : $GLOBALS['HTTP_SERVER_VARS'];
  49. $basedir = isset($server_vars['DOCUMENT_ROOT']) ? $server_vars['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 Exception ("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 Exception ("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 (substr($file, 0, 1) == '/') {
  86. $_image_path = $basedir . $file;
  87. } else {
  88. $_image_path = $file;
  89. }
  90. if (!isset($params['width']) || !isset($params['height'])) {
  91. if (!$_image_data = @getimagesize($_image_path)) {
  92. if (!file_exists($_image_path)) {
  93. trigger_error("html_image: unable to find '$_image_path'", E_USER_NOTICE);
  94. return;
  95. } else if (!is_readable($_image_path)) {
  96. trigger_error("html_image: unable to read '$_image_path'", E_USER_NOTICE);
  97. return;
  98. } else {
  99. trigger_error("html_image: '$_image_path' is not a valid image file", E_USER_NOTICE);
  100. return;
  101. }
  102. }
  103. if ($template->security) {
  104. if (!$smarty->security_handler->isTrustedResourceDir($_image_path)) {
  105. return;
  106. }
  107. }
  108. if (!isset($params['width'])) {
  109. $width = $_image_data[0];
  110. }
  111. if (!isset($params['height'])) {
  112. $height = $_image_data[1];
  113. }
  114. }
  115. if (isset($params['dpi'])) {
  116. if (strstr($server_vars['HTTP_USER_AGENT'], 'Mac')) {
  117. $dpi_default = 72;
  118. } else {
  119. $dpi_default = 96;
  120. }
  121. $_resize = $dpi_default / $params['dpi'];
  122. $width = round($width * $_resize);
  123. $height = round($height * $_resize);
  124. }
  125. return $prefix . '<img src="' . $path_prefix . $file . '" alt="' . $alt . '" width="' . $width . '" height="' . $height . '"' . $extra . ' />' . $suffix;
  126. }
  127. ?>