Ticket #474: function.image.php

File function.image.php, 6.8 kB (added by Lipki, 10 months ago)
Line 
1 <?php
2 /**
3 * @package    jelix
4 * @subpackage jtpl_plugin
5 * @author     Lepeltier kévin
6 * @copyright  2007 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  * Ajoute un lien vers l'image,
13  * l'image est redimensionné, et mis en cache
14  *
15  * class :string
16  * id :string
17  * alt :string
18  * width :uint
19  * height :uint
20  * zoom 1-100
21  * omo :boolean
22  * alignh [left|center|right|:int]
23  * alignv [top|center|bottom|:int]
24  * ext [png|jpg|gif]
25  * quality 0-100 if ext = jpg
26  *
27  * gif   -> image/gif
28  * jpeg  -> image/jpeg
29  * jpg   -> image/jpeg
30  * jpe   -> image/jpeg
31  * xpm   -> image/x-xpixmap
32  * xbm   -> image/x-xbitmap
33  * wbmp  -> image/vnd.wap.wbmp
34  * png   -> image/png
35  * other -> image/png
36  */
37
38 /**
39  * image plugin :  write the url corresponding to the image
40  *
41  * @param jTpl $tpl template engine
42  * @param string $src the url of image (data/fichiers/):string.[gif|jpeg|jpg|jpe|xpm|xbm|wbmp|png]
43  * @param array $params parameters for the url
44  */
45 function jtpl_function_html_image($tpl, $src, $params=array()) {
46     
47     // extension choisit
48     if( empty($params['ext']) ) {
49         $path_parts = pathinfo($src);
50         $ext = $path_parts['extension'];
51     } else $ext = $params['ext'];
52     
53     // nom unique du fichier en cache
54     $chaine = $src;
55     foreach($params as $key => $value)
56         if( !in_array($key, array('alt', 'class', 'id')))
57             $chaine .= $value;
58     $cachename = md5($chaine).'.'.$ext;
59     
60     // path
61     $cache_path = JELIX_APP_WWW_PATH.'cache/images/'.$cachename;
62     $origine_path = JELIX_APP_WWW_PATH.'data/fichiers/'.$src;
63     
64     global $gJConfig;
65     $www = 'http'.(empty($_SERVER['HTTPS'])?'':'s').'://'.$_SERVER['HTTP_HOST'].$gJConfig->urlengine['basePath'];
66     $cache_www = $www.'cache/images/'.$cachename;
67     $origine_www = $www.'data/fichiers/'.$src;
68     
69     // mettre en cache et faire les transformations si nésséssaire.
70     if( is_file($origine_path) && !is_file($cache_path) ) {
71         $att = array('width'=>'', 'height'=>'', 'omo'=>'', 'zoom'=>'', 'alignh'=>'', 'alignv'=>'', 'ext'=>'', 'quality'=>'');
72         if( count(array_intersect_key($params, $att)) )
73             jtpl_function_html_image_inCache($src, $cachename, $params);
74     }
75     
76     // les attributs
77     $att = array('alt'=>'', 'id'=>'', 'class'=>'');
78     $att = array_intersect_key($params, $att);
79     
80     // si l'image ne subit pas de transformation
81     if( !is_file($cache_path) ) {
82         $att['src'] = $origine_www;
83         $att['style'] = '';
84         if( !empty($params['width']) )     $att['style'] .= 'width: '.$params['width'].'px;';
85         if( !empty($params['height']) ) $att['style'] .= 'height: '.$params['height'].'px;';
86     } else
87         $att['src'] = $cache_www;
88     
89     // la balise image
90     echo '<img';
91     foreach( $att as $key => $val )
92         if( !empty($val) )
93             echo ' '.$key.'="'.$val.'"';
94     echo '/>';
95     
96 }
97
98 function jtpl_function_html_image_inCache($src, $cachename, $array) {
99     
100     $mimes = array('gif'=>'image/gif', 'png'=>'image/png',
101                    'jpeg'=>'image/jpeg', 'jpg'=>'image/jpeg', 'jpe'=>'image/jpeg',
102                    'xpm'=>'image/x-xpixmap', 'xbm'=>'image/x-xbitmap', 'wbmp'=>'image/vnd.wap.wbmp');
103     
104     global $gJConfig;
105     $origine_www = 'http'.(empty($_SERVER['HTTPS'])?'':'s').'://'.$_SERVER['HTTP_HOST'].$gJConfig->urlengine['basePath'].'data/fichiers/'.$src;
106     
107     $path_parts = pathinfo($origine_www);
108     $ext = $mimes[$path_parts['extension']];
109     $quality = (!empty($array['quality']))?  $array['quality'] : 100;
110     
111     // création de l'image
112     switch ( $ext ) {
113         case 'image/gif'             : $image = imagecreatefromgif($origine_www); break;
114         case 'image/jpeg'            : $image = imagecreatefromjpeg($origine_www); break;
115         case 'image/png'             : $image = imagecreatefrompng($origine_www); break;
116         case 'image/vnd.wap.wbmp'    : $image = imagecreatefromwbmp($origine_www); break;
117         case 'image/image/x-xbitmap' : $image = imagecreatefromxbm($origine_www); break;
118         case 'image/x-xpixmap'       : $image = imagecreatefromxpm($origine_www); break;
119         default                      : return ;
120     }
121     
122     if (!empty($array['width']) || !empty($array['height'])) {
123     
124         $ancienimage = $image;
125         $resampleheight = imagesy($ancienimage);
126         $resamplewidth = imagesx($ancienimage);
127         $posx = 0;
128         $posy = 0;
129         
130         if(empty($array['width'])) {
131             $finalheight = $array['height'];
132             $finalwidth = $finalheight*imagesx($ancienimage)/imagesy($ancienimage);
133         } else if (empty($array['height'])) {
134             $finalwidth = $array['width'];
135             $finalheight = $finalwidth*imagesy($ancienimage)/imagesx($ancienimage);
136         } else {
137             $finalwidth = $array['width'];
138             $finalheight = $array['height'];
139             if(!empty($array['omo']) && $array['omo'] == 'true') {
140                 if($array['width'] >= $array['height']) {
141                     $resampleheight = ( $resamplewidth*$array['height'] )/$array['width'];
142                 } else {
143                     $resamplewidth = ( $resampleheight*$array['width'] )/$array['height'];
144                 }
145             }
146         }
147
148         if(!empty($array['zoom'])) {
149             $resampleheight /= 100/$array['zoom'];
150             $resamplewidth /= 100/$array['zoom'];
151         }
152         
153         $posx = imagesx($ancienimage)/2 -$resamplewidth/2;
154         $posy = imagesy($ancienimage)/2 -$resampleheight/2;
155
156         if(!empty($array['alignh'])) {
157             if($array['alignh'] == 'left')            $posx = 0;
158             else if($array['alignh'] == 'right')    $posx = -($resamplewidth - imagesx($ancienimage));
159             else if($array['alignh'] != 'center')    $posx = -$array['alignh'];
160         }
161
162         if(!empty($array['alignv'])) {
163            if($array['alignv'] == 'top')            $posy = 0;
164             else if($array['alignv'] == 'bottom')    $posy = -($resampleheight - imagesy($ancienimage));
165             else if($array['alignv'] != 'center')    $posy = -$array['alignv'];
166         }
167     
168         $image = imagecreatetruecolor($finalwidth, $finalheight);
169         imagealphablending($image, false);
170         imagesavealpha($image, true);
171         $tp = imagecolorallocate($image,0,0,0);
172         imagefill($image,0,0,$tp);
173         imagecolortransparent($image,$tp);
174         imagecopyresampled($image, $ancienimage, 0, 0, $posx, $posy, imagesx($image), imagesy($image), $resamplewidth, $resampleheight);
175     }
176     
177     $ext = empty($array['ext'])?$ext:$mimes[$array['ext']];
178     $cache_path = JELIX_APP_WWW_PATH.'cache/images/';
179
180     //enregistrer
181     switch ( $ext ) {
182         case 'image/gif'  : imagegif($image, $cache_path.$cachename); break;
183         case 'image/jpeg' : imagejpeg($image, $cache_path.$cachename, $quality); break;
184         default           : imagepng($image, $cache_path.$cachename);
185     }
186     
187     // destruction
188     @imagedestroy($image);
189 }
190
191 ?>
192
Download in other formats: Original Format