Ticket #604: 604-jImage.diff

File 604-jImage.diff, 12.4 kB (added by Julien, 4 months ago)
  • build/manifests/jelix-lib.mn

    old new  
    233233  jFile.class.php 
    234234* jFilter.class.php 
    235235  jHttp.class.php 
     236  jImage.class.php 
    236237  jIniFile.class.php 
    237238  jIniFileModifier.class.php 
    238239* jJson.class.php 
     
    351352! jacl2.coord.php 
    352353  plugin.xml 
    353354  jacl2.coord.ini.php.dist 
    354    
     355 
    355356cd lib/jelix/plugins/coord/zendframework 
    356357  zendframework.coord.php 
    357358  plugin.xml 
     
    459460  function.link_to_remote.php 
    460461  function.breadcrumb.php 
    461462  function.jmessage.php 
     463  function.jimage.php 
    462464 
    463465cd lib/jelix/plugins/tpl/ltx2pdf 
    464466  function.jlocale.php 
  • lib/jelix/plugins/tpl/html/function.jimage.php

    old new  
     1<?php 
     2/** 
     3 * @package     jelix 
     4 * @subpackage  jtpl_plugin 
     5 * @author      Julien Issler 
     6 * @contributor 
     7 * @copyright   2008 Julien Issler 
     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 * @param jTpl $tpl template engine 
     14 * @param string $src the url of image, relative to JELIX_APP_WWW_PATH 
     15 * @param array $params parameters for the image 
     16 */ 
     17function jtpl_function_html_jimage($tpl, $src, $params=array()) { 
     18 
     19    if(!is_file(JELIX_APP_WWW_PATH.$src) || ($image_properties = getimagesize(JELIX_APP_WWW_PATH.$src)) === false) 
     20        return; 
     21 
     22    $width = $image_properties[0]; 
     23    $height = $image_properties[1]; 
     24 
     25 
     26 
     27    if(isset($params['width']) || isset($params['height']) || isset($params['maxwidth']) || isset($params['maxheight'])){ 
     28        $params['preserve_aspect_ratio'] = isset($params['preserve_aspect_ratio'])?$params['preserve_aspect_ratio']:true; 
     29        if($params['preserve_aspect_ratio']){ 
     30            $aspect_ratio = $width/$height; 
     31            if(isset($params['width'])){ 
     32                $width = $params['width']; 
     33                $height = isset($params['height'])?$params['height']:$width/$aspect_ratio; 
     34            } 
     35            else if(isset($params['height'])){ 
     36                $height = $params['height']; 
     37                $width = isset($params['width'])?$params['width']:$height*$aspect_ratio; 
     38            } 
     39 
     40            if(isset($params['maxwidth']) && $width > $params['maxwidth']){ 
     41                $height *= $params['maxwidth']/$width; 
     42                $width = $params['maxwidth']; 
     43                if(isset($params['maxheight']) && $height > $params['maxheight']){ 
     44                    $width *= $params['maxheight']/$height; 
     45                    $height = $params['maxheight']; 
     46                } 
     47            } 
     48            else if(isset($params['maxheight']) && $height > $params['maxheight']){ 
     49                $width *= $params['maxheight']/$height; 
     50                $height = $params['maxheight']; 
     51            } 
     52        } 
     53        else{ 
     54            if(isset($params['width'])) 
     55                $width = $params['width']; 
     56            if(isset($params['height'])) 
     57                $height = $params['height']; 
     58            if(isset($params['maxwidth']) && $width > $params['maxwidth']) 
     59                $width = $params['maxwidth']; 
     60            if(isset($params['maxheight']) && $height > $params['maxheight']) 
     61                $height = $params['maxheight']; 
     62        } 
     63    } 
     64 
     65    if(isset($params['ext'])) 
     66        $ext = strtolower($params['ext']); 
     67    else{ 
     68        $path_parts = pathinfo($src); 
     69        $ext = strtolower($path_parts['extension']); 
     70    } 
     71 
     72    // White background for IE < 7 
     73    if (!isset($params['background']) && strpos($_SERVER["HTTP_USER_AGENT"], 'MSIE') !== false && strpos($_SERVER["HTTP_USER_AGENT"], 'MSIE 7') === false) 
     74        $params['background'] = '#ffffff'; 
     75 
     76    $params_enabling_cache = array('ext','quality','width','height','preserve_aspect_ratio','maxwidth','maxheight','background'); 
     77 
     78    $cachename = ''; 
     79    ksort($params); 
     80    foreach($params as $k=>$v) 
     81        if(in_array($k,$params_enabling_cache)) 
     82            $cachename .= $k.$v; 
     83 
     84    if($cachename){ 
     85        $cachepath = JELIX_APP_WWW_PATH.'_caches/jimages/'; 
     86        $cachename = md5($src.$cachename).'.'.$ext; 
     87        if(file_exists($cachepath.$cachename)) 
     88            $url = '_caches/jimages/'.$cachename; 
     89        else{ 
     90            try{ 
     91                $jImage = new jImage($width,$height); 
     92                if(isset($params['background'])) 
     93                    $jImage->fillWithColor($params['background']); 
     94                $jImage->pasteImageFromFile(JELIX_APP_WWW_PATH.$src, $width, $height); 
     95                jFile::createDir($cachepath); 
     96                $jImage->saveToFile($cachepath.$cachename); 
     97                unset($jImage); 
     98                $url = '_caches/jimages/'.$cachename; 
     99            } 
     100            catch(Exception $e){} 
     101        } 
     102    } 
     103 
     104    if(!isset($url)) 
     105        $url = $src; 
     106 
     107 
     108    $allowed_html_attributes = array('id', 'class', 'style', 'longdesc', 'name', 'ismap', 'usemap', 'title', 'lang'); 
     109    $html_attributes = array(); 
     110    foreach($params as $k=>$v){ 
     111        if($v !== '' && in_array($k,$allowed_html_attributes)) 
     112            $html_attributes[$k] = $v; 
     113    } 
     114 
     115    if(isset($html_attributes['title'])) 
     116        $html_attributes['title'] = htmlspecialchars($html_attributes['title']); 
     117 
     118    echo '<img src="',$GLOBALS['gJConfig']->urlengine['basePath'],$url,'" alt="',isset($params['alt'])?htmlspecialchars($params['alt']):'','" width="',round($width),'" height="',round($height),'" '; 
     119    foreach($html_attributes as $key => $val) 
     120        echo $key,'="',$val,'" '; 
     121    echo '/>'; 
     122 
     123} 
  • lib/jelix/core-modules/jelix/locales/en_EN/errors.UTF-8.properties

    old new  
    3939ad.response.not.loaded=(112)Action %s : the class for the response type "%s" can't be loaded (file : %s) 
    4040ad.response.type.notallowed=(113)Action %s : the response type "%s" is not allowed for the current request (file : %s) 
    4141 
    42 ltx2pdf.exec = (120) %s has returned : %s  
     42ltx2pdf.exec = (120) %s has returned : %s 
    4343 
    4444#---- coordinator 
    4545module.untrusted=(130)Module "%s" is unknown or disabled 
     
    115115 
    116116#---- datetime 
    117117datetime.invalid = (400)jDateTime: invalid date/time (%d-%d-%d %d:%d:%d) 
     118 
     119#---- jImage 
     120jimage.not_a_valid_image_file = (430)jImage: the file %s is not a valid image file 
     121jimage.mime_type_not_supported = (431)jImage: the MIME type %s is not supported 
     122jimage.gd_function_not_exists = (432)jImage: the GD function %s does not exist 
     123jimage.save_format_not_supported = (433)jImage: cannot save image with %s extension 
  • lib/jelix/core-modules/jelix/locales/en_US/errors.UTF-8.properties

    old new  
    115115 
    116116#---- datetime 
    117117datetime.invalid = (400)jDateTime: invalid date/time (%d-%d-%d %d:%d:%d) 
     118 
     119#---- jImage 
     120jimage.not_a_valid_image_file = (430)jImage: the file %s is not a valid image file 
     121jimage.mime_type_not_supported = (431)jImage: the MIME type %s is not supported 
     122jimage.gd_function_not_exists = (432)jImage: the GD function %s does not exist 
     123jimage.save_format_not_supported = (433)jImage: cannot save image with %s extension 
  • lib/jelix/core-modules/jelix/locales/fr_FR/errors.UTF-8.properties

    old new  
    3939ad.response.not.loaded=(112)Action %s : La classe pour la réponse de type "%s" ne peut être chargée (fichier : %s) 
    4040ad.response.type.notallowed=(113)Action %s : le type de réponse "%s" n'est pas permis pour la requête courante (file : %s) 
    4141 
    42 ltx2pdf.exec = (120)L'execution de %s a retourné : %s  
     42ltx2pdf.exec = (120)L'execution de %s a retourné : %s 
    4343 
    4444#---- coordinator 
    4545module.untrusted=(130)Module "%s" inconnu ou désactivé 
     
    114114 
    115115#---- datetime 
    116116datetime.invalid = (400)jDateTime: date/heure invalide (%d-%d-%d %d:%d:%d) 
     117 
     118#---- jImage 
     119jimage.not_a_valid_image_file = (430)jImage : le fichier %s n'est pas une image valide 
     120jimage.mime_type_not_supported = (431)jImage : le type MIME %s n'est pas supporté 
     121jimage.gd_function_not_exists = (432)jImage : la fonction GD %s n'existe pas 
     122jimage.save_format_not_supported = (433)jImage : impossible d'enregistrer l'image avec l'extension %s 
  • lib/jelix/utils/jImage.class.php

    old new  
     1<?php 
     2/** 
     3* @package    jelix 
     4* @subpackage utils 
     5* @author     Julien Issler 
     6* @contributor 
     7* @copyright  2008 Julien Issler 
     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* @since 1.1 
     11*/ 
     12 
     13/** 
     14 * jImage class 
     15 * 
     16 * @package  jelix 
     17 * @subpackage utils 
     18 * @since 1.1 
     19 */ 
     20class jImage{ 
     21 
     22 
     23    protected $_gd_image; 
     24 
     25    protected $_width; 
     26 
     27    protected $_height; 
     28 
     29 
     30    public function __construct($width, $height){ 
     31 
     32        if(!function_exists('imagecreatetruecolor')) 
     33            throw new jException('jelix~errors.jimage.gd_function_not_exists',array('imagecreatetruecolor')); 
     34 
     35        $this->_gd_image = imagecreatetruecolor($width, $height); 
     36        $this->_width = $width; 
     37        $this->_height = $height; 
     38        imagesavealpha($this->_gd_image, true); 
     39        imagefill($this->_gd_image,0,0,imagecolorallocatealpha($this->_gd_image,0,0,0,127)); 
     40    } 
     41 
     42 
     43    public function __destruct(){ 
     44        imagedestroy($this->_gd_image); 
     45    } 
     46 
     47    public function pasteImageFromFile($file, $w, $h){ 
     48 
     49        if(!file_exists($file)) 
     50            throw new jException('jelix~errors.file.notexists',array($file)); 
     51 
     52        if(($file_info = getimagesize($file))===false) 
     53            throw new jException('jelix~errors.jimage.not_a_valid_image_file',array($file)); 
     54 
     55        switch($file_info['mime']){ 
     56            case 'image/gif': 
     57                $f = 'imagecreatefromgif'; break; 
     58            case 'image/jpeg': 
     59                $f = 'imagecreatefromjpeg'; break; 
     60            case 'image/png': 
     61                $f = 'imagecreatefrompng'; break; 
     62            default: 
     63                throw new jException('jelix~errors.jimage.mime_type_not_supported',array($file_info['mime'])); 
     64        } 
     65 
     66        if(!function_exists($f)) 
     67            throw new jException('jelix~errors.jimage.gd_function_not_exists',array($f)); 
     68 
     69        $imported_image = $f($file); 
     70 
     71        imagecopyresampled($this->_gd_image, $imported_image, 0, 0, 0, 0, $w, $h, $file_info[0], $file_info[1]); 
     72 
     73        imagedestroy($imported_image); 
     74 
     75    } 
     76 
     77 
     78    public function fillWithColor($color, $alpha=0){ 
     79        $c = $this->_convertHexColor($color); 
     80        imagefill($this->_gd_image,0,0,imagecolorallocatealpha($this->_gd_image,$c[0],$c[1],$c[2],$alpha)); 
     81    } 
     82 
     83 
     84    public function saveToFile($file, $quality=null){ 
     85        $pathinfo = pathinfo($file); 
     86        switch(strtolower($pathinfo['extension'])){ 
     87            case 'jpg': 
     88            case 'jpeg': 
     89                imagejpeg($this->_gd_image, $file, $quality); 
     90                break; 
     91            case 'gif': 
     92                imagegif($this->_gd_image, $file); 
     93                break; 
     94            case 'png': 
     95                imagepng($this->_gd_image, $file); 
     96                break; 
     97            default: 
     98                throw new jException('jelix~errors.jimage.save_format_not_supported',array($pathinfo['extension'])); 
     99        } 
     100    } 
     101 
     102 
     103    protected function _convertHexColor($color){ 
     104        $color = str_replace('#','',$color); 
     105        return array(hexdec(substr($color,0,2)),hexdec(substr($color,2,2)),hexdec(substr($color,4,2))); 
     106    } 
     107 
     108} 
Download in other formats: Original Format