PageRenderTime 48ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/ php-ppcms/lib/Smarty-2.6.14/libs/plugins/function.pp_pimage.php

http://php-ppcms.googlecode.com/
PHP | 226 lines | 168 code | 18 blank | 40 comment | 72 complexity | 51854ad57f1503857a926190f623d06d MD5 | raw file
Possible License(s): LGPL-2.1, GPL-2.0
  1. <?php
  2. /**
  3. * Smarty plugin
  4. * @package Smarty
  5. * @subpackage plugins
  6. */
  7. /**
  8. * Smarty {pp_pimage} function plugin
  9. *
  10. * Type: function<br>
  11. * Name: pp_pimage<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: {pp_pimage 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 {pp_pimage}
  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_pp_pimage($params, &$smarty)
  36. {
  37. require_once $smarty->_get_plugin_filepath('shared','escape_special_chars');
  38. $url = '';
  39. $src = '';
  40. $basedir = '';
  41. $path_prefix = '';
  42. $alt = '';
  43. $width = '';
  44. $height = '';
  45. $lw = '';
  46. $lh = '';
  47. $size = '';
  48. $pid = $id = '';
  49. $extra = '';
  50. $type = 'thumb';
  51. $product = NULL;
  52. foreach($params as $_key => $_val) {
  53. switch($_key) {
  54. case 'url':
  55. case 'src':
  56. case 'basedir':
  57. case 'path_prefix':
  58. case 'alt':
  59. case 'width':
  60. case 'height':
  61. case 'lw':
  62. case 'lh':
  63. case 'size':
  64. case 'pid':
  65. case 'id':
  66. $$_key = $_val;
  67. break;
  68. case 'type':
  69. case 'product':
  70. $$_key = $_val;
  71. break;
  72. case 'link':
  73. case 'href':
  74. $prefix = '<a href="' . $_val . '">';
  75. $suffix = '</a>';
  76. break;
  77. default:
  78. if(!is_array($_val)) {
  79. $extra .= ' '.$_key.'="'.smarty_function_escape_special_chars($_val).'"';
  80. } else {
  81. $smarty->trigger_error("pp_pimage: extra attribute '$_key' cannot be an array", E_USER_NOTICE);
  82. }
  83. break;
  84. }
  85. }
  86. //
  87. if( substr($path_prefix, 0, 3) == '===' ) {
  88. $path_prefix = constant(substr($path_prefix, 3));
  89. }
  90. //
  91. if($src != '' && $size == 1 && (!isset($params['width']) || !isset($params['height']))) {
  92. if( substr($basedir, 0, 3) == '===' ) {
  93. $basedir = constant(substr($basedir, 3));
  94. }
  95. $_image_path = $basedir.$path_prefix.$src;
  96. if(!$_image_data = @getimagesize($_image_path)) {
  97. if(!file_exists($_image_path)) {
  98. $smarty->trigger_error("pp_pimage: unable to find '$_image_path'", E_USER_NOTICE);
  99. return;
  100. } else if(!is_readable($_image_path)) {
  101. $smarty->trigger_error("pp_pimage: unable to read '$_image_path'", E_USER_NOTICE);
  102. return;
  103. } else {
  104. $smarty->trigger_error("pp_pimage: '$_image_path' is not a valid image file", E_USER_NOTICE);
  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. //
  116. if( isset($product) ) {
  117. $src = $product['products_image'];
  118. $width = $product['products_image_width'];
  119. $height = $product['products_image_height'];
  120. if( $type == 'thumb' ) {
  121. if( $url == '' ) {
  122. $url = $smarty->get_vars_context('PRODUCTTHUMBIMAGE');
  123. }
  124. if( $lw == '' ) {
  125. $lw = 120;
  126. }
  127. if( $lh == '' ) {
  128. $lh = 120;
  129. }
  130. } elseif( $type == 'resize' ) {
  131. if( $url == '' ) {
  132. $url = $smarty->get_vars_context('PRODUCTRESIZEIMAGE');
  133. }
  134. if( $lw == '' ) {
  135. $lw = 120;
  136. }
  137. if( $lh == '' ) {
  138. $lh = 120;
  139. }
  140. } elseif( $type == 'origin' ) {
  141. if( $url == '' ) {
  142. $url = $smarty->get_vars_context('PRODUCTORIGINIMAGE');
  143. }
  144. if( $lw == '' ) {
  145. $lw = 120;
  146. }
  147. if( $lh == '' ) {
  148. $lh = 120;
  149. }
  150. } else {
  151. if( $url == '' ) {
  152. $url = $smarty->get_vars_context('PRODUCTTHUMBIMAGE');
  153. }
  154. if( $lw == '' ) {
  155. $lw = 120;
  156. }
  157. if( $lh == '' ) {
  158. $lh = 120;
  159. }
  160. }
  161. }
  162. //
  163. if( $src == '' ) {
  164. return '';
  165. }
  166. if( $lw == '' && $lh == '' ) {
  167. if( strpos($url, 'thumb') !== false ) {
  168. $lw = 200;
  169. $lh = 200;
  170. } elseif( strpos($url, 'resize') !== false ) {
  171. $lw = 500;
  172. $lh = 500;
  173. }
  174. }
  175. if( $height > 0 && $width > 0 && $lw > 0 && $lh > 0 ) {
  176. $ratio = $width / $height;
  177. $ratio_set = $lw / $lh;
  178. if( $ratio >= $ratio_set ) {
  179. $lh = ceil($height * ( $lw / $width ));
  180. } else {
  181. $lw = ceil($width * ( $lh / $height ));
  182. }
  183. }
  184. if( $id != '' ) {
  185. $id = ' id="' . $pid . $id . '"';
  186. }
  187. //swf
  188. if( substr($src, -4) == '.swf' ) {
  189. if( $lw > 0 && $lh > 0 ) {
  190. $width = $lw;
  191. $height = $lh;
  192. }
  193. return $prefix . '<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=10,0,0,0" width="'.$width.'" height="'.$height.'" id="'.$id.'" align="middle">
  194. <param name="allowScriptAccess" value="sameDomain" /><param name="allowFullScreen" value="false" /><param name="movie" value="'.$url.$path_prefix.$src.'" /><param name="quality" value="high" /><param name="bgcolor" value="#ffffff" />
  195. <embed src="'.$url.$path_prefix.$src.'" quality="high" bgcolor="#ffffff" width="'.$width.'" height="'.$height.'" name="'.$id.'" align="middle" allowScriptAccess="sameDomain" allowFullScreen="false" type="application/x-shockwave-flash" pluginspage="http://www.adobe.com/go/getflashplayer" />
  196. </object>'. $suffix;
  197. }
  198. //image
  199. if( $lw > 0 && $lh > 0 ) {
  200. return $prefix . '<img src="'.$url.$path_prefix.$src.'" alt="'.$alt.'" width="'.$lw.'" height="'.$lh.'"'.$id.$extra . '> '. $suffix;
  201. } elseif( $width > 0 && $height > 0 ) {
  202. return $prefix . '<img src="'.$url.$path_prefix.$src.'" alt="'.$alt.'" width="'.$width.'" height="'.$height.'"'.$id.$extra . '> '. $suffix;
  203. } else {
  204. return $prefix . '<img src="'.$url.$path_prefix.$src.'" alt="'.$alt.'" '.$id.$extra . '> '. $suffix;
  205. }
  206. }
  207. //
  208. ?>