Changeset 1013
- Timestamp:
- 07/11/08 18:38:57 (2 months ago)
- Files:
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/lib/jelix/plugins/tpl/html/function.image.php
r965 r1013 1 1 <?php 2 2 /** 3 * @package jelix 4 * @subpackage jtpl_plugin 5 * @author Lepeltier kévin 6 * @copyright 2007-2008 Lepeltier kévin 7 * @link http://www.jelix.org 8 * @licence GNU Lesser General Public Licence see LICENCE file or http://www.gnu.org/licenses/lgpl.html 3 * @package jelix 4 * @subpackage jtpl_plugin 5 * @author Lepeltier kévin 6 * @contributor Dominique Papin 7 * @copyright 2007-2008 Lepeltier kévin, 2008 Dominique Papin 8 * @link http://www.jelix.org 9 * @licence GNU Lesser General Public Licence see LICENCE file or http://www.gnu.org/licenses/lgpl.html 9 10 */ 10 11 11 12 /** 12 13 * image plugin : write the url corresponding to the image 13 * 14 * 14 15 * Add a link to the image, 15 16 * The image is resized, and cached … … 51 52 */ 52 53 function jtpl_function_html_image($tpl, $src, $params=array()) { 53 54 54 55 // Extension 55 56 if( empty($params['ext']) ) { … … 57 58 $ext = strtolower($path_parts['extension']); 58 59 } else $ext = strtolower($params['ext']); 59 60 60 61 // White background for IE 61 62 if ( empty($params['background']) … … 64 65 $params['background'] = '#ffffff'; 65 66 } 66 67 67 68 // Name of the file cache 68 69 $chaine = $src; … … 71 72 $chaine .= $key.$value; 72 73 $cachename = md5($chaine).'.'.$ext; 73 74 74 75 // Path 75 76 $cache_path = JELIX_APP_WWW_PATH.'cache/images/'.$cachename; 76 77 $origine_path = JELIX_APP_WWW_PATH.$src; 77 78 78 79 global $gJConfig; 79 80 $www = 'http'.(empty($_SERVER['HTTPS'])?'':'s').'://'.$_SERVER['HTTP_HOST'].$gJConfig->urlengine['basePath']; 80 81 $cache_www = $www.'cache/images/'.$cachename; 81 82 $origine_www = $www.$src; 82 83 83 84 // Cache and make changes if necessary 84 85 if( is_file($origine_path) && !is_file($cache_path) ) { … … 87 88 jtpl_function_html_image_inCache($src, $cachename, $params); 88 89 } 89 90 90 91 // Attributes 91 92 $att = array('alt'=>'', 'id'=>'', 'class'=>'', 'style'=>'', 'longdesc'=>'', 'name'=>'', 'ismap'=>'', 'usemap'=>'', 'title'=>'', 'dir'=>'', 'lang'=>'', 'onclick'=>'', 'ondblclick'=>'', 'onmousedown'=>'', 'onmouseup'=>'', 'onmouseover'=>'', 'onmousemove'=>'', 'onmouseout'=>'', 'onkeypress'=>'', 'onkeydown'=>'', 'onkeyup'=>''); 92 93 $att = array_intersect_key($params, $att); 93 94 94 95 // If the image does not undergo transformation 95 96 if( !is_file($cache_path) ) { … … 102 103 } else 103 104 $att['src'] = $cache_www; 104 105 105 106 // Tag image 106 107 echo '<img'; … … 109 110 echo ' '.$key.'="'.$val.'"'; 110 111 echo '/>'; 111 112 112 113 } 113 114 114 115 function jtpl_function_html_image_inCache($src, $cachename, $array) { 115 116 $mimes = array('gif'=>'image/gif', 'png'=>'image/png', 117 'jpeg'=>'image/jpeg', 'jpg'=>'image/jpeg', 'jpe'=>'image/jpeg', 116 117 $mimes = array('gif'=>'image/gif', 'png'=>'image/png', 118 'jpeg'=>'image/jpeg', 'jpg'=>'image/jpeg', 'jpe'=>'image/jpeg', 118 119 'xpm'=>'image/x-xpixmap', 'xbm'=>'image/x-xbitmap', 'wbmp'=>'image/vnd.wap.wbmp'); 119 120 120 121 global $gJConfig; 121 122 $origine_www = 'http'.(empty($_SERVER['HTTPS'])?'':'s').'://'.$_SERVER['HTTP_HOST'].$gJConfig->urlengine['basePath'].$src; 122 123 123 124 $path_parts = pathinfo($origine_www); 124 125 $ext = $mimes[strtolower($path_parts['extension'])]; 125 126 $quality = (!empty($array['quality']))? $array['quality'] : 100; 126 127 127 128 // Creating an image 128 129 switch ( $ext ) { … … 135 136 default : return ; 136 137 } 137 138 138 139 if(!empty($array['maxwidth']) && !empty($array['maxheight'])) { 139 140 $rapy = imagesy($image)/$array['maxwidth']; 141 $rapx = imagesx($image)/$array['maxheight']; 142 143 if( $rapy > $rapx ) { 144 $array['height'] = $array['maxheight']; 145 $array['width'] = imagesx($image)/$rapy; 140 141 $origWidth = imagesx($image); 142 $origHeight = imagesy($image); 143 $constWidth = $array['maxwidth']; 144 $constHeight = $array['maxheight']; 145 $ratio = imagesx($image)/imagesy($image); 146 147 if ( $origWidth < $constWidth && $origHeight < $constHeight ) { 148 $array['width'] = $origWidth; 149 $array['height'] = $origHeight; 146 150 } else { 147 $array['width'] = $array['maxwidth']; 148 $array['height'] = imagesy($image)/$rapx; 149 } 150 } 151 151 $ratioHeight = $constWidth/$ratio; 152 $ratioWidth = $constHeight*$ratio; 153 if ( $ratioWidth > $constWidth ) { 154 $constHeight = $ratioHeight; 155 } 156 else if ( $ratioHeight > $constHeight ) { 157 $constWidth = $ratioWidth; 158 } 159 $array['width'] = $constWidth; 160 $array['height'] = $constHeight; 161 } 162 } 163 152 164 if (!empty($array['width']) || !empty($array['height'])) { 153 165 154 166 $ancienimage = $image; 155 167 $resampleheight = imagesy($ancienimage); … … 157 169 $posx = 0; 158 170 $posy = 0; 159 171 160 172 if(empty($array['width'])) { 161 173 $finalheight = $array['height']; … … 175 187 } 176 188 } 177 189 178 190 if(!empty($array['zoom'])) { 179 191 $resampleheight /= 100/$array['zoom']; 180 192 $resamplewidth /= 100/$array['zoom']; 181 193 } 182 194 183 195 $posx = imagesx($ancienimage)/2 -$resamplewidth/2; 184 196 $posy = imagesy($ancienimage)/2 -$resampleheight/2; 185 197 186 198 if(!empty($array['alignh'])) { 187 199 if($array['alignh'] == 'left') $posx = 0; … … 189 201 else if($array['alignh'] != 'center') $posx = -$array['alignh']; 190 202 } 191 203 192 204 if(!empty($array['alignv'])) { 193 205 if($array['alignv'] == 'top') $posy = 0; … … 195 207 else if($array['alignv'] != 'center') $posy = -$array['alignv']; 196 208 } 197 209 198 210 $image = imagecreatetruecolor($finalwidth, $finalheight); 199 211 imagesavealpha($image, true); 200 212 $tp = imagecolorallocatealpha($image,0,0,0,127); 201 213 imagefill($image,0,0,$tp); 202 214 203 215 imagecopyresampled($image, $ancienimage, 0, 0, $posx, $posy, imagesx($image), imagesy($image), $resamplewidth, $resampleheight); 204 216 } 205 217 206 218 // The shadow cast adds to the dimension of the image chooses 207 219 if( !empty($array['shadow']) ) 208 220 $image = jtpl_function_html_image_ombre ($image, $array); 209 221 210 222 // Background 211 223 if( !empty($array['background']) ) { … … 218 230 $image = $fond; 219 231 } 220 221 232 233 222 234 $ext = empty($array['ext'])?$ext:$mimes[$array['ext']]; 223 235 $cache_path = JELIX_APP_WWW_PATH.'cache/images/'; 224 236 jFile::createDir($cache_path); 225 226 237 238 227 239 // Register 228 240 switch ( $ext ) { … … 231 243 default : imagepng($image, $cache_path.$cachename); 232 244 } 233 245 234 246 // Destruction 235 247 @imagedestroy($image); … … 237 249 238 250 function jtpl_function_html_image_ombre ( $image, $array) { 239 251 240 252 // Default 241 253 $leng = isset($array['soffset'])?$array['soffset']:10; … … 244 256 $opac = isset($array['sopacity'])?$array['sopacity']:20; 245 257 $color = isset($array['scolor'])?$array['scolor']:'#000000'; 246 258 247 259 // Color of the shadow 248 260 $color = str_replace('#', '', $color); … … 254 266 for ($x=0;$x<3;$x++) 255 267 $rgb[$x] = hexdec(substr($color,(2*$x),1)); 256 268 257 269 // Gaussian blur parameter 258 270 $coeffs = array (array ( 1), 259 array ( 1, 1), 271 array ( 1, 1), 260 272 array ( 1, 2, 1), 261 273 array ( 1, 3, 3, 1), … … 270 282 $sum = pow (2, $flou); 271 283 $demi = $flou/2; 272 273 284 285 274 286 // Horizontal blur and blur margin 275 287 $temp1 = imagecreatetruecolor(imagesx($image)+$flou, imagesy($image)+$flou); … … 277 289 $tp = imagecolorallocatealpha($temp1,0,0,0,127); 278 290 imagefill($temp1,0,0,$tp); 279 291 280 292 for ( $i=0 ; $i < imagesx($temp1) ; $i++ ) 281 293 for ( $j=0 ; $j < imagesy($temp1) ; $j++ ) { … … 291 303 imagesetpixel($temp1,$i,$j,$c); 292 304 } 293 305 294 306 // Vertical blur, a shift of the angle, opacity and color 295 307 296 308 $x = cos(deg2rad($angle))*$leng; 297 309 $y = sin(deg2rad($angle))*$leng; 298 310 299 311 $temp2 = imagecreatetruecolor(imagesx($temp1)+abs($x), imagesy($temp1)+abs($y)); 300 312 imagesavealpha($temp2, true); 301 313 $tp = imagecolorallocatealpha($temp2,0,0,0,127); 302 314 imagefill($temp2,0,0,$tp); 303 315 304 316 $x1 = $x<0?0:$x; 305 317 $y1 = $y<0?0:$y; 306 318 307 319 for ( $i=0 ; $i < imagesx($temp1) ; $i++ ) 308 320 for ( $j=0 ; $j < imagesy($temp1) ; $j++ ) { … … 319 331 } 320 332 imagedestroy($temp1); 321 333 322 334 // Merge of the image and are shade 323 335 $x = $x>0?0:$x; 324 336 $y = $y>0?0:$y; 325 337 imagecopy( $temp2, $image, $demi-$x, $demi-$y, 0, 0, imagesx($image), imagesy($image)); 326 338 327 339 return $temp2; 328 340
