Automatically exported from code.google.com/p/planningalerts
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 4.7 KiB

17 年之前
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. * - height = image height (optional, default actual height)
  17. * - width = image width (optional, default actual width)
  18. * - basedir = base directory for absolute paths, default
  19. * is environment variable DOCUMENT_ROOT
  20. * - path_prefix = prefix for path output (optional, default empty)
  21. *
  22. * Examples: {html_image file="/images/masthead.gif"}
  23. * Output: <img src="/images/masthead.gif" 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 at ohrt dot com>
  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. $height = '';
  41. $width = '';
  42. $extra = '';
  43. $prefix = '';
  44. $suffix = '';
  45. $path_prefix = '';
  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 'height':
  52. case 'width':
  53. case 'dpi':
  54. case 'path_prefix':
  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(!$_image_data = @getimagesize($_image_path)) {
  90. if(!file_exists($_image_path)) {
  91. $smarty->trigger_error("html_image: unable to find '$_image_path'", E_USER_NOTICE);
  92. return;
  93. } else if(!is_readable($_image_path)) {
  94. $smarty->trigger_error("html_image: unable to read '$_image_path'", E_USER_NOTICE);
  95. return;
  96. } else {
  97. $smarty->trigger_error("html_image: '$_image_path' is not a valid image file", E_USER_NOTICE);
  98. return;
  99. }
  100. }
  101. if ($smarty->security &&
  102. ($_params = array('resource_type' => 'file', 'resource_name' => $_image_path)) &&
  103. (require_once(SMARTY_CORE_DIR . 'core.is_secure.php')) &&
  104. (!smarty_core_is_secure($_params, $smarty)) ) {
  105. $smarty->trigger_error("html_image: (secure) '$_image_path' not in secure directory", E_USER_NOTICE);
  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. /* vim: set expandtab: */
  127. ?>