PageRenderTime 46ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/vendors/Smarty-3.0.9/libs/plugins/function.html_image.php

https://github.com/sevkialacatli/CakeSmarty
PHP | 137 lines | 92 code | 12 blank | 33 comment | 23 complexity | 35035057f3cf246e44bf3ff90aba0926 MD5 | raw file
Possible License(s): LGPL-3.0
  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 $template template object
  32. * @return string
  33. * @uses smarty_function_escape_special_chars()
  34. */
  35. function smarty_function_html_image($params, $template)
  36. {
  37. require_once(SMARTY_PLUGINS_DIR . 'shared.escape_special_chars.php');
  38. $alt = '';
  39. $file = '';
  40. $height = '';
  41. $width = '';
  42. $extra = '';
  43. $prefix = '';
  44. $suffix = '';
  45. $path_prefix = '';
  46. $server_vars = $_SERVER;
  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. throw new SmartyException ("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. throw new SmartyException ("html_image: extra attribute '$_key' cannot be an array", E_USER_NOTICE);
  75. }
  76. break;
  77. }
  78. }
  79. if (empty($file)) {
  80. 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. trigger_error("html_image: unable to find '$_image_path'", E_USER_NOTICE);
  92. return;
  93. } else if (!is_readable($_image_path)) {
  94. trigger_error("html_image: unable to read '$_image_path'", E_USER_NOTICE);
  95. return;
  96. } else {
  97. trigger_error("html_image: '$_image_path' is not a valid image file", E_USER_NOTICE);
  98. return;
  99. }
  100. }
  101. if (isset($template->smarty->security_policy)) {
  102. if (!$template->smarty->security_policy->isTrustedResourceDir($_image_path)) {
  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="' . $path_prefix . $file . '" alt="' . $alt . '" width="' . $width . '" height="' . $height . '"' . $extra . ' />' . $suffix;
  124. }
  125. ?>