function.html_image.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <?php
  2. /**
  3. * Smarty plugin
  4. * @package Smarty
  5. * @subpackage plugins
  6. */
  7. /**
  8. * Smarty {html_image} function plugin
  9. *
  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. * Input:<br>
  15. * - file = file (and path) of image (required)
  16. * - border = border width (optional, default 0)
  17. * - height = image height (optional, default actual height)
  18. * - image =image width (optional, default actual width)
  19. * - basedir = base directory for absolute paths, default
  20. * is environment variable DOCUMENT_ROOT
  21. *
  22. * Examples: {html_image file="images/masthead.gif"}
  23. * Output: <img src="images/masthead.gif" border=0 width=400 height=23>
  24. * @link http://smarty.php.net/manual/en/language.function.html.image.php {html_image}
  25. * (Smarty online manual)
  26. * @author Monte Ohrt <monte@ispi.net>
  27. * @author credits to Duda <duda@big.hu> - wrote first image function
  28. * in repository, helped with lots of functionality
  29. * @version 1.0
  30. * @param array
  31. * @param Smarty
  32. * @return string
  33. * @uses smarty_function_escape_special_chars()
  34. */
  35. function smarty_function_html_image($params, &$smarty)
  36. {
  37. require_once $smarty->_get_plugin_filepath('shared','escape_special_chars');
  38. $alt = '';
  39. $file = '';
  40. $border = 0;
  41. $height = '';
  42. $width = '';
  43. $extra = '';
  44. $prefix = '';
  45. $suffix = '';
  46. $server_vars = ($smarty->request_use_auto_globals) ? $_SERVER : $GLOBALS['HTTP_SERVER_VARS'];
  47. $basedir = isset($server_vars['DOCUMENT_ROOT']) ? $server_vars['DOCUMENT_ROOT'] : '';
  48. foreach($params as $_key => $_val) {
  49. switch($_key) {
  50. case 'file':
  51. case 'border':
  52. case 'height':
  53. case 'width':
  54. case 'dpi':
  55. case 'basedir':
  56. $$_key = $_val;
  57. break;
  58. case 'alt':
  59. if(!is_array($_val)) {
  60. $$_key = smarty_function_escape_special_chars($_val);
  61. } else {
  62. $smarty->trigger_error("html_image: extra attribute '$_key' cannot be an array", E_USER_NOTICE);
  63. }
  64. break;
  65. case 'link':
  66. case 'href':
  67. $prefix = '<a href="' . $_val . '">';
  68. $suffix = '</a>';
  69. break;
  70. default:
  71. if(!is_array($_val)) {
  72. $extra .= ' '.$_key.'="'.smarty_function_escape_special_chars($_val).'"';
  73. } else {
  74. $smarty->trigger_error("html_image: extra attribute '$_key' cannot be an array", E_USER_NOTICE);
  75. }
  76. break;
  77. }
  78. }
  79. if (empty($file)) {
  80. $smarty->trigger_error("html_image: missing 'file' parameter", E_USER_NOTICE);
  81. return;
  82. }
  83. if (substr($file,0,1) == '/') {
  84. $_image_path = $basedir . $file;
  85. } else {
  86. $_image_path = $file;
  87. }
  88. if(!isset($params['width']) || !isset($params['height'])) {
  89. if ($smarty->security &&
  90. ($_params = array('resource_type' => 'file', 'resource_name' => $_image_path)) &&
  91. (require_once(SMARTY_CORE_DIR . 'core.is_secure.php')) &&
  92. (!smarty_core_is_secure($_params, $smarty)) ) {
  93. $smarty->trigger_error("html_image: (secure) '$_image_path' not in secure directory", E_USER_NOTICE);
  94. } elseif (!$_image_data = @getimagesize($_image_path)) {
  95. if(!file_exists($_image_path)) {
  96. $smarty->trigger_error("html_image: unable to find '$_image_path'", E_USER_NOTICE);
  97. return;
  98. } else if(!is_readable($_image_path)) {
  99. $smarty->trigger_error("html_image: unable to read '$_image_path'", E_USER_NOTICE);
  100. return;
  101. } else {
  102. $smarty->trigger_error("html_image: '$_image_path' is not a valid image file", E_USER_NOTICE);
  103. return;
  104. }
  105. }
  106. if(!isset($params['width'])) {
  107. $width = $_image_data[0];
  108. }
  109. if(!isset($params['height'])) {
  110. $height = $_image_data[1];
  111. }
  112. }
  113. if(isset($params['dpi'])) {
  114. if(strstr($server_vars['HTTP_USER_AGENT'], 'Mac')) {
  115. $dpi_default = 72;
  116. } else {
  117. $dpi_default = 96;
  118. }
  119. $_resize = $dpi_default/$params['dpi'];
  120. $width = round($width * $_resize);
  121. $height = round($height * $_resize);
  122. }
  123. return $prefix . '<img src="'.$file.'" alt="'.$alt.'" border="'.$border.'" width="'.$width.'" height="'.$height.'"'.$extra.' />' . $suffix;
  124. }
  125. /* vim: set expandtab: */
  126. ?>