Ticket #604: jimage.diff
| File jimage.diff, 26.2 kB (added by bastnic, 6 months ago) |
|---|
-
build/manifests/jelix-lib.mn
old new 247 247 jZipCreator.class.php 248 248 * jZone.class.php 249 249 jWSDL.class.php 250 jImage.class.php 250 251 251 252 cd lib/jelix-modules 252 253 LICENCE -
lib/jelix/plugins/tpl/html/function.image.php
old new 51 51 * @param array $params parameters for the url 52 52 */ 53 53 function jtpl_function_html_image($tpl, $src, $params=array()) { 54 55 // Extension 56 if( empty($params['ext']) ) { 57 $path_parts = pathinfo($src); 58 $ext = strtolower($path_parts['extension']); 59 } else $ext = strtolower($params['ext']); 60 61 // White background for IE 62 if ( empty($params['background']) 63 && strpos($_SERVER["HTTP_USER_AGENT"], 'MSIE') !== false 64 && strpos($_SERVER["HTTP_USER_AGENT"], 'MSIE 7') === false) { 65 $params['background'] = '#ffffff'; 66 } 67 68 // Name of the file cache 69 $chaine = $src; 70 foreach($params as $key => $value) 71 if( !in_array($key, array('alt', 'class', 'id', 'style', 'longdesc', 'name', 'ismap', 'usemap', 'title', 'dir', 'lang', 'onclick', 'ondblclick', 'onmousedown', 'onmouseup', 'onmouseover', 'onmousemove', 'onmouseout', 'onkeypress', 'onkeydown', 'onkeyup'))) 72 $chaine .= $key.$value; 73 $cachename = md5($chaine).'.'.$ext; 74 75 // Path 76 $cache_path = JELIX_APP_WWW_PATH.'cache/images/'.$cachename; 77 $origine_path = JELIX_APP_WWW_PATH.$src; 78 79 global $gJConfig; 80 $www = 'http'.(empty($_SERVER['HTTPS'])?'':'s').'://'.$_SERVER['HTTP_HOST'].$gJConfig->urlengine['basePath']; 81 $cache_www = $www.'cache/images/'.$cachename; 82 $origine_www = $www.$src; 83 84 // Cache and make changes if necessary 85 if( is_file($origine_path) && !is_file($cache_path) ) { 86 $att = array('width'=>'', 'height'=>'', 'maxwidth'=>'', 'maxheight'=>'', 'zoom'=>'', 'alignh'=>'', 'alignv'=>'', 'ext'=>'', 'quality'=>'', 'shadow'=>''); 87 if( count(array_intersect_key($params, $att)) ) 88 jtpl_function_html_image_inCache($src, $cachename, $params); 89 } 90 91 // Attributes 92 $att = array('alt'=>'', 'id'=>'', 'class'=>'', 'style'=>'', 'longdesc'=>'', 'name'=>'', 'ismap'=>'', 'usemap'=>'', 'title'=>'', 'dir'=>'', 'lang'=>'', 'onclick'=>'', 'ondblclick'=>'', 'onmousedown'=>'', 'onmouseup'=>'', 'onmouseover'=>'', 'onmousemove'=>'', 'onmouseout'=>'', 'onkeypress'=>'', 'onkeydown'=>'', 'onkeyup'=>''); 93 $att = array_intersect_key($params, $att); 94 95 // If the image does not undergo transformation 96 if( !is_file($cache_path) ) { 97 $att['src'] = $origine_www; 98 $att['style'] = empty($att['style'])?'':$att['style']; 99 if( !empty($params['width']) ) $att['style'] .= 'width: '.$params['width'].'px;'; 100 else if( !empty($params['maxwidth']) ) $att['style'] .= 'width: '.$params['maxwidth'].'px;'; 101 if( !empty($params['height']) ) $att['style'] .= 'height: '.$params['height'].'px;'; 102 else if( !empty($params['maxheight']) ) $att['style'] .= 'height: '.$params['maxheight'].'px;'; 103 } else 104 $att['src'] = $cache_www; 105 106 // Tag image 107 echo '<img'; 108 foreach( $att as $key => $val ) 109 if( !empty($val) ) 110 echo ' '.$key.'="'.$val.'"'; 111 echo '/>'; 112 113 } 114 115 function jtpl_function_html_image_inCache($src, $cachename, $array) { 116 117 $mimes = array('gif'=>'image/gif', 'png'=>'image/png', 118 'jpeg'=>'image/jpeg', 'jpg'=>'image/jpeg', 'jpe'=>'image/jpeg', 119 'xpm'=>'image/x-xpixmap', 'xbm'=>'image/x-xbitmap', 'wbmp'=>'image/vnd.wap.wbmp'); 120 121 global $gJConfig; 122 $origine_www = 'http'.(empty($_SERVER['HTTPS'])?'':'s').'://'.$_SERVER['HTTP_HOST'].$gJConfig->urlengine['basePath'].$src; 123 124 $path_parts = pathinfo($origine_www); 125 $ext = $mimes[strtolower($path_parts['extension'])]; 126 $quality = (!empty($array['quality']))? $array['quality'] : 100; 127 128 // Creating an image 129 switch ( $ext ) { 130 case 'image/gif' : $image = imagecreatefromgif($origine_www); break; 131 case 'image/jpeg' : $image = imagecreatefromjpeg($origine_www); break; 132 case 'image/png' : $image = imagecreatefrompng($origine_www); break; 133 case 'image/vnd.wap.wbmp' : $image = imagecreatefromwbmp($origine_www); break; 134 case 'image/image/x-xbitmap' : $image = imagecreatefromxbm($origine_www); break; 135 case 'image/x-xpixmap' : $image = imagecreatefromxpm($origine_www); break; 136 default : return ; 137 } 138 139 if(!empty($array['maxwidth']) && !empty($array['maxheight'])) { 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; 150 } else { 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 164 if (!empty($array['width']) || !empty($array['height'])) { 165 166 $ancienimage = $image; 167 $resampleheight = imagesy($ancienimage); 168 $resamplewidth = imagesx($ancienimage); 169 $posx = 0; 170 $posy = 0; 171 172 if(empty($array['width'])) { 173 $finalheight = $array['height']; 174 $finalwidth = $finalheight*imagesx($ancienimage)/imagesy($ancienimage); 175 } else if (empty($array['height'])) { 176 $finalwidth = $array['width']; 177 $finalheight = $finalwidth*imagesy($ancienimage)/imagesx($ancienimage); 178 } else { 179 $finalwidth = $array['width']; 180 $finalheight = $array['height']; 181 if(!empty($array['omo']) && $array['omo'] == 'true') { 182 if($array['width'] >= $array['height']) { 183 $resampleheight = ( $resamplewidth*$array['height'] )/$array['width']; 184 } else { 185 $resamplewidth = ( $resampleheight*$array['width'] )/$array['height']; 186 } 187 } 188 } 189 190 if(!empty($array['zoom'])) { 191 $resampleheight /= 100/$array['zoom']; 192 $resamplewidth /= 100/$array['zoom']; 193 } 194 195 $posx = imagesx($ancienimage)/2 -$resamplewidth/2; 196 $posy = imagesy($ancienimage)/2 -$resampleheight/2; 197 198 if(!empty($array['alignh'])) { 199 if($array['alignh'] == 'left') $posx = 0; 200 else if($array['alignh'] == 'right') $posx = -($resamplewidth - imagesx($ancienimage)); 201 else if($array['alignh'] != 'center') $posx = -$array['alignh']; 202 } 203 204 if(!empty($array['alignv'])) { 205 if($array['alignv'] == 'top') $posy = 0; 206 else if($array['alignv'] == 'bottom') $posy = -($resampleheight - imagesy($ancienimage)); 207 else if($array['alignv'] != 'center') $posy = -$array['alignv']; 208 } 209 210 $image = imagecreatetruecolor($finalwidth, $finalheight); 211 imagesavealpha($image, true); 212 $tp = imagecolorallocatealpha($image,0,0,0,127); 213 imagefill($image,0,0,$tp); 214 215 imagecopyresampled($image, $ancienimage, 0, 0, $posx, $posy, imagesx($image), imagesy($image), $resamplewidth, $resampleheight); 216 } 217 218 // The shadow cast adds to the dimension of the image chooses 219 if( !empty($array['shadow']) ) 220 $image = jtpl_function_html_image_ombre ($image, $array); 221 222 // Background 223 if( !empty($array['background']) ) { 224 $array['background'] = str_replace('#', '', $array['background']); 225 $rgb = array(0,0,0); 226 for ($x=0;$x<3;$x++) $rgb[$x] = hexdec(substr($array['background'],(2*$x),2)); 227 $fond = imagecreatetruecolor(imagesx($image), imagesy($image)); 228 imagefill( $fond, 0, 0, imagecolorallocate( $fond, $rgb[0], $rgb[1], $rgb[2]) ); 229 imagecopy( $fond, $image, 0, 0, 0, 0, imagesx($image), imagesy($image)); 230 $image = $fond; 231 } 232 233 234 $ext = empty($array['ext'])?$ext:$mimes[$array['ext']]; 235 $cache_path = JELIX_APP_WWW_PATH.'cache/images/'; 236 jFile::createDir($cache_path); 237 238 239 // Register 240 switch ( $ext ) { 241 case 'image/gif' : imagegif($image, $cache_path.$cachename); break; 242 case 'image/jpeg' : imagejpeg($image, $cache_path.$cachename, $quality); break; 243 default : imagepng($image, $cache_path.$cachename); 244 } 245 246 // Destruction 247 @imagedestroy($image); 54 echo jImage::get($src, $params); 248 55 } 249 56 250 function jtpl_function_html_image_ombre ( $image, $array) {251 57 252 // Default253 $leng = isset($array['soffset'])?$array['soffset']:10;254 $angle = isset($array['sangle'])?$array['sangle']:135;255 $flou = isset($array['sblur'])?$array['sblur']:10;256 $opac = isset($array['sopacity'])?$array['sopacity']:20;257 $color = isset($array['scolor'])?$array['scolor']:'#000000';258 259 // Color of the shadow260 $color = str_replace('#', '', $color);261 $rgb = array(0,0,0);262 if (strlen($color) == 6)263 for ($x=0;$x<3;$x++)264 $rgb[$x] = hexdec(substr($color,(2*$x),2));265 else if (strlen($color) == 3)266 for ($x=0;$x<3;$x++)267 $rgb[$x] = hexdec(substr($color,(2*$x),1));268 269 // Gaussian blur parameter270 $coeffs = array (array ( 1),271 array ( 1, 1),272 array ( 1, 2, 1),273 array ( 1, 3, 3, 1),274 array ( 1, 4, 6, 4, 1),275 array ( 1, 5, 10, 10, 5, 1),276 array ( 1, 6, 15, 20, 15, 6, 1),277 array ( 1, 7, 21, 35, 35, 21, 7, 1),278 array ( 1, 8, 28, 56, 70, 56, 28, 8, 1),279 array ( 1, 9, 36, 84, 126, 126, 84, 36, 9, 1),280 array ( 1, 10, 45, 120, 210, 252, 210, 120, 45, 10, 1),281 array ( 1, 11, 55, 165, 330, 462, 462, 330, 165, 55, 11, 1));282 $sum = pow (2, $flou);283 $demi = $flou/2;284 285 286 // Horizontal blur and blur margin287 $temp1 = imagecreatetruecolor(imagesx($image)+$flou, imagesy($image)+$flou);288 imagesavealpha($temp1, true);289 $tp = imagecolorallocatealpha($temp1,0,0,0,127);290 imagefill($temp1,0,0,$tp);291 292 for ( $i=0 ; $i < imagesx($temp1) ; $i++ )293 for ( $j=0 ; $j < imagesy($temp1) ; $j++ ) {294 $ig = $i-$demi; $jg = $j-$demi; $suma = 0;295 for ( $k=0 ; $k <= $flou ; $k++ ) {296 $ik = $ig-$demi+$k;297 if( $jg<0 || $jg>imagesy($temp1)-$flou-1 ) $alpha = 127;298 else if( $ik<0 || $ik>imagesx($temp1)-$flou-1 ) $alpha = 127;299 else $alpha = (imagecolorat($image, $ik, $jg) & 0x7F000000) >> 24;300 $suma += $alpha*$coeffs[$flou][$k];301 }302 $c = imagecolorallocatealpha($temp1, 0, 0, 0, $suma/$sum );303 imagesetpixel($temp1,$i,$j,$c);304 }305 306 // Vertical blur, a shift of the angle, opacity and color307 308 $x = cos(deg2rad($angle))*$leng;309 $y = sin(deg2rad($angle))*$leng;310 311 $temp2 = imagecreatetruecolor(imagesx($temp1)+abs($x), imagesy($temp1)+abs($y));312 imagesavealpha($temp2, true);313 $tp = imagecolorallocatealpha($temp2,0,0,0,127);314 imagefill($temp2,0,0,$tp);315 316 $x1 = $x<0?0:$x;317 $y1 = $y<0?0:$y;318 319 for ( $i=0 ; $i < imagesx($temp1) ; $i++ )320 for ( $j=0 ; $j < imagesy($temp1) ; $j++ ) {321 $suma = 0;322 for ( $k=0 ; $k <= $flou ; $k++ ) {323 $jk = $j-$demi+$k;324 if( $jk<0 || $jk>imagesy($temp1)-1 ) $alpha = 127;325 else $alpha = (imagecolorat($temp1, $i, $jk) & 0x7F000000) >> 24;326 $suma += $alpha*$coeffs[$flou][$k];327 }328 $alpha = 127-((127-($suma/$sum))/(100/$opac));329 $c = imagecolorallocatealpha($temp2, $rgb[0], $rgb[1], $rgb[2], $alpha < 0 ? 0 : $alpha > 127 ? 127 : $alpha );330 imagesetpixel($temp2,$i+$x1,$j+$y1,$c);331 }332 imagedestroy($temp1);333 334 // Merge of the image and are shade335 $x = $x>0?0:$x;336 $y = $y>0?0:$y;337 imagecopy( $temp2, $image, $demi-$x, $demi-$y, 0, 0, imagesx($image), imagesy($image));338 339 return $temp2;340 341 }342 -
lib/jelix/utils/jImage.class.php
old new 1 <?php 2 /** 3 * @package jelix 4 * @subpackage utils 5 * @author Bastien Jaillot 6 * @copyright 2008 Bastien Jaillot 7 * @link http://www.jelix.org 8 * @licence GNU Lesser General Public Licence see LICENCE file or http://www.gnu.org/licenses/lgpl.html 9 */ 10 11 /** 12 * Utility class to create cache image 13 * @package jelix 14 * @subpackage utils 15 * @static 16 */ 17 class jImage { 18 19 static function get ($src, $params = array()) { 20 21 // Extension 22 if( empty($params['ext']) ) { 23 $path_parts = pathinfo($src); 24 $ext = strtolower($path_parts['extension']); 25 } else $ext = strtolower($params['ext']); 26 27 // White background for IE 28 if ( empty($params['background']) 29 && strpos($_SERVER["HTTP_USER_AGENT"], 'MSIE') !== false 30 && strpos($_SERVER["HTTP_USER_AGENT"], 'MSIE 7') === false) { 31 $params['background'] = '#ffffff'; 32 } 33 34 // Name of the file cache 35 $chaine = $src; 36 foreach($params as $key => $value) 37 if( !in_array($key, array('alt', 'class', 'id', 'style', 'longdesc', 'name', 'ismap', 'usemap', 'title', 'dir', 'lang', 'onclick', 'ondblclick', 'onmousedown', 'onmouseup', 'onmouseover', 'onmousemove', 'onmouseout', 'onkeypress', 'onkeydown', 'onkeyup'))) 38 $chaine .= $key.$value; 39 $cachename = md5($chaine).'.'.$ext; 40 41 // Path 42 $cache_path = JELIX_APP_WWW_PATH.'cache/images/'.$cachename; 43 $origine_path = JELIX_APP_WWW_PATH.$src; 44 45 global $gJConfig; 46 $www = 'http'.(empty($_SERVER['HTTPS'])?'':'s').'://'.$_SERVER['HTTP_HOST'].$gJConfig->urlengine['basePath']; 47 $cache_www = $www.'cache/images/'.$cachename; 48 $origine_www = $www.$src; 49 50 // Cache and make changes if necessary 51 if( is_file($origine_path) && !is_file($cache_path) ) { 52 $att = array('width'=>'', 'height'=>'', 'maxwidth'=>'', 'maxheight'=>'', 'zoom'=>'', 'alignh'=>'', 'alignv'=>'', 'ext'=>'', 'quality'=>'', 'shadow'=>''); 53 if( count(array_intersect_key($params, $att)) ) 54 self::image_inCache($src, $cachename, $params); 55 } 56 57 // Attributes 58 $att = array('alt'=>'', 'id'=>'', 'class'=>'', 'style'=>'', 'longdesc'=>'', 'name'=>'', 'ismap'=>'', 'usemap'=>'', 'title'=>'', 'dir'=>'', 'lang'=>'', 'onclick'=>'', 'ondblclick'=>'', 'onmousedown'=>'', 'onmouseup'=>'', 'onmouseover'=>'', 'onmousemove'=>'', 'onmouseout'=>'', 'onkeypress'=>'', 'onkeydown'=>'', 'onkeyup'=>''); 59 $att = array_intersect_key($params, $att); 60 61 // If the image does not undergo transformation 62 if( !is_file($cache_path) ) { 63 $att['src'] = $origine_www; 64 $att['style'] = empty($att['style'])?'':$att['style']; 65 if( !empty($params['width']) ) $att['style'] .= 'width: '.$params['width'].'px;'; 66 else if( !empty($params['maxwidth']) ) $att['style'] .= 'width: '.$params['maxwidth'].'px;'; 67 if( !empty($params['height']) ) $att['style'] .= 'height: '.$params['height'].'px;'; 68 else if( !empty($params['maxheight']) ) $att['style'] .= 'height: '.$params['maxheight'].'px;'; 69 } else 70 $att['src'] = $cache_www; 71 72 // Tag image 73 $imgprint = '<img'; 74 foreach( $att as $key => $val ) 75 if( !empty($val) ) 76 $imgprint .= ' '.$key.'="'.$val.'"'; 77 $imgprint .= '/>'; 78 79 return $imgprint; 80 } 81 82 static protected function image_inCache($src, $cachename, $array) { 83 84 $mimes = array('gif'=>'image/gif', 'png'=>'image/png', 85 'jpeg'=>'image/jpeg', 'jpg'=>'image/jpeg', 'jpe'=>'image/jpeg', 86 'xpm'=>'image/x-xpixmap', 'xbm'=>'image/x-xbitmap', 'wbmp'=>'image/vnd.wap.wbmp'); 87 88 global $gJConfig; 89 $origine_www = 'http'.(empty($_SERVER['HTTPS'])?'':'s').'://'.$_SERVER['HTTP_HOST'].$gJConfig->urlengine['basePath'].$src; 90 91 $path_parts = pathinfo($origine_www); 92 $ext = $mimes[strtolower($path_parts['extension'])]; 93 $quality = (!empty($array['quality']))? $array['quality'] : 100; 94 95 // Creating an image 96 switch ( $ext ) { 97 case 'image/gif' : $image = imagecreatefromgif($origine_www); break; 98 case 'image/jpeg' : $image = imagecreatefromjpeg($origine_www); break; 99 case 'image/png' : $image = imagecreatefrompng($origine_www); break; 100 case 'image/vnd.wap.wbmp' : $image = imagecreatefromwbmp($origine_www); break; 101 case 'image/image/x-xbitmap' : $image = imagecreatefromxbm($origine_www); break; 102 case 'image/x-xpixmap' : $image = imagecreatefromxpm($origine_www); break; 103 default : return ; 104 } 105 106 if(!empty($array['maxwidth']) && !empty($array['maxheight'])) { 107 108 $origWidth = imagesx($image); 109 $origHeight = imagesy($image); 110 $constWidth = $array['maxwidth']; 111 $constHeight = $array['maxheight']; 112 $ratio = imagesx($image)/imagesy($image); 113 114 if ( $origWidth < $constWidth && $origHeight < $constHeight ) { 115 $array['width'] = $origWidth; 116 $array['height'] = $origHeight; 117 } else { 118 $ratioHeight = $constWidth/$ratio; 119 $ratioWidth = $constHeight*$ratio; 120 if ( $ratioWidth > $constWidth ) { 121 $constHeight = $ratioHeight; 122 } 123 else if ( $ratioHeight > $constHeight ) { 124 $constWidth = $ratioWidth; 125 } 126 $array['width'] = $constWidth; 127 $array['height'] = $constHeight; 128 } 129 } 130 131 if (!empty($array['width']) || !empty($array['height'])) { 132 133 $ancienimage = $image; 134 $resampleheight = imagesy($ancienimage); 135 $resamplewidth = imagesx($ancienimage); 136 $posx = 0; 137 $posy = 0; 138 139 if(empty($array['width'])) { 140 $finalheight = $array['height']; 141 $finalwidth = $finalheight*imagesx($ancienimage)/imagesy($ancienimage); 142 } else if (empty($array['height'])) { 143 $finalwidth = $array['width']; 144 $finalheight = $finalwidth*imagesy($ancienimage)/imagesx($ancienimage); 145 } else { 146 $finalwidth = $array['width']; 147 $finalheight = $array['height']; 148 if(!empty($array['omo']) && $array['omo'] == 'true') { 149 if($array['width'] >= $array['height']) { 150 $resampleheight = ( $resamplewidth*$array['height'] )/$array['width']; 151 } else { 152 $resamplewidth = ( $resampleheight*$array['width'] )/$array['height']; 153 } 154 } 155 } 156 157 if(!empty($array['zoom'])) { 158 $resampleheight /= 100/$array['zoom']; 159 $resamplewidth /= 100/$array['zoom']; 160 } 161 162 $posx = imagesx($ancienimage)/2 -$resamplewidth/2; 163 $posy = imagesy($ancienimage)/2 -$resampleheight/2; 164 165 if(!empty($array['alignh'])) { 166 if($array['alignh'] == 'left') $posx = 0; 167 else if($array['alignh'] == 'right') $posx = -($resamplewidth - imagesx($ancienimage)); 168 else if($array['alignh'] != 'center') $posx = -$array['alignh']; 169 } 170 171 if(!empty($array['alignv'])) { 172 if($array['alignv'] == 'top') $posy = 0; 173 else if($array['alignv'] == 'bottom') $posy = -($resampleheight - imagesy($ancienimage)); 174 else if($array['alignv'] != 'center') $posy = -$array['alignv']; 175 } 176 177 $image = imagecreatetruecolor($finalwidth, $finalheight); 178 imagesavealpha($image, true); 179 $tp = imagecolorallocatealpha($image,0,0,0,127); 180 imagefill($image,0,0,$tp); 181 182 imagecopyresampled($image, $ancienimage, 0, 0, $posx, $posy, imagesx($image), imagesy($image), $resamplewidth, $resampleheight); 183 } 184 185 // The shadow cast adds to the dimension of the image chooses 186 if( !empty($array['shadow']) ) 187 $image = self::image_ombre ($image, $array); 188 189 // Background 190 if( !empty($array['background']) ) { 191 $array['background'] = str_replace('#', '', $array['background']); 192 $rgb = array(0,0,0); 193 for ($x=0;$x<3;$x++) $rgb[$x] = hexdec(substr($array['background'],(2*$x),2)); 194 $fond = imagecreatetruecolor(imagesx($image), imagesy($image)); 195 imagefill( $fond, 0, 0, imagecolorallocate( $fond, $rgb[0], $rgb[1], $rgb[2]) ); 196 imagecopy( $fond, $image, 0, 0, 0, 0, imagesx($image), imagesy($image)); 197 $image = $fond; 198 } 199 200 201 $ext = empty($array['ext'])?$ext:$mimes[$array['ext']]; 202 $cache_path = JELIX_APP_WWW_PATH.'cache/images/'; 203 jFile::createDir($cache_path); 204 205 206 // Register 207 switch ( $ext ) { 208 case 'image/gif' : imagegif($image, $cache_path.$cachename); break; 209 case 'image/jpeg' : imagejpeg($image, $cache_path.$cachename, $quality); break; 210 default : imagepng($image, $cache_path.$cachename); 211 } 212 213 // Destruction 214 @imagedestroy($image); 215 } 216 217 static protected function image_ombre ($image, $array) { 218 219 // Default 220 $leng = isset($array['soffset'])?$array['soffset']:10; 221 $angle = isset($array['sangle'])?$array['sangle']:135; 222 $flou = isset($array['sblur'])?$array['sblur']:10; 223 $opac = isset($array['sopacity'])?$array['sopacity']:20; 224 $color = isset($array['scolor'])?$array['scolor']:'#000000'; 225 226 // Color of the shadow 227 $color = str_replace('#', '', $color); 228 $rgb = array(0,0,0); 229 if (strlen($color) == 6) 230 for ($x=0;$x<3;$x++) 231 $rgb[$x] = hexdec(substr($color,(2*$x),2)); 232 else if (strlen($color) == 3) 233 for ($x=0;$x<3;$x++) 234 $rgb[$x] = hexdec(substr($color,(2*$x),1)); 235 236 // Gaussian blur parameter 237 $coeffs = array (array ( 1), 238 array ( 1, 1), 239 array ( 1, 2, 1), 240 array ( 1, 3, 3, 1), 241 array ( 1, 4, 6, 4, 1), 242 array ( 1, 5, 10, 10, 5, 1), 243 array ( 1, 6, 15, 20, 15, 6, 1), 244 array ( 1, 7, 21, 35, 35, 21, 7, 1), 245 array ( 1, 8, 28, 56, 70, 56, 28, 8, 1), 246 array ( 1, 9, 36, 84, 126, 126, 84, 36, 9, 1), 247 array ( 1, 10, 45, 120, 210, 252, 210, 120, 45, 10, 1), 248 array ( 1, 11, 55, 165, 330, 462, 462, 330, 165, 55, 11, 1)); 249 $sum = pow (2, $flou); 250 $demi = $flou/2; 251 252 253 // Horizontal blur and blur margin 254 $temp1 = imagecreatetruecolor(imagesx($image)+$flou, imagesy($image)+$flou); 255 imagesavealpha($temp1, true); 256 $tp = imagecolorallocatealpha($temp1,0,0,0,127); 257 imagefill($temp1,0,0,$tp); 258 259 for ( $i=0 ; $i < imagesx($temp1) ; $i++ ) 260 for ( $j=0 ; $j < imagesy($temp1) ; $j++ ) { 261 $ig = $i-$demi; $jg = $j-$demi; $suma = 0; 262 for ( $k=0 ; $k <= $flou ; $k++ ) { 263 $ik = $ig-$demi+$k; 264 if( $jg<0 || $jg>imagesy($temp1)-$flou-1 ) $alpha = 127; 265 else if( $ik<0 || $ik>imagesx($temp1)-$flou-1 ) $alpha = 127; 266 else $alpha = (imagecolorat($image, $ik, $jg) & 0x7F000000) >> 24; 267 $suma += $alpha*$coeffs[$flou][$k]; 268 } 269 $c = imagecolorallocatealpha($temp1, 0, 0, 0, $suma/$sum ); 270 imagesetpixel($temp1,$i,$j,$c); 271 } 272 273 // Vertical blur, a shift of the angle, opacity and color 274 275 $x = cos(deg2rad($angle))*$leng; 276 $y = sin(deg2rad($angle))*$leng; 277 278 $temp2 = imagecreatetruecolor(imagesx($temp1)+abs($x), imagesy($temp1)+abs($y)); 279 imagesavealpha($temp2, true); 280 $tp = imagecolorallocatealpha($temp2,0,0,0,127); 281 imagefill($temp2,0,0,$tp); 282 283 $x1 = $x<0?0:$x; 284 $y1 = $y<0?0:$y; 285 286 for ( $i=0 ; $i < imagesx($temp1) ; $i++ ) 287 for ( $j=0 ; $j < imagesy($temp1) ; $j++ ) { 288 $suma = 0; 289 for ( $k=0 ; $k <= $flou ; $k++ ) { 290 $jk = $j-$demi+$k; 291 if( $jk<0 || $jk>imagesy($temp1)-1 ) $alpha = 127; 292 else $alpha = (imagecolorat($temp1, $i, $jk) & 0x7F000000) >> 24; 293 $suma += $alpha*$coeffs[$flou][$k]; 294 } 295 $alpha = 127-((127-($suma/$sum))/(100/$opac)); 296 $c = imagecolorallocatealpha($temp2, $rgb[0], $rgb[1], $rgb[2], $alpha < 0 ? 0 : $alpha > 127 ? 127 : $alpha ); 297 imagesetpixel($temp2,$i+$x1,$j+$y1,$c); 298 } 299 imagedestroy($temp1); 300 301 // Merge of the image and are shade 302 $x = $x>0?0:$x; 303 $y = $y>0?0:$y; 304 imagecopy( $temp2, $image, $demi-$x, $demi-$y, 0, 0, imagesx($image), imagesy($image)); 305 306 return $temp2; 307 308 } 309 310 }
