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