Ticket #628: jelix-trunk-#628.patch

File jelix-trunk-#628.patch, 11.2 kB (added by bibo, 7 months ago)
  • lib/jelix/plugins/tpl/html/function.image.php

    old new  
    11<?php 
    22/** 
    3  * @package    jelix 
    4  * @subpackage jtpl_plugin 
    5  * @author     Lepeltier kévin 
    6  * @copyright  2007-2008 Lepeltier kévin 
    7  * @link       http://www.jelix.org 
    8  * @licence    GNU Lesser General Public Licence see LICENCE file or http://www.gnu.org/licenses/lgpl.html 
     3 * @package     jelix 
     4 * @subpackage  jtpl_plugin 
     5 * @author      Lepeltier kévin 
     6 * @contributor Dominique Papin 
     7 * @copyright   2007-2008 Lepeltier kévin, 2008 Dominique Papin 
     8 * @link        http://www.jelix.org 
     9 * @licence     GNU Lesser General Public Licence see LICENCE file or http://www.gnu.org/licenses/lgpl.html 
    910 */ 
    1011 
    1112/** 
    1213 * image plugin :  write the url corresponding to the image 
    13  *  
     14 * 
    1415 * Add a link to the image, 
    1516 * The image is resized, and cached 
    1617 * 
     
    5051 * @param array $params parameters for the url 
    5152 */ 
    5253function jtpl_function_html_image($tpl, $src, $params=array()) { 
    53      
     54 
    5455    // Extension 
    5556    if( empty($params['ext']) ) { 
    5657        $path_parts = pathinfo($src); 
    5758        $ext = strtolower($path_parts['extension']); 
    5859    } else $ext = strtolower($params['ext']); 
    59      
     60 
    6061    // White background for IE 
    6162    if (   empty($params['background']) 
    6263        && strpos($_SERVER["HTTP_USER_AGENT"], 'MSIE') !== false 
    6364        && strpos($_SERVER["HTTP_USER_AGENT"], 'MSIE 7') === false) { 
    6465        $params['background'] = '#ffffff'; 
    6566    } 
    66      
     67 
    6768    // Name of the file cache 
    6869    $chaine = $src; 
    6970    foreach($params as $key => $value) 
    7071        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'))) 
    7172            $chaine .= $key.$value; 
    7273    $cachename = md5($chaine).'.'.$ext; 
    73      
     74 
    7475    // Path 
    7576    $cache_path = JELIX_APP_WWW_PATH.'cache/images/'.$cachename; 
    7677    $origine_path = JELIX_APP_WWW_PATH.$src; 
    77      
     78 
    7879    global $gJConfig; 
    7980    $www = 'http'.(empty($_SERVER['HTTPS'])?'':'s').'://'.$_SERVER['HTTP_HOST'].$gJConfig->urlengine['basePath']; 
    8081    $cache_www = $www.'cache/images/'.$cachename; 
    8182    $origine_www = $www.$src; 
    82      
     83 
    8384    // Cache and make changes if necessary 
    8485    if( is_file($origine_path) && !is_file($cache_path) ) { 
    8586        $att = array('width'=>'', 'height'=>'', 'maxwidth'=>'', 'maxheight'=>'', 'zoom'=>'', 'alignh'=>'', 'alignv'=>'', 'ext'=>'', 'quality'=>'', 'shadow'=>''); 
    8687        if( count(array_intersect_key($params, $att)) ) 
    8788            jtpl_function_html_image_inCache($src, $cachename, $params); 
    8889    } 
    89      
     90 
    9091    // Attributes 
    9192    $att = array('alt'=>'', 'id'=>'', 'class'=>'', 'style'=>'', 'longdesc'=>'', 'name'=>'', 'ismap'=>'', 'usemap'=>'', 'title'=>'', 'dir'=>'', 'lang'=>'', 'onclick'=>'', 'ondblclick'=>'', 'onmousedown'=>'', 'onmouseup'=>'', 'onmouseover'=>'', 'onmousemove'=>'', 'onmouseout'=>'', 'onkeypress'=>'', 'onkeydown'=>'', 'onkeyup'=>''); 
    9293    $att = array_intersect_key($params, $att); 
    93      
     94 
    9495    // If the image does not undergo transformation 
    9596    if( !is_file($cache_path) ) { 
    9697        $att['src'] = $origine_www; 
     
    101102        else if( !empty($params['maxheight']) )    $att['style'] .= 'height: '.$params['maxheight'].'px;'; 
    102103    } else 
    103104        $att['src'] = $cache_www; 
    104      
     105 
    105106    // Tag image 
    106107    echo '<img'; 
    107108    foreach( $att as $key => $val ) 
    108109        if( !empty($val) ) 
    109110            echo ' '.$key.'="'.$val.'"'; 
    110111    echo '/>'; 
    111      
     112 
    112113    } 
    113114 
    114115function jtpl_function_html_image_inCache($src, $cachename, $array) { 
    115      
    116     $mimes = array('gif'=>'image/gif', 'png'=>'image/png',  
    117                    'jpeg'=>'image/jpeg', 'jpg'=>'image/jpeg', 'jpe'=>'image/jpeg',  
     116 
     117    $mimes = array('gif'=>'image/gif', 'png'=>'image/png', 
     118                   'jpeg'=>'image/jpeg', 'jpg'=>'image/jpeg', 'jpe'=>'image/jpeg', 
    118119                   'xpm'=>'image/x-xpixmap', 'xbm'=>'image/x-xbitmap', 'wbmp'=>'image/vnd.wap.wbmp'); 
    119      
     120 
    120121    global $gJConfig; 
    121122    $origine_www = 'http'.(empty($_SERVER['HTTPS'])?'':'s').'://'.$_SERVER['HTTP_HOST'].$gJConfig->urlengine['basePath'].$src; 
    122      
     123 
    123124    $path_parts = pathinfo($origine_www); 
    124125    $ext = $mimes[strtolower($path_parts['extension'])]; 
    125126    $quality = (!empty($array['quality']))?  $array['quality'] : 100; 
    126      
     127 
    127128    // Creating an image 
    128129    switch ( $ext ) { 
    129130        case 'image/gif'             : $image = imagecreatefromgif($origine_www); break; 
     
    134135        case 'image/x-xpixmap'       : $image = imagecreatefromxpm($origine_www); break; 
    135136        default                      : return ; 
    136137    } 
    137      
     138 
    138139    if(!empty($array['maxwidth']) && !empty($array['maxheight'])) { 
    139          
    140         $rapy = imagesy($image)/$array['maxwidth']; 
    141         $rapx = imagesx($image)/$array['maxheight']; 
    142          
    143         if( $rapy > $rapx ) { 
    144             $array['height'] = $array['maxheight']; 
    145             $array['width'] = imagesx($image)/$rapy; 
     140 
     141        $origWidth = imagesx($image); 
     142        $origHeight = imagesy($image); 
     143        $constWidth = $array['maxwidth']; 
     144        $constHeight = $array['maxheight']; 
     145        $ratio = imagesx($image)/imagesy($image); 
     146 
     147        if ( $origWidth < $constWidth && $origHeight < $constHeight ) { 
     148            $array['width'] = $origWidth; 
     149            $array['height'] = $origHeight; 
    146150        } else { 
    147             $array['width'] = $array['maxwidth']; 
    148             $array['height'] = imagesy($image)/$rapx; 
     151            $ratioHeight = $constWidth/$ratio; 
     152            $ratioWidth = $constHeight*$ratio; 
     153            if ( $ratioWidth > $constWidth ) { 
     154                $constHeight = $ratioHeight; 
     155            } 
     156            else if ( $ratioHeight > $constHeight ) { 
     157                $constWidth = $ratioWidth; 
     158            } 
     159            $array['width'] = $constWidth; 
     160            $array['height'] = $constHeight; 
    149161        } 
    150162    } 
    151      
     163 
    152164    if (!empty($array['width']) || !empty($array['height'])) { 
    153      
     165 
    154166        $ancienimage = $image; 
    155167        $resampleheight = imagesy($ancienimage); 
    156168        $resamplewidth = imagesx($ancienimage); 
    157169        $posx = 0; 
    158170        $posy = 0; 
    159          
     171 
    160172        if(empty($array['width'])) { 
    161173            $finalheight = $array['height']; 
    162174            $finalwidth = $finalheight*imagesx($ancienimage)/imagesy($ancienimage); 
     
    174186                } 
    175187            } 
    176188        } 
    177          
     189 
    178190        if(!empty($array['zoom'])) { 
    179191            $resampleheight /= 100/$array['zoom']; 
    180192            $resamplewidth /= 100/$array['zoom']; 
    181193        } 
    182          
     194 
    183195        $posx = imagesx($ancienimage)/2 -$resamplewidth/2; 
    184196        $posy = imagesy($ancienimage)/2 -$resampleheight/2; 
    185          
     197 
    186198        if(!empty($array['alignh'])) { 
    187199            if($array['alignh'] == 'left')            $posx = 0; 
    188200            else if($array['alignh'] == 'right')    $posx = -($resamplewidth - imagesx($ancienimage)); 
    189201            else if($array['alignh'] != 'center')    $posx = -$array['alignh']; 
    190202        } 
    191          
     203 
    192204        if(!empty($array['alignv'])) { 
    193205            if($array['alignv'] == 'top')            $posy = 0; 
    194206            else if($array['alignv'] == 'bottom')    $posy = -($resampleheight - imagesy($ancienimage)); 
    195207            else if($array['alignv'] != 'center')    $posy = -$array['alignv']; 
    196208        } 
    197          
     209 
    198210        $image = imagecreatetruecolor($finalwidth, $finalheight); 
    199211        imagesavealpha($image, true); 
    200212        $tp = imagecolorallocatealpha($image,0,0,0,127); 
    201213        imagefill($image,0,0,$tp); 
    202          
     214 
    203215        imagecopyresampled($image, $ancienimage, 0, 0, $posx, $posy, imagesx($image), imagesy($image), $resamplewidth, $resampleheight); 
    204216    } 
    205      
     217 
    206218    // The shadow cast adds to the dimension of the image chooses 
    207219    if( !empty($array['shadow']) ) 
    208220       $image = jtpl_function_html_image_ombre ($image, $array); 
    209      
     221 
    210222    // Background 
    211223    if( !empty($array['background']) ) { 
    212224        $array['background'] = str_replace('#', '', $array['background']); 
     
    217229        imagecopy( $fond, $image, 0, 0, 0, 0, imagesx($image), imagesy($image)); 
    218230        $image = $fond; 
    219231    } 
    220      
    221      
     232 
     233 
    222234    $ext = empty($array['ext'])?$ext:$mimes[$array['ext']]; 
    223235    $cache_path = JELIX_APP_WWW_PATH.'cache/images/'; 
    224236    jFile::createDir($cache_path); 
    225      
    226      
     237 
     238 
    227239    // Register 
    228240    switch ( $ext ) { 
    229241        case 'image/gif'  : imagegif($image, $cache_path.$cachename); break; 
    230242        case 'image/jpeg' : imagejpeg($image, $cache_path.$cachename, $quality); break; 
    231243        default           : imagepng($image, $cache_path.$cachename); 
    232244    } 
    233      
     245 
    234246    // Destruction 
    235247    @imagedestroy($image); 
    236248} 
    237249 
    238250function jtpl_function_html_image_ombre ( $image, $array) { 
    239      
     251 
    240252    // Default 
    241253    $leng = isset($array['soffset'])?$array['soffset']:10; 
    242254    $angle = isset($array['sangle'])?$array['sangle']:135; 
    243255    $flou = isset($array['sblur'])?$array['sblur']:10; 
    244256    $opac = isset($array['sopacity'])?$array['sopacity']:20; 
    245257    $color = isset($array['scolor'])?$array['scolor']:'#000000'; 
    246      
     258 
    247259    // Color of the shadow 
    248260    $color = str_replace('#', '', $color); 
    249261    $rgb = array(0,0,0); 
     
    253265    else if (strlen($color) == 3) 
    254266        for ($x=0;$x<3;$x++) 
    255267            $rgb[$x] = hexdec(substr($color,(2*$x),1)); 
    256      
     268 
    257269    // Gaussian blur parameter 
    258270    $coeffs = array (array ( 1), 
    259                      array ( 1, 1),  
     271                     array ( 1, 1), 
    260272                     array ( 1, 2, 1), 
    261273                     array ( 1, 3, 3, 1), 
    262274                     array ( 1, 4, 6, 4, 1), 
     
    269281                     array ( 1, 11, 55, 165, 330, 462, 462, 330, 165, 55, 11, 1)); 
    270282    $sum = pow (2, $flou); 
    271283    $demi = $flou/2; 
    272      
    273      
     284 
     285 
    274286    // Horizontal blur and blur margin 
    275287    $temp1 = imagecreatetruecolor(imagesx($image)+$flou, imagesy($image)+$flou); 
    276288    imagesavealpha($temp1, true); 
    277289    $tp = imagecolorallocatealpha($temp1,0,0,0,127); 
    278290    imagefill($temp1,0,0,$tp); 
    279      
     291 
    280292    for ( $i=0 ; $i < imagesx($temp1) ; $i++ ) 
    281293    for ( $j=0 ; $j < imagesy($temp1) ; $j++ ) { 
    282294        $ig = $i-$demi; $jg = $j-$demi; $suma = 0; 
     
    290302        $c = imagecolorallocatealpha($temp1, 0, 0, 0, $suma/$sum ); 
    291303        imagesetpixel($temp1,$i,$j,$c); 
    292304    } 
    293      
     305 
    294306    // Vertical blur, a shift of the angle, opacity and color 
    295      
     307 
    296308    $x = cos(deg2rad($angle))*$leng; 
    297309    $y = sin(deg2rad($angle))*$leng; 
    298      
     310 
    299311    $temp2 = imagecreatetruecolor(imagesx($temp1)+abs($x), imagesy($temp1)+abs($y)); 
    300312    imagesavealpha($temp2, true); 
    301313    $tp = imagecolorallocatealpha($temp2,0,0,0,127); 
    302314    imagefill($temp2,0,0,$tp); 
    303      
     315 
    304316    $x1 = $x<0?0:$x; 
    305317    $y1 = $y<0?0:$y; 
    306      
     318 
    307319    for ( $i=0 ; $i < imagesx($temp1) ; $i++ ) 
    308320    for ( $j=0 ; $j < imagesy($temp1) ; $j++ ) { 
    309321        $suma = 0; 
     
    318330        imagesetpixel($temp2,$i+$x1,$j+$y1,$c); 
    319331    } 
    320332    imagedestroy($temp1); 
    321      
     333 
    322334    // Merge of the image and are shade 
    323335    $x = $x>0?0:$x; 
    324336    $y = $y>0?0:$y; 
    325337    imagecopy( $temp2, $image, $demi-$x, $demi-$y, 0, 0, imagesx($image), imagesy($image)); 
    326      
     338 
    327339    return $temp2; 
    328340 
    329341} 
Download in other formats: Original Format