Ticket #604: jImage_final.2.diff
| File jImage_final.2.diff, 37.7 kB (added by Lipki, 6 months ago) |
|---|
-
plugins/tpl/html/function.image.php
old new 34 34 * sopacity :uint 35 35 * scolor #000000 :string 36 36 * background #000000 :string 37 * notexists :boolean [true|false|alternate uri] 37 38 * 38 39 * gif -> image/gif 39 40 * jpeg -> image/jpeg … … 51 52 */ 52 53 function jtpl_function_html_image($tpl, $src, $params=array()) { 53 54 54 // Extension 55 if( empty($params['ext']) ) { 56 $path_parts = pathinfo($src); 57 $ext = strtolower($path_parts['extension']); 58 } else $ext = strtolower($params['ext']); 55 if( empty($params['notexists']) ) 56 $params['notexists'] = false; 59 57 60 // White background for IE 61 if ( empty($params['background']) 62 && strpos($_SERVER["HTTP_USER_AGENT"], 'MSIE') !== false 63 && strpos($_SERVER["HTTP_USER_AGENT"], 'MSIE 7') === false) { 64 $params['background'] = '#ffffff'; 65 } 58 // cache 59 $cache = new jCacheImage( $src, $params ); 66 60 67 // Name of the file cache 68 $chaine = $src; 69 foreach($params as $key => $value) 70 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'))) 71 $chaine .= $key.$value; 72 $cachename = md5($chaine).'.'.$ext; 61 // url de alternative 62 if( is_string($params['notexists']) ) 63 $cache->alternate( $params['notexists'], $params ); 73 64 74 // Path 75 $cache_path = JELIX_APP_WWW_PATH.'cache/images/'.$cachename; 76 $origine_path = JELIX_APP_WWW_PATH.$src; 65 if( !$cache->isFileInCache() ) 66 $cache->appliParams( $params ); 67 68 // attributes 69 $cache->addAttributes( $params ); 77 70 78 global $gJConfig;79 $www = 'http'.(empty($_SERVER['HTTPS'])?'':'s').'://'.$_SERVER['HTTP_HOST'].$gJConfig->urlengine['basePath'];80 $cache_www = $www.'cache/images/'.$cachename;81 $origine_www = $www.$src;82 83 // Cache and make changes if necessary84 if( is_file($origine_path) && !is_file($cache_path) ) {85 $att = array('width'=>'', 'height'=>'', 'maxwidth'=>'', 'maxheight'=>'', 'zoom'=>'', 'alignh'=>'', 'alignv'=>'', 'ext'=>'', 'quality'=>'', 'shadow'=>'');86 if( count(array_intersect_key($params, $att)) )87 jtpl_function_html_image_inCache($src, $cachename, $params);88 }89 90 // Attributes91 $att = array('alt'=>'', 'id'=>'', 'class'=>'', 'style'=>'', 'longdesc'=>'', 'name'=>'', 'ismap'=>'', 'usemap'=>'', 'title'=>'', 'dir'=>'', 'lang'=>'', 'onclick'=>'', 'ondblclick'=>'', 'onmousedown'=>'', 'onmouseup'=>'', 'onmouseover'=>'', 'onmousemove'=>'', 'onmouseout'=>'', 'onkeypress'=>'', 'onkeydown'=>'', 'onkeyup'=>'');92 $att = array_intersect_key($params, $att);93 94 // If the image does not undergo transformation95 if( !is_file($cache_path) ) {96 $att['src'] = $origine_www;97 $att['style'] = empty($att['style'])?'':$att['style'];98 if( !empty($params['width']) ) $att['style'] .= 'width: '.$params['width'].'px;';99 else if( !empty($params['maxwidth']) ) $att['style'] .= 'width: '.$params['maxwidth'].'px;';100 if( !empty($params['height']) ) $att['style'] .= 'height: '.$params['height'].'px;';101 else if( !empty($params['maxheight']) ) $att['style'] .= 'height: '.$params['maxheight'].'px;';102 } else103 $att['src'] = $cache_www;104 105 71 // Tag image 106 echo '<img'; 107 foreach( $att as $key => $val ) 108 if( !empty($val) ) 109 echo ' '.$key.'="'.$val.'"'; 110 echo '/>'; 72 if( $params['notexists'] !== true || $cache->isFile() ) 73 echo $cache->inHtml(); 111 74 112 } 113 114 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', 118 'xpm'=>'image/x-xpixmap', 'xbm'=>'image/x-xbitmap', 'wbmp'=>'image/vnd.wap.wbmp'); 119 120 global $gJConfig; 121 $origine_www = 'http'.(empty($_SERVER['HTTPS'])?'':'s').'://'.$_SERVER['HTTP_HOST'].$gJConfig->urlengine['basePath'].$src; 122 123 $path_parts = pathinfo($origine_www); 124 $ext = $mimes[strtolower($path_parts['extension'])]; 125 $quality = (!empty($array['quality']))? $array['quality'] : 100; 126 127 // Creating an image 128 switch ( $ext ) { 129 case 'image/gif' : $image = imagecreatefromgif($origine_www); break; 130 case 'image/jpeg' : $image = imagecreatefromjpeg($origine_www); break; 131 case 'image/png' : $image = imagecreatefrompng($origine_www); break; 132 case 'image/vnd.wap.wbmp' : $image = imagecreatefromwbmp($origine_www); break; 133 case 'image/image/x-xbitmap' : $image = imagecreatefromxbm($origine_www); break; 134 case 'image/x-xpixmap' : $image = imagecreatefromxpm($origine_www); break; 135 default : return ; 136 } 137 138 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; 146 } else { 147 $array['width'] = $array['maxwidth']; 148 $array['height'] = imagesy($image)/$rapx; 149 } 150 } 151 152 if (!empty($array['width']) || !empty($array['height'])) { 153 154 $ancienimage = $image; 155 $resampleheight = imagesy($ancienimage); 156 $resamplewidth = imagesx($ancienimage); 157 $posx = 0; 158 $posy = 0; 159 160 if(empty($array['width'])) { 161 $finalheight = $array['height']; 162 $finalwidth = $finalheight*imagesx($ancienimage)/imagesy($ancienimage); 163 } else if (empty($array['height'])) { 164 $finalwidth = $array['width']; 165 $finalheight = $finalwidth*imagesy($ancienimage)/imagesx($ancienimage); 166 } else { 167 $finalwidth = $array['width']; 168 $finalheight = $array['height']; 169 if(!empty($array['omo']) && $array['omo'] == 'true') { 170 if($array['width'] >= $array['height']) { 171 $resampleheight = ( $resamplewidth*$array['height'] )/$array['width']; 172 } else { 173 $resamplewidth = ( $resampleheight*$array['width'] )/$array['height']; 174 } 175 } 176 } 177 178 if(!empty($array['zoom'])) { 179 $resampleheight /= 100/$array['zoom']; 180 $resamplewidth /= 100/$array['zoom']; 181 } 182 183 $posx = imagesx($ancienimage)/2 -$resamplewidth/2; 184 $posy = imagesy($ancienimage)/2 -$resampleheight/2; 185 186 if(!empty($array['alignh'])) { 187 if($array['alignh'] == 'left') $posx = 0; 188 else if($array['alignh'] == 'right') $posx = -($resamplewidth - imagesx($ancienimage)); 189 else if($array['alignh'] != 'center') $posx = -$array['alignh']; 190 } 191 192 if(!empty($array['alignv'])) { 193 if($array['alignv'] == 'top') $posy = 0; 194 else if($array['alignv'] == 'bottom') $posy = -($resampleheight - imagesy($ancienimage)); 195 else if($array['alignv'] != 'center') $posy = -$array['alignv']; 196 } 197 198 $image = imagecreatetruecolor($finalwidth, $finalheight); 199 imagesavealpha($image, true); 200 $tp = imagecolorallocatealpha($image,0,0,0,127); 201 imagefill($image,0,0,$tp); 202 203 imagecopyresampled($image, $ancienimage, 0, 0, $posx, $posy, imagesx($image), imagesy($image), $resamplewidth, $resampleheight); 204 } 205 206 // The shadow cast adds to the dimension of the image chooses 207 if( !empty($array['shadow']) ) 208 $image = jtpl_function_html_image_ombre ($image, $array); 209 210 // Background 211 if( !empty($array['background']) ) { 212 $array['background'] = str_replace('#', '', $array['background']); 213 $rgb = array(0,0,0); 214 for ($x=0;$x<3;$x++) $rgb[$x] = hexdec(substr($array['background'],(2*$x),2)); 215 $fond = imagecreatetruecolor(imagesx($image), imagesy($image)); 216 imagefill( $fond, 0, 0, imagecolorallocate( $fond, $rgb[0], $rgb[1], $rgb[2]) ); 217 imagecopy( $fond, $image, 0, 0, 0, 0, imagesx($image), imagesy($image)); 218 $image = $fond; 219 } 220 221 222 $ext = empty($array['ext'])?$ext:$mimes[$array['ext']]; 223 $cache_path = JELIX_APP_WWW_PATH.'cache/images/'; 224 jFile::createDir($cache_path); 225 226 227 // Register 228 switch ( $ext ) { 229 case 'image/gif' : imagegif($image, $cache_path.$cachename); break; 230 case 'image/jpeg' : imagejpeg($image, $cache_path.$cachename, $quality); break; 231 default : imagepng($image, $cache_path.$cachename); 232 } 233 234 // Destruction 235 @imagedestroy($image); 236 } 237 238 function jtpl_function_html_image_ombre ( $image, $array) { 239 240 // Default 241 $leng = isset($array['soffset'])?$array['soffset']:10; 242 $angle = isset($array['sangle'])?$array['sangle']:135; 243 $flou = isset($array['sblur'])?$array['sblur']:10; 244 $opac = isset($array['sopacity'])?$array['sopacity']:20; 245 $color = isset($array['scolor'])?$array['scolor']:'#000000'; 246 247 // Color of the shadow 248 $color = str_replace('#', '', $color); 249 $rgb = array(0,0,0); 250 if (strlen($color) == 6) 251 for ($x=0;$x<3;$x++) 252 $rgb[$x] = hexdec(substr($color,(2*$x),2)); 253 else if (strlen($color) == 3) 254 for ($x=0;$x<3;$x++) 255 $rgb[$x] = hexdec(substr($color,(2*$x),1)); 256 257 // Gaussian blur parameter 258 $coeffs = array (array ( 1), 259 array ( 1, 1), 260 array ( 1, 2, 1), 261 array ( 1, 3, 3, 1), 262 array ( 1, 4, 6, 4, 1), 263 array ( 1, 5, 10, 10, 5, 1), 264 array ( 1, 6, 15, 20, 15, 6, 1), 265 array ( 1, 7, 21, 35, 35, 21, 7, 1), 266 array ( 1, 8, 28, 56, 70, 56, 28, 8, 1), 267 array ( 1, 9, 36, 84, 126, 126, 84, 36, 9, 1), 268 array ( 1, 10, 45, 120, 210, 252, 210, 120, 45, 10, 1), 269 array ( 1, 11, 55, 165, 330, 462, 462, 330, 165, 55, 11, 1)); 270 $sum = pow (2, $flou); 271 $demi = $flou/2; 272 273 274 // Horizontal blur and blur margin 275 $temp1 = imagecreatetruecolor(imagesx($image)+$flou, imagesy($image)+$flou); 276 imagesavealpha($temp1, true); 277 $tp = imagecolorallocatealpha($temp1,0,0,0,127); 278 imagefill($temp1,0,0,$tp); 279 280 for ( $i=0 ; $i < imagesx($temp1) ; $i++ ) 281 for ( $j=0 ; $j < imagesy($temp1) ; $j++ ) { 282 $ig = $i-$demi; $jg = $j-$demi; $suma = 0; 283 for ( $k=0 ; $k <= $flou ; $k++ ) { 284 $ik = $ig-$demi+$k; 285 if( $jg<0 || $jg>imagesy($temp1)-$flou-1 ) $alpha = 127; 286 else if( $ik<0 || $ik>imagesx($temp1)-$flou-1 ) $alpha = 127; 287 else $alpha = (imagecolorat($image, $ik, $jg) & 0x7F000000) >> 24; 288 $suma += $alpha*$coeffs[$flou][$k]; 289 } 290 $c = imagecolorallocatealpha($temp1, 0, 0, 0, $suma/$sum ); 291 imagesetpixel($temp1,$i,$j,$c); 292 } 293 294 // Vertical blur, a shift of the angle, opacity and color 295 296 $x = cos(deg2rad($angle))*$leng; 297 $y = sin(deg2rad($angle))*$leng; 298 299 $temp2 = imagecreatetruecolor(imagesx($temp1)+abs($x), imagesy($temp1)+abs($y)); 300 imagesavealpha($temp2, true); 301 $tp = imagecolorallocatealpha($temp2,0,0,0,127); 302 imagefill($temp2,0,0,$tp); 303 304 $x1 = $x<0?0:$x; 305 $y1 = $y<0?0:$y; 306 307 for ( $i=0 ; $i < imagesx($temp1) ; $i++ ) 308 for ( $j=0 ; $j < imagesy($temp1) ; $j++ ) { 309 $suma = 0; 310 for ( $k=0 ; $k <= $flou ; $k++ ) { 311 $jk = $j-$demi+$k; 312 if( $jk<0 || $jk>imagesy($temp1)-1 ) $alpha = 127; 313 else $alpha = (imagecolorat($temp1, $i, $jk) & 0x7F000000) >> 24; 314 $suma += $alpha*$coeffs[$flou][$k]; 315 } 316 $alpha = 127-((127-($suma/$sum))/(100/$opac)); 317 $c = imagecolorallocatealpha($temp2, $rgb[0], $rgb[1], $rgb[2], $alpha < 0 ? 0 : $alpha > 127 ? 127 : $alpha ); 318 imagesetpixel($temp2,$i+$x1,$j+$y1,$c); 319 } 320 imagedestroy($temp1); 321 322 // Merge of the image and are shade 323 $x = $x>0?0:$x; 324 $y = $y>0?0:$y; 325 imagecopy( $temp2, $image, $demi-$x, $demi-$y, 0, 0, imagesx($image), imagesy($image)); 326 327 return $temp2; 328 329 } 330 ?> 75 } -
utils/jCacheImage.class.php
old new 1 <?php 2 /** 3 * @package jelix 4 * @subpackage utils 5 * @author Bastien Jaillot, Lepeltier kévin 6 * @copyright 2008 Bastien Jaillot, 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 9 */ 10 11 /** 12 * Add a jImage caching and exit html 13 * @package jelix 14 * @subpackage utils 15 * @static 16 */ 17 class jCacheImage extends jImage { 18 19 /** 20 * $CACHEPATH constant folder or store the images are cached 21 */ 22 public $CACHEPATH = 'cache/images/'; 23 24 /** 25 * $cache_path :string server path to the image caching 26 */ 27 public $cache_path; 28 29 /** 30 * $cache_www :string url of the image caching 31 */ 32 public $cache_www; 33 34 /** 35 * $cache_name :string name of the image caching 36 */ 37 public $cache_name; 38 39 /** 40 * $attributes :array List attributes xHtml associate has this image 41 */ 42 public $attributes = array(); 43 44 45 46 47 48 49 /** 50 * Prepares variable for caching 51 * @param $src Image URL 52 * @param $params for name in cache 53 */ 54 public function __construct( $src, $params ) { 55 56 parent::__construct( $src ); 57 58 // attributes de base 59 $this->attributes['src'] = $this->www; 60 $this->attributes['alt'] = ''; 61 62 $this->cache ( $params ); 63 64 } 65 66 /** 67 * Define the cache path 68 */ 69 public function cache ( $params ) { 70 71 $chaine = $this->src; 72 foreach($params as $key => $value) 73 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'))) 74 $chaine .= $key.$value; 75 $this->cache_name = md5($chaine); 76 $this->cache_path = JELIX_APP_WWW_PATH.$this->CACHEPATH; 77 $this->cache_www = $this->racine.$this->CACHEPATH; 78 79 } 80 81 /** 82 * check whether the image is cached 83 * @return boolean 84 */ 85 public function isFileInCache() { 86 return is_file( $this->cache_path.$this->cache_name.'.'.$this->extFinal ); 87 } 88 89 /** 90 * Replaces url by another if the file does not exist 91 * @param $srcbis Image URL 92 */ 93 public function alternate( $srcbis, $params = null ) { 94 if( !$this->isFile() && is_file( JELIX_APP_WWW_PATH.$srcbis ) ) 95 $this->src = $srcbis; 96 97 // Path 98 $this->path = JELIX_APP_WWW_PATH.$this->src; 99 $this->www = $this->racine.$this->src; 100 101 // extension 102 $path_parts = pathinfo($this->www); 103 $this->ext = strtolower($path_parts['extension']); 104 if( empty($this->extFinal) ) 105 $this->extFinal = $this->ext; 106 107 $this->cache ( $params ); 108 } 109 110 /** 111 * Adds attributes tying a tag img 112 * @param $array Liste of attributes 113 */ 114 public function addAttributes( $array ) { 115 116 $att = array('alt'=>'', 'id'=>'', 'class'=>'', 'style'=>'', 'longdesc'=>'', 'name'=>'', 'ismap'=>'', 'usemap'=>'', 'title'=>'', 'dir'=>'', 'lang'=>'', 'onclick'=>'', 'ondblclick'=>'', 'onmousedown'=>'', 'onmouseup'=>'', 'onmouseover'=>'', 'onmousemove'=>'', 'onmouseout'=>'', 'onkeypress'=>'', 'onkeydown'=>'', 'onkeyup'=>''); 117 $att = array_intersect_key($array, $att); 118 $this->attributes = array_merge($this->attributes, $att); 119 120 } 121 122 /** 123 * Return the tag html to display the image 124 * @return html tag img 125 */ 126 public function inHtml() { 127 128 // met en cache 129 if( $this->isFileInCache() ) 130 $this->attributes['src'] = $this->cache_www.$this->cache_name.'.'.$this->extFinal; 131 else if( !empty( $this->image ) ) 132 if( $this->sauveInAndDestroy( $this->cache_path, $this->cache_name ) ) 133 $this->attributes['src'] = $this->cache_www.$this->cache_name.'.'.$this->extFinal; 134 135 136 // fabrique la balise img 137 $ret = ''; 138 foreach( $this->attributes as $key => $val ) 139 if( !empty($val) || $key == 'alt' ) 140 $ret .= ' '.$key.'="'.htmlspecialchars($val).'"'; 141 return '<img'.$ret.'/>'; 142 143 } 144 145 146 /** 147 * Effetue transformations and caches 148 * @param $src Image URL 149 * @param $params for name in cache 150 */ 151 public static function inCache( $src, $params ) { 152 153 $cache = new jCacheImage( $src, $params ); 154 $cache->appliParams( $params ); 155 $cache->sauveInAndDestroy( $this->cache_path, $this->cache_name ); 156 return $this->cache_www.$this->cache_name.'.'.$this->extFinal; 157 158 } 159 160 161 162 } -
utils/jImage.class.php
old new 1 <?php 2 /** 3 * @package jelix 4 * @subpackage utils 5 * @author Lepeltier kévin 6 * @contributor Bastien Jaillot, Dominique Papin 7 * @copyright 2007-2008 Lepeltier kévin, 2008 Dominique Papin, 2008 Bastien Jaillot 8 * @link http://www.jelix.org 9 * @licence GNU Lesser General Public Licence see LICENCE file or http://www.gnu.org/licenses/lgpl.html 10 */ 11 12 /** 13 * Collection method for the transformation of image. 14 * @package jelix 15 * @subpackage utils 16 * @static 17 */ 18 class jImage { 19 20 /** 21 * $origine_src :string url of the image requested on the folder /www/ 22 */ 23 public $origine_src; 24 25 /** 26 * $src :string url of the image undergoing transformations on the folder /www/ 27 */ 28 public $src; 29 30 /** 31 * $ext :string extension of the image 32 */ 33 public $ext; 34 35 /** 36 * $ext :string extension of the final image 37 */ 38 public $extFinal; 39 40 /** 41 * $root :string URL for the file /www/ 42 */ 43 public $root; 44 45 /** 46 * $path :string server path to the image 47 */ 48 public $path; 49 50 /** 51 * $www :string absolute url image undergoing transformations 52 */ 53 public $www; 54 55 /** 56 * $mimes :array Table types mimes supported 57 */ 58 private $mimes = array('gif'=>'image/gif', 'png'=>'image/png', 59 'jpeg'=>'image/jpeg', 'jpg'=>'image/jpeg', 'jpe'=>'image/jpeg', 60 'xpm'=>'image/x-xpixmap', 'xbm'=>'image/x-xbitmap', 'wbmp'=>'image/vnd.wap.wbmp'); 61 62 /** 63 * $image variable containing the image change 64 */ 65 public $image; 66 67 68 public $resampleheight; 69 public $resamplewidth; 70 public $finalheight; 71 public $finalwidth; 72 public $posx; 73 public $posy; 74 75 /** 76 * $quality :int qualiter jpeg de l'image final 77 */ 78 public $quality = 100; 79 80 /** 81 * $background Background color, if undefined, the background is transparent 82 */ 83 public $background; 84 85 /** 86 * Takes parameter in the URL of the image on file /www/ 87 * and prepares variable for image processing 88 * @param $src Image URL 89 */ 90 public function __construct( $src ) { 91 92 $this->origine_src = $src; 93 $this->src = $src; 94 95 // Path 96 global $gJConfig; 97 $this->root = 'http'.(empty($_SERVER['HTTPS'])?'':'s').'://'.$_SERVER['HTTP_HOST'].$gJConfig->urlengine['basePath']; 98 $this->path = JELIX_APP_WWW_PATH.$this->src; 99 $this->www = $this->root.$this->src; 100 101 // extension 102 $path_parts = pathinfo($this->www); 103 $this->ext = strtolower($path_parts['extension']); 104 if( empty($this->extFinal) ) 105 $this->extFinal = $this->ext; 106 107 } 108 109 /** 110 * Replaces url by another if the file does not exist 111 * @param $srcbis Image URL 112 */ 113 public function alternate( $srcbis ) { 114 if( !$this->isFile() && is_file( JELIX_APP_WWW_PATH.$srcbis ) ) 115 $this->src = $srcbis; 116 } 117 118 /** 119 * check if the requested image exist 120 * @return boolean 121 */ 122 public function isFile() { 123 return is_file( JELIX_APP_WWW_PATH.$this->origine_src ); 124 } 125 126 /** 127 * Place the image in the variable image 128 * @return boolean 129 */ 130 private function getImage() { 131 132 // Creating an image 133 if( is_file( $this->path ) ) { 134 $extmime = $this->mimes[$this->ext]; 135 switch ( $extmime ) { 136 case 'image/gif' : $this->image = imagecreatefromgif($this->www); break; 137 case 'image/jpeg' : $this->image = imagecreatefromjpeg($this->www); break; 138 case 'image/png' : $this->image = imagecreatefrompng($this->www); break; 139 case 'image/vnd.wap.wbmp' : $this->image = imagecreatefromwbmp($this->www); break; 140 case 'image/image/x-xbitmap' : $this->image = imagecreatefromxbm($this->www); break; 141 case 'image/x-xpixmap' : $this->image = imagecreatefromxpm($this->www); break; 142 default : return false; 143 } 144 145 $this->resampleheight = $this->finalheight = imagesy($this->image); 146 $this->resamplewidth = $this->finalwidth = imagesx($this->image); 147 $this->posx = 0; 148 $this->posy = 0; 149 150 return true; 151 } else return false; 152 153 } 154 155 /** 156 * Returns a new image transparent 157 * @return $image 158 * @param $width width of the image 159 * @param $height height of the image 160 */ 161 private function newImage( $width, $height) { 162 163 $image = imagecreatetruecolor($width, $height); 164 imagesavealpha( $image, true ); 165 $tp = imagecolorallocatealpha( $image, 0, 0, 0, 127 ); 166 imagefill( $image, 0, 0, $tp ); 167 return $image; 168 169 } 170 171 172 173 174 175 176 /** 177 * Resizes the image keeping the proportions 178 * 179 * width :uint 180 * height :uint 181 * maxwidth :uint only with maxheight 182 * maxheight :uint only with maxwidth 183 * 184 * This function makes a center alignment 185 * 186 * @param $params parameter for resizing 187 */ 188 public function omotiResize( $params ) { 189 190 if( !empty($this->image) || $this->getImage() ) { 191 192 if(!empty($params['maxwidth']) && !empty($params['maxheight'])) { 193 $params['height'] = null; 194 $params['width'] = null; 195 $ratio = imagesx($this->image)/imagesy($this->image); 196 if( $ratio > 1 ) $params['width'] = $params['maxwidth']; 197 if( $ratio < 1 ) $params['height'] = $params['maxheight']; 198 } 199 200 if ( empty($params['width']) ) { 201 $this->finalheight = $params['height']; 202 $this->finalwidth = $this->finalheight*$this->resamplewidth/$this->resampleheight; 203 } else if ( empty($params['height']) ) { 204 $this->finalwidth = $params['width']; 205 $this->finalheight = $this->finalwidth *$this->resampleheight/$this->resamplewidth; 206 } else { 207 $this->finalwidth = $params['width']; 208 $this->finalheight = $params['height']; 209 if( $this->finalwidth >= $this->finalheight ) 210 $this->resampleheight = ( $this->resamplewidth *$this->finalheight )/$this->finalwidth; 211 else $this->resamplewidth = ( $this->resampleheight*$this->finalwidth )/$this->finalheight; 212 } 213 214 $this->posx = imagesx($this->image)/2 -$this->resamplewidth/2; 215 $this->posy = imagesy($this->image)/2 -$this->resampleheight/2; 216 217 } 218 219 } 220 221 /** 222 * Resizes the image without keeping the homotétie 223 * 224 * width :uint 225 * height :uint 226 * 227 * @param $params parameter for resizing 228 */ 229 public function resize( $params ) { 230 231 if( !empty($this->image) || $this->getImage() ) { 232 233 $this->resampleheight = imagesy($this->image); 234 $this->resamplewidth = imagesx($this->image); 235 $this->finalwidth = $params['width']; 236 $this->finalheight = $params['height']; 237 238 } 239 240 } 241 242 /** 243 * Zooms into a picture 244 * This function makes a center alignment 245 * @param $zoom zoom value 1-100 246 */ 247 public function zoom( $zoom ) { 248 249 if( !empty($this->image) || $this->getImage() ) { 250 251 $this->resampleheight /= 100/$zoom; 252 $this->resamplewidth /= 100/$zoom; 253 254 $this->posx = imagesx($this->image)/2 -$this->resamplewidth/2; 255 $this->posy = imagesy($this->image)/2 -$this->resampleheight/2; 256 257 } 258 259 } 260 261 /** 262 * Amendment to the alignment of the image 263 * 264 * alignh [left|center|right|:int] 265 * alignv [top|center|bottom|:int] 266 * 267 * @param $params parameter for alignment 268 */ 269 public function align( $params ) { 270 271 if( !empty($this->image) || $this->getImage() ) { 272 273 $this->posx = imagesx($this->image)/2 -$this->resamplewidth/2; 274 $this->posy = imagesy($this->image)/2 -$this->resampleheight/2; 275 276 if(!empty($params['alignh'])) { 277 if($params['alignh'] == 'left') $this->posx = 0; 278 else if($params['alignh'] == 'right') $this->posx = -($this->resamplewidth - imagesx($this->image)); 279 else if($params['alignh'] != 'center') $this->posx = -$params['alignh']; 280 } 281 282 if(!empty($params['alignv'])) { 283 if($params['alignv'] == 'top') $this->posy = 0; 284 else if($params['alignv'] == 'bottom') $this->posy = -($this->resampleheight - imagesy($this->image)); 285 else if($params['alignv'] != 'center') $this->posy = -$params['alignv']; 286 } 287 288 } 289 290 } 291 292 /** 293 * Change the extension of the image 294 * @param $ext the new extension [png|jpg|gif] 295 */ 296 public function changeExt( $ext ) { 297 298 if( !empty($this->image) || $this->getImage() ) { 299 300 $this->extFinal = $ext; 301 302 } 303 304 } 305 306 /** 307 * Change the qualiter the jpeg image 308 * @param $quality 0-100 if ext = jpg 309 */ 310 public function changeQuality( $quality ) { 311 312 if( !empty($this->image) || $this->getImage() ) { 313 314 $this->quality = $quality; 315 316 } 317 318 } 319 320 /** 321 * Change or add a background color 322 * @param $color the new color #000000 :string 323 */ 324 public function background( $color ) { 325 326 if( !empty($this->image) || $this->getImage() ) { 327 328 $this->background = $this->hexaToRgb( $color ); 329 330 } 331 332 } 333 334 /** 335 * Adds a shadow 336 * 337 * soffset :uint 338 * sangle :uint 339 * sblur :uint 340 * sopacity :uint 341 * scolor #000000 :string 342 * 343 * The shadow increases the size of the final image 344 * 345 * @param $params 346 */ 347 public function shadow( $params ) { 348 349 if( !empty($this->image) || $this->getImage() ) { 350 351 // Default 352 $leng = isset($params['soffset']) ?$params['soffset'] :10; 353 $angle = isset($params['sangle']) ?$params['sangle'] :135; 354 $flou = isset($params['sblur']) ?$params['sblur'] :10; 355 $opac = isset($params['sopacity'])?$params['sopacity']:20; 356 $color = isset($params['scolor']) ?$params['scolor'] :'#000000'; 357 358 // Color of the shadow 359 $rgb = $this->hexaToRgb( $color ); 360 361 // Gaussian blur parameter 362 $coeffs = array (array ( 1), 363 array ( 1, 1), 364 array ( 1, 2, 1), 365 array ( 1, 3, 3, 1), 366 array ( 1, 4, 6, 4, 1), 367 array ( 1, 5, 10, 10, 5, 1), 368 array ( 1, 6, 15, 20, 15, 6, 1), 369 array ( 1, 7, 21, 35, 35, 21, 7, 1), 370 array ( 1, 8, 28, 56, 70, 56, 28, 8, 1), 371 array ( 1, 9, 36, 84, 126, 126, 84, 36, 9, 1), 372 array ( 1, 10, 45, 120, 210, 252, 210, 120, 45, 10, 1), 373 array ( 1, 11, 55, 165, 330, 462, 462, 330, 165, 55, 11, 1)); 374 $sum = pow (2, $flou); 375 $demi = $flou/2; 376 377 $imagebis = $this->newImage( $this->finalwidth, $this->finalheight ); 378 imagecopyresampled( $imagebis, $this->image, 0, 0, $this->posx, $this->posy, $this->finalwidth, $this->finalheight, $this->resamplewidth, $this->resampleheight ); 379 380 // Horizontal blur and blur margin 381 $temp1 = $this->newImage( $this->finalwidth+$flou, $this->finalheight+$flou ); 382 383 for ( $i=0 ; $i < imagesx($temp1) ; $i++ ) 384 for ( $j=0 ; $j < imagesy($temp1) ; $j++ ) { 385 $ig = $i-$demi; $jg = $j-$demi; $suma = 0; 386 for ( $k=0 ; $k <= $flou ; $k++ ) { 387 $ik = $ig-$demi+$k; 388 if( $jg<0 || $jg>imagesy($temp1)-$flou-1 ) $alpha = 127; 389 else if( $ik<0 || $ik>imagesx($temp1)-$flou-1 ) $alpha = 127; 390 else $alpha = (imagecolorat($imagebis, $ik, $jg) & 0x7F000000) >> 24; 391 $suma += $alpha*$coeffs[$flou][$k]; 392 } 393 $c = imagecolorallocatealpha($temp1, 0, 0, 0, $suma/$sum ); 394 imagesetpixel($temp1,$i,$j,$c); 395 } 396 397 // Vertical blur, a shift of the angle, opacity and color 398 399 $x = cos(deg2rad($angle))*$leng; 400 $y = sin(deg2rad($angle))*$leng; 401 402 $this->shadow = $this->newImage( imagesx($temp1)+abs($x), imagesy($temp1)+abs($y) ); 403 404 $x1 = $x<0?0:$x; 405 $y1 = $y<0?0:$y; 406 407 for ( $i=0 ; $i < imagesx($temp1) ; $i++ ) 408 for ( $j=0 ; $j < imagesy($temp1) ; $j++ ) { 409 $suma = 0; 410 for ( $k=0 ; $k <= $flou ; $k++ ) { 411 $jk = $j-$demi+$k; 412 if( $jk<0 || $jk>imagesy($temp1)-1 ) $alpha = 127; 413 else $alpha = (imagecolorat($temp1, $i, $jk) & 0x7F000000) >> 24; 414 $suma += $alpha*$coeffs[$flou][$k]; 415 } 416 $alpha = 127-((127-($suma/$sum))/(100/$opac)); 417 $c = imagecolorallocatealpha($this->shadow, $rgb[0], $rgb[1], $rgb[2], $alpha < 0 ? 0 : $alpha > 127 ? 127 : $alpha ); 418 imagesetpixel($this->shadow,$i+$x1,$j+$y1,$c); 419 } 420 imagedestroy($temp1); 421 422 // Merge of the image and are shade 423 $x = $x>0?0:$x; 424 $y = $y>0?0:$y; 425 $this->shadowPosx = $demi-$x; 426 $this->shadowPosy = $demi-$y; 427 428 } 429 430 } 431 432 433 434 /** 435 * Convert the chaine #0100ff to array(1, 0, 255) 436 * @return array(0, 0, 0) 437 * @param $color #000000 438 */ 439 public function hexaToRgb( $color ) { 440 441 $color = str_replace('#', '', $color); 442 $rgb = array(0,0,0); 443 if (strlen($color) == 6) 444 for ($x=0;$x<3;$x++) 445 $rgb[$x] = hexdec(substr($color,(2*$x),2)); 446 else if (strlen($color) == 3) 447 for ($x=0;$x<3;$x++) 448 $rgb[$x] = hexdec(substr($color,(2*$x),1)); 449 return $rgb; 450 451 } 452 453 /** 454 * Sauve The image in file and destroy this 455 * @return boolean success 456 * @param $url Image URL 457 * @param $name Image Name 458 */ 459 public function saveInAndDestroyed( $url, $name ) { 460 461 $ok = $this->sauveIn( $url, $name ); 462 @imagedestroy( $this->image ); 463 return $ok; 464 465 } 466 467 /** 468 * Sauve The image in file 469 * @return boolean success 470 * @param $url Image URL 471 * @param $name Image Name 472 */ 473 public function sauveIn( $url, $name ) { 474 475 $image = $this->finalise(); 476 477 jFile::createDir($url); 478 $extmime = $this->mimes[$this->extFinal]; 479 switch ( $extmime ) { 480 case 'image/gif' : $ok = imagegif ($image, $url.$name.'.'.$this->extFinal); break; 481 case 'image/jpeg' : $ok = imagejpeg ($image, $url.$name.'.'.$this->extFinal, $this->quality); break; 482 default : $ok = imagepng ($image, $url.$name.'.'.$this->extFinal); 483 } 484 return $ok; 485 486 } 487 488 /** 489 * Create the final image and return this 490 * @return $image 491 */ 492 public function finalise() { 493 494 $oldimage = $this->image; 495 $image = $this->newImage( $this->finalwidth, $this->finalheight ); 496 imagecopyresampled( $image, $oldimage, 0, 0, $this->posx, $this->posy, $this->finalwidth, $this->finalheight, $this->resamplewidth, $this->resampleheight ); 497 if( !empty( $this->shadow ) ) { 498 $shadow = $this->shadow; 499 imagecopy( $shadow, $image, $this->shadowPosx, $this->shadowPosy, 0, 0, imagesx($image), imagesy($image)); 500 $image = $shadow; 501 } 502 if( !empty($this->background) ) { 503 $fond = imagecreatetruecolor(imagesx($image), imagesy($image)); 504 imagefill( $fond, 0, 0, imagecolorallocate( $fond, $this->background[0], $this->background[1], $this->background[2]) ); 505 imagecopy( $fond, $image, 0, 0, 0, 0, imagesx($image), imagesy($image)); 506 $image = $fond; 507 } 508 509 return $image; 510 } 511 512 /** 513 * Apply transformations past parameters 514 * @param $params for transforme 515 */ 516 public function appliParams( $params ) { 517 518 // homothetic resize 519 if( ( !empty($params['width']) XOR !empty($params['height']) ) 520 || ( !empty($params['width']) && !empty($params['height']) && !empty($params['omo']) && $params['omo'] ) 521 || ( !empty($params['maxwidth']) && !empty($params['maxheight']) ) ) 522 $this->omotiResize( $params ); 523 // non homothetic resize 524 else 525 if( !empty($params['width']) && !empty($params['height']) ) 526 $this->resize( $params ); 527 528 // zoom 529 if( !empty($params['zoom']) ) 530 $this->zoom( $params['zoom'] );
