Ticket #604: jImage_pres.diff

File jImage_pres.diff, 32.3 kB (added by Lipki, 6 months ago)
  • plugins/tpl/html/function.image.php

    old new  
    3434 * sopacity :uint 
    3535 * scolor #000000 :string 
    3636 * background #000000 :string 
     37 * notexists :boolean [true|false|alternate uri] 
    3738 * 
    3839 * gif   -> image/gif 
    3940 * jpeg  -> image/jpeg 
     
    5152 */ 
    5253function jtpl_function_html_image($tpl, $src, $params=array()) { 
    5354     
    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; 
     57    $params['force'] = true; 
     58         
     59        // cache 
     60        $cache = new jCacheImage( $src, $params ); 
     61         
     62        // url de alternative 
     63        if( is_string($params['notexists']) ) 
     64           $cache->alternate( $params['notexists'] ); 
     65         
    5966     
    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     } 
    66      
    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; 
    73      
    74     // Path 
    75     $cache_path = JELIX_APP_WWW_PATH.'cache/images/'.$cachename; 
    76     $origine_path = JELIX_APP_WWW_PATH.$src; 
    77      
    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 necessary 
    84     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     // Attributes 
    91     $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 transformation 
    95     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     } else 
    103         $att['src'] = $cache_www; 
    104      
     67            
     68    // homothetic resize 
     69    if( ( !empty($params['width']) XOR !empty($params['height']) ) 
     70         || ( !empty($params['width']) && !empty($params['height']) && !empty($params['omo']) && $params['omo'] ) ) 
     71       $cache->omotiResize( $params ); 
     72        // non homothetic resize 
     73        else 
     74           if( !empty($params['width']) && !empty($params['height']) ) 
     75           $cache->resize( $params ); 
     76        
     77    // zoom 
     78    if( !empty($params['zoom']) ) 
     79       $cache->zoom( $params['zoom'] ); 
     80            
     81    // alignement 
     82    if( !empty($params['alignh']) || !empty($params['alignv']) ) 
     83       $cache->align( $params ); 
     84         
     85    // extension 
     86        if( !empty($params['ext']) ) 
     87        $cache->changeExt($params['ext']); 
     88         
     89    // quality 
     90    if( !empty($params['quality']) ) 
     91        $cache->changeQuality($params['quality']); 
     92                 
     93    // shadow 
     94    if( !empty($params['shadow']) ) 
     95        $cache->shadow($params); 
     96         
     97        // attributs 
     98        $cache->addAttributs( $params ); 
     99         
    105100    // Tag image 
    106     echo '<img'; 
    107     foreach( $att as $key => $val ) 
    108         if( !empty($val) ) 
    109             echo ' '.$key.'="'.$val.'"'; 
    110     echo '/>'; 
     101        if( $params['notexists'] !== true || $cache->isFile() ) 
     102                echo $cache->inHtml(); 
    111103     
    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); 
    236104} 
    237105 
    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; 
    328106 
    329 
    330 ?> 
     107 
     108    // Extension 
     109    // White background for IE 
     110    // if not exists 
     111    // Name of the file cache 
     112    // Path 
     113    // Cache and make changes if necessary 
     114    // Attributes 
     115    // If the image does not undergo transformation 
  • utils/jCacheImage.class.php

    old new  
     1<?php 
     2/**  
     3 * @package    jelix  
     4 * @subpackage utils  
     5 * @author      Bastien Jaillot  
     6 * @contributor Dominique Papin, Lepeltier kévin (the author of the original plugin)  
     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 * Utility class to create cache image 
     14 * @package    jelix 
     15 * @subpackage utils 
     16 * @static 
     17 */ 
     18class jCacheImage extends jImage { 
     19     
     20    /** 
     21     * $CACHEPATH constante le dossier ou sont stocker les images en cache 
     22     */ 
     23    public $CACHEPATH = 'cache/images/'; 
     24     
     25    /** 
     26     * $cache_path :string chemin serveur vers l'image mise en cache 
     27     */ 
     28    public $cache_path; 
     29         
     30    /** 
     31     * $cache_www :string url de l'image mise en cache 
     32     */ 
     33    public $cache_www; 
     34     
     35    /** 
     36     * $cache_name :string nom de l'image mise en cache 
     37     */ 
     38    public $cache_name; 
     39     
     40    /** 
     41     * $attributs :array Liste des attributs xHtml associer a cette image 
     42     */ 
     43    public $attributs = array(); 
     44         
     45         
     46         
     47         
     48         
     49         
     50         
     51    public function __construct( $src, $params ) { 
     52         
     53                $this->params = $params; 
     54        parent::__construct( $src ); 
     55         
     56                // attributs de base 
     57                $this->attributs['src'] = $this->www; 
     58        $this->attributs['alt'] = ''; 
     59                 
     60                $this->cache (); 
     61                 
     62    } 
     63         
     64        /** 
     65         *  
     66         * @return  
     67         */ 
     68        public function cache () { 
     69                 
     70                $chaine = $this->src; 
     71        foreach($this->params as $key => $value) 
     72            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'))) 
     73                $chaine .= $key.$value; 
     74        $this->cache_name = md5($chaine); 
     75        $this->cache_path = JELIX_APP_WWW_PATH.$this->CACHEPATH; 
     76        $this->cache_www = $this->racine.$this->CACHEPATH; 
     77                 
     78        } 
     79     
     80    /** 
     81     * Remplace l'url par un autre si le fichier n'éxiste pas 
     82     */ 
     83    public function alternate( $srcbis ) { 
     84        if( !$this->isFile() && is_file( JELIX_APP_WWW_PATH.$srcbis ) ) 
     85            $this->src = $srcbis; 
     86                 
     87                // Path 
     88        $this->path = JELIX_APP_WWW_PATH.$this->src; 
     89        $this->www = $this->racine.$this->src; 
     90         
     91        // extension 
     92        $path_parts = pathinfo($this->www); 
     93        $this->ext = strtolower($path_parts['extension']); 
     94         
     95        $this->cache (); 
     96    } 
     97         
     98        /** 
     99         *  
     100         * @return  
     101         */ 
     102        public function addAttributs( $array ) { 
     103                 
     104                $att = array('alt'=>'', 'id'=>'', 'class'=>'', 'style'=>'', 'longdesc'=>'', 'name'=>'', 'ismap'=>'', 'usemap'=>'', 'title'=>'', 'dir'=>'', 'lang'=>'', 'onclick'=>'', 'ondblclick'=>'', 'onmousedown'=>'', 'onmouseup'=>'', 'onmouseover'=>'', 'onmousemove'=>'', 'onmouseout'=>'', 'onkeypress'=>'', 'onkeydown'=>'', 'onkeyup'=>''); 
     105        $att = array_intersect_key($array, $att); 
     106                $this->attributs = array_merge($this->attributs, $att); 
     107                 
     108        } 
     109         
     110        /** 
     111     * retourne la balise html pour afficher l'image 
     112     * @return  
     113     */ 
     114    public function inHtml() { 
     115         
     116        // met en cache 
     117        if( !empty( $this->image ) ) { 
     118                echo $this->cache_path; 
     119            if( $this->sauveInAndDestroy( $this->cache_path, $this->cache_name ) ) 
     120                $this->attributs['src'] = $this->cache_www.$this->cache_name.'.'.$this->extFinal; 
     121        } 
     122         
     123         
     124        // fabrique la balise img 
     125        $ret = ''; 
     126        foreach( $this->attributs as $key => $val ) 
     127            if( !empty($val) || $key == 'alt' ) 
     128                $ret .= ' '.$key.'="'.$val.'"'; 
     129        return '<img'.$ret.'/>'; 
     130         
     131    } 
     132         
     133         
     134         
     135} 
  • utils/jImage.class.php

    old new  
     1<?php 
     2/**  
     3 * @package    jelix  
     4 * @subpackage utils  
     5 * @author      Bastien Jaillot  
     6 * @contributor Dominique Papin, Lepeltier kévin (the author of the original plugin)  
     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 * Utility class to create cache image 
     14 * @package    jelix 
     15 * @subpackage utils 
     16 * @static 
     17 */ 
     18class jImage { 
     19         
     20     
     21    /** 
     22     * $origine_src :string url de l'image demandée relative au dossier /www/ 
     23     */ 
     24    public $origine_src; 
     25         
     26    /** 
     27     * $src :string url de l'image qui subit les transformations relative au dossier /www/ 
     28     */ 
     29    public $src; 
     30         
     31    /** 
     32     * $ext :string extension de l'image 
     33     */ 
     34    public $ext; 
     35         
     36    /** 
     37     * $ext :string extension de l'image final 
     38     */ 
     39    public $extFinal; 
     40         
     41        /** 
     42         * $racine :string url du dossier /www/ 
     43         */ 
     44        public $racine; 
     45         
     46        /** 
     47         * $path :string chemin serveur vers l'image 
     48         */ 
     49        private $path; 
     50         
     51    /** 
     52     * $www :string url de l'image qui subit les transformations 
     53     */ 
     54    public $www; 
     55         
     56         
     57         
     58         
     59         
     60        /** 
     61         * $mimes :array tableau des types mimes 
     62         */ 
     63        private $mimes = array('gif'=>'image/gif', 'png'=>'image/png',  
     64                           'jpeg'=>'image/jpeg', 'jpg'=>'image/jpeg', 'jpe'=>'image/jpeg',  
     65                           'xpm'=>'image/x-xpixmap', 'xbm'=>'image/x-xbitmap', 'wbmp'=>'image/vnd.wap.wbmp'); 
     66         
     67        /** 
     68         * $image variable contenant l'image modifier 
     69         */ 
     70        public $image; 
     71         
     72         
     73        /** 
     74     * $quality :int qualiter jpeg de l'image final 
     75     */ 
     76        public $quality = 100; 
     77         
     78        /** 
     79         * prend en paramêtre l'url de l'image et de l'image alternative relative au dossier /www/ 
     80         * @return  
     81         * @param $src url de l'image 
     82         * @param $orsrc url de l'image alternative 
     83         */ 
     84    public function __construct( $src ) { 
     85         
     86        $this->origine_src = $src; 
     87        $this->src = $src; 
     88         
     89            // Path 
     90        global $gJConfig; 
     91        $this->racine = 'http'.(empty($_SERVER['HTTPS'])?'':'s').'://'.$_SERVER['HTTP_HOST'].$gJConfig->urlengine['basePath']; 
     92            $this->path = JELIX_APP_WWW_PATH.$this->src; 
     93        $this->www = $this->racine.$this->src; 
     94                 
     95                // extension 
     96        $path_parts = pathinfo($this->www); 
     97        $this->ext = strtolower($path_parts['extension']); 
     98                 
     99    } 
     100     
     101    /** 
     102     * Remplace l'url par un autre si le fichier n'éxiste pas 
     103     */ 
     104    public function alternate( $srcbis ) { 
     105        if( !$this->isFile() && is_file( JELIX_APP_WWW_PATH.$srcbis ) ) 
     106            $this->src = $srcbis; 
     107    } 
     108     
     109    /** 
     110     * vérifie si l'image demandé existe 
     111     * @return boolean 
     112     */ 
     113    public function isFile() { 
     114        return is_file( JELIX_APP_WWW_PATH.$this->origine_src ); 
     115    } 
     116         
     117        /** 
     118         * Place l'image dans la variable image 
     119         * @return boolean 
     120         */ 
     121        private function getImage() { 
     122                 
     123            // Creating an image 
     124                if( is_file( $this->path ) ) { 
     125                        $extmime = $this->mimes[$this->ext]; 
     126                        switch ( $extmime ) { 
     127                        case 'image/gif'             : $this->image = imagecreatefromgif($this->www); break; 
     128                        case 'image/jpeg'            : $this->image = imagecreatefromjpeg($this->www); break; 
     129                        case 'image/png'             : $this->image = imagecreatefrompng($this->www); break; 
     130                        case 'image/vnd.wap.wbmp'    : $this->image = imagecreatefromwbmp($this->www); break; 
     131                        case 'image/image/x-xbitmap' : $this->image = imagecreatefromxbm($this->www); break; 
     132                        case 'image/x-xpixmap'       : $this->image = imagecreatefromxpm($this->www); break; 
     133                        default                      : return false; 
     134                    } 
     135                         
     136                        $this->resampleheight = $this->finalheight = imagesy($this->image); 
     137            $this->resamplewidth  = $this->finalwidth  = imagesx($this->image); 
     138            $this->posx = 0; 
     139            $this->posy = 0; 
     140                         
     141                        return true; 
     142                } else return false; 
     143                 
     144        } 
     145         
     146        /** 
     147     * retourne une nouvelle image transparente 
     148     * @return 
     149     */ 
     150    private function newImage( $width, $height) { 
     151         
     152        $image = imagecreatetruecolor($width, $height); 
     153        imagesavealpha( $image, true ); 
     154        $tp = imagecolorallocatealpha( $image, 0, 0, 0, 127 ); 
     155        imagefill( $image, 0, 0, $tp ); 
     156                return $image; 
     157         
     158    } 
     159         
     160         
     161         
     162         
     163         
     164         
     165        /** 
     166         * Redimensionne l'image en gardant les proportions 
     167         * @param $params paramêtre pour le redimensionnement 
     168         */ 
     169        public function omotiResize( $params ) { 
     170                 
     171                if( !empty($this->image) || $this->getImage() ) { 
     172                         
     173                        if        ( empty($params['width']) ) { 
     174                $this->finalheight = $params['height']; 
     175                $this->finalwidth  = $this->finalheight*$this->resamplewidth/$this->resampleheight; 
     176                    } else if ( empty($params['height']) ) { 
     177                $this->finalwidth  = $params['width']; 
     178                $this->finalheight = $this->finalwidth *$this->resampleheight/$this->resamplewidth; 
     179                } else { 
     180                        $this->finalwidth  = $params['width']; 
     181                    $this->finalheight = $params['height']; 
     182                    if( $this->finalwidth >= $this->finalheight ) 
     183                         $this->resampleheight = ( $this->resamplewidth *$this->finalheight )/$this->finalwidth; 
     184                    else $this->resamplewidth  = ( $this->resampleheight*$this->finalwidth  )/$this->finalheight; 
     185                } 
     186             
     187            $this->posx = imagesx($this->image)/2 -$this->resamplewidth/2; 
     188            $this->posy = imagesy($this->image)/2 -$this->resampleheight/2; 
     189                         
     190                } 
     191                 
     192        } 
     193         
     194        /** 
     195     * Redimensionne l'image sans garder l'homotétie 
     196     * @param $params paramêtre pour le redimensionnement 
     197     */ 
     198    public function resize( $params ) { 
     199         
     200        if( !empty($this->image) || $this->getImage() ) { 
     201             
     202            $this->resampleheight = imagesy($this->image); 
     203            $this->resamplewidth = imagesx($this->image); 
     204            $this->finalwidth = $params['width']; 
     205            $this->finalheight = $params['height']; 
     206             
     207        } 
     208         
     209    } 
     210     
     211    /** 
     212     * effectue un zoom sur l'image 
     213     * @param $zoom valeur du zoom 
     214     */ 
     215    public function zoom( $zoom ) { 
     216         
     217        if( !empty($this->image) || $this->getImage() ) { 
     218             
     219                $this->resampleheight /= 100/$zoom; 
     220                $this->resamplewidth /= 100/$zoom; 
     221             
     222            $this->posx = imagesx($this->image)/2 -$this->resamplewidth/2; 
     223            $this->posy = imagesy($this->image)/2 -$this->resampleheight/2; 
     224             
     225        } 
     226         
     227    } 
     228     
     229    /** 
     230     * effectue un zoom sur l'image 
     231     * @param $params 
     232     */ 
     233    public function align( $params ) { 
     234         
     235        if( !empty($this->image) || $this->getImage() ) { 
     236             
     237            $this->posx = imagesx($this->image)/2 -$this->resamplewidth/2; 
     238            $this->posy = imagesy($this->image)/2 -$this->resampleheight/2; 
     239                 
     240                if(!empty($params['alignh'])) { 
     241                    if($params['alignh'] == 'left')          $this->posx = 0; 
     242                    else if($params['alignh'] == 'right')    $this->posx = -($this->resamplewidth - imagesx($this->image)); 
     243                    else if($params['alignh'] != 'center')   $this->posx = -$params['alignh']; 
     244                } 
     245                 
     246                if(!empty($params['alignv'])) { 
     247                    if($params['alignv'] == 'top')           $this->posy = 0; 
     248                    else if($params['alignv'] == 'bottom')   $this->posy = -($this->resampleheight - imagesy($this->image)); 
     249                    else if($params['alignv'] != 'center')   $this->posy = -$params['alignv']; 
     250                } 
     251             
     252        } 
     253         
     254    } 
     255     
     256    /** 
     257     * Change l'extension de l'image 
     258     * @param $ext la nouvelle extension 
     259     */ 
     260    public function changeExt( $ext ) { 
     261         
     262        if( !empty($this->image) || $this->getImage() ) { 
     263             
     264            $this->extFinal = $ext; 
     265             
     266        } 
     267         
     268    } 
     269     
     270    /** 
     271     * Change la qualiter de l'image jpeg 
     272     * @param $ext la nouvelle extension 
     273     */ 
     274    public function changeQuality( $quality ) { 
     275         
     276        if( !empty($this->image) || $this->getImage() ) { 
     277             
     278            $this->quality = $quality; 
     279             
     280        } 
     281         
     282    } 
     283     
     284    /** 
     285     * Ajoute une ombre portée 
     286     * @param $params 
     287     */ 
     288    public function shadow( $params ) { 
     289         
     290        if( !empty($this->image) || $this->getImage() ) { 
     291             
     292            // Default 
     293                    $leng  = isset($params['soffset']) ?$params['soffset'] :10; 
     294                    $angle = isset($params['sangle'])  ?$params['sangle']  :135; 
     295                    $flou  = isset($params['sblur'])   ?$params['sblur']   :10; 
     296                    $opac  = isset($params['sopacity'])?$params['sopacity']:20; 
     297                    $color = isset($params['scolor'])  ?$params['scolor']  :'#000000'; 
     298                         
     299                        // Color of the shadow 
     300                    $rgb = $this->hexaToRgb( $color ); 
     301                         
     302                        // Gaussian blur parameter 
     303                    $coeffs = array (array ( 1), 
     304                                     array ( 1, 1),  
     305                                     array ( 1, 2, 1), 
     306                                     array ( 1, 3, 3, 1), 
     307                                     array ( 1, 4, 6, 4, 1), 
     308                                     array ( 1, 5, 10, 10, 5, 1), 
     309                                     array ( 1, 6, 15, 20, 15, 6, 1), 
     310                                     array ( 1, 7, 21, 35, 35, 21, 7, 1), 
     311                                     array ( 1, 8, 28, 56, 70, 56, 28, 8, 1), 
     312                                     array ( 1, 9, 36, 84, 126, 126, 84, 36, 9, 1), 
     313                                     array ( 1, 10, 45, 120, 210, 252, 210, 120, 45, 10, 1), 
     314                                     array ( 1, 11, 55, 165, 330, 462, 462, 330, 165, 55, 11, 1)); 
     315                    $sum = pow (2, $flou); 
     316                    $demi = $flou/2; 
     317                         
     318            $imagebis = $this->newImage( $this->finalwidth, $this->finalheight ); 
     319                        imagecopyresampled( $imagebis, $this->image, 0, 0, $this->posx, $this->posy, $this->finalwidth, $this->finalheight, $this->resamplewidth, $this->resampleheight ); 
     320                         
     321                        // Horizontal blur and blur margin 
     322                        $temp1 = $this->newImage( $this->finalwidth+$flou, $this->finalheight+$flou ); 
     323                     
     324                    for ( $i=0 ; $i < imagesx($temp1) ; $i++ ) 
     325                    for ( $j=0 ; $j < imagesy($temp1) ; $j++ ) { 
     326                        $ig = $i-$demi; $jg = $j-$demi; $suma = 0; 
     327                        for ( $k=0 ; $k <= $flou ; $k++ ) { 
     328                            $ik = $ig-$demi+$k; 
     329                            if( $jg<0 || $jg>imagesy($temp1)-$flou-1 ) $alpha = 127; 
     330                            else if( $ik<0 || $ik>imagesx($temp1)-$flou-1 ) $alpha = 127; 
     331                            else $alpha = (imagecolorat($imagebis, $ik, $jg) & 0x7F000000) >> 24; 
     332                            $suma += $alpha*$coeffs[$flou][$k]; 
     333                        } 
     334                        $c = imagecolorallocatealpha($temp1, 0, 0, 0, $suma/$sum ); 
     335                        imagesetpixel($temp1,$i,$j,$c); 
     336                    } 
     337                     
     338                    // Vertical blur, a shift of the angle, opacity and color 
     339                     
     340                    $x = cos(deg2rad($angle))*$leng; 
     341                    $y = sin(deg2rad($angle))*$leng; 
     342                     
     343            $this->shadow = $this->newImage( imagesx($temp1)+abs($x), imagesy($temp1)+abs($y) ); 
     344                     
     345                    $x1 = $x<0?0:$x; 
     346                    $y1 = $y<0?0:$y; 
     347                     
     348                    for ( $i=0 ; $i < imagesx($temp1) ; $i++ ) 
     349                    for ( $j=0 ; $j < imagesy($temp1) ; $j++ ) { 
     350                        $suma = 0; 
     351                        for ( $k=0 ; $k <= $flou ; $k++ ) { 
     352                            $jk = $j-$demi+$k; 
     353                            if( $jk<0 || $jk>imagesy($temp1)-1 ) $alpha = 127; 
     354                            else $alpha = (imagecolorat($temp1, $i, $jk) & 0x7F000000) >> 24; 
     355                            $suma += $alpha*$coeffs[$flou][$k]; 
     356                        } 
     357                        $alpha = 127-((127-($suma/$sum))/(100/$opac)); 
     358                        $c = imagecolorallocatealpha($this->shadow, $rgb[0], $rgb[1], $rgb[2], $alpha < 0 ? 0 : $alpha > 127 ? 127 : $alpha ); 
     359                        imagesetpixel($this->shadow,$i+$x1,$j+$y1,$c); 
     360                    } 
     361                    imagedestroy($temp1); 
     362                     
     363                    // Merge of the image and are shade 
     364                        $x = $x>0?0:$x; 
     365            $y = $y>0?0:$y; 
     366                        $this->shadowPosx = $demi-$x; 
     367            $this->shadowPosy = $demi-$y; 
     368             
     369        } 
     370         
     371    } 
     372         
     373         
     374         
     375    /** 
     376     *  
     377     */ 
     378    public function hexaToRgb( $color ) { 
     379         
     380        $color = str_replace('#', '', $color); 
     381        $rgb = array(0,0,0); 
     382        if (strlen($color) == 6) 
     383            for ($x=0;$x<3;$x++) 
     384                $rgb[$x] = hexdec(substr($color,(2*$x),2)); 
     385        else if (strlen($color) == 3) 
     386            for ($x=0;$x<3;$x++) 
     387                $rgb[$x] = hexdec(substr($color,(2*$x),1)); 
     388                return $rgb; 
     389         
     390    } 
     391         
     392    /** 
     393     *  
     394     */ 
     395    public function sauveInAndDestroy( $url, $name ) { 
     396         
     397                $ok = $this->sauveIn( $url, $name ); 
     398        @imagedestroy( $this->image ); 
     399                return $ok; 
     400                 
     401    } 
     402         
     403        /** 
     404         *  
     405         */ 
     406        public function sauveIn( $url, $name ) { 
     407                 
     408                $this->finalise(); 
     409                 
     410        jFile::createDir($url); 
     411        if( empty($this->extFinal) ) $this->extFinal = $this->ext; 
     412                $extmime = $this->mimes[$this->extFinal]; 
     413            switch ( $extmime ) { 
     414                case 'image/gif'  : $ok = imagegif  ($this->image, $url.$name.'.'.$this->extFinal); break; 
     415                case 'image/jpeg' : $ok = imagejpeg ($this->image, $url.$name.'.'.$this->extFinal, $this->quality); break; 
     416                default           : $ok = imagepng  ($this->image, $url.$name.'.'.$this->extFinal); 
     417            } 
     418                return $ok; 
     419                 
     420        } 
     421     
     422    /** 
     423     *  
     424     */ 
     425    public function finalise() { 
     426         
     427        $oldimage = $this->image; 
     428        $this->image = $this->newImage( $this->finalwidth, $this->finalheight ); 
     429        imagecopyresampled( $this->image, $oldimage, 0, 0, $this->posx, $this->posy, $this->finalwidth, $this->finalheight, $this->resamplewidth, $this->resampleheight ); 
     430        if( !empty( $this->shadow ) ) { 
     431           imagecopy( $this->shadow, $this->image, $this->shadowPosx, $this->shadowPosy, 0, 0, imagesx($this->image), imagesy($this->image)); 
     432           $this->image = $this->shadow; 
     433        } 
     434    } 
     435} 
Download in other formats: Original Format