Dotclear

source: plugins/blowupConfig/lib/class.blowup.config.php @ 270:48858be15bda

Revision 270:48858be15bda, 11.8 KB checked in by Franck <carnet.franck.paul@…>, 14 years ago (diff)

Changement d'année

Line 
1<?php
2# -- BEGIN LICENSE BLOCK ---------------------------------------
3#
4# This file is part of Dotclear 2.
5#
6# Copyright (c) 2003-2011 Olivier Meunier & Association Dotclear
7# Licensed under the GPL version 2.0 license.
8# See LICENSE file or
9# http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
10#
11# -- END LICENSE BLOCK -----------------------------------------
12if (!defined('DC_RC_PATH')) { return; }
13
14class blowupConfig
15{
16     protected static $fonts = array(
17          'sans-serif' => array(
18               'ss1' => 'Arial, Helvetica, sans-serif',
19               'ss2' => 'Verdana,Geneva, Arial, Helvetica, sans-serif',
20               'ss3' => '"Lucida Grande", "Lucida Sans Unicode", sans-serif',
21               'ss4' => '"Trebuchet MS", Helvetica, sans-serif',
22               'ss5' => 'Impact, Charcoal, sans-serif'
23          ),
24         
25          'serif' => array(
26               's1' => 'Times, "Times New Roman", serif',
27               's2' => 'Georgia, serif',
28               's3' => 'Baskerville, "Palatino Linotype", serif'
29          ),
30         
31          'monospace' => array(
32               'm1' => '"Andale Mono", "Courier New", monospace',
33               'm2' => '"Courier New", Courier, mono, monospace'
34          )
35     );
36     
37     protected static $fonts_combo = array();
38     protected static $fonts_list = array();
39     
40     public static $top_images = array(
41          'default' => 'Default',
42          'blank' => 'Blank',
43          'light-trails-1' => 'Light Trails 1',
44          'light-trails-2' => 'Light Trails 2',
45          'light-trails-3' => 'Light Trails 3',
46          'light-trails-4' => 'Light Trails 4',
47          'butterflies' => 'Butterflies',
48          'flourish-1' => 'Flourished 1',
49          'flourish-2' => 'Flourished 2',
50          'animals' => 'Animals',
51          'flamingo' => 'Flamingo',
52          'rabbit' => 'Rabbit',
53          'roadrunner-1' => 'Road Runner 1',
54          'roadrunner-2' => 'Road Runner 2',
55          'typo' => 'Typo',
56     );
57     
58     public static function fontsList()
59     {
60          if (empty(self::$fonts_combo))
61          {
62               self::$fonts_combo[__('default')] = '';
63               foreach (self::$fonts as $family => $g)
64               {
65                    $fonts = array();
66                    foreach ($g as $code => $font) {
67                         $fonts[str_replace('"','',$font)] = $code;
68                    }
69                    self::$fonts_combo[$family] = $fonts;
70               }
71          }
72         
73          return self::$fonts_combo;
74     }
75     
76     public static function fontDef($c)
77     {
78          if (empty(self::$fonts_list))
79          {
80               foreach (self::$fonts as $family => $g)
81               {
82                    foreach ($g as $code => $font) {
83                         self::$fonts_list[$code] = $font;
84                    }
85               }
86          }
87         
88          return isset(self::$fonts_list[$c]) ? self::$fonts_list[$c] : null;
89     }
90     
91     public static function adjustFontSize($s)
92     {
93          if (preg_match('/^([0-9.]+)\s*(%|pt|px|em|ex)?$/',$s,$m)) {
94               if (empty($m[2])) {
95                    $m[2] = 'px';
96               }
97               return $m[1].$m[2];
98          }
99         
100          return null;
101     }
102     
103     public static function adjustPosition($p)
104     {
105          if (!preg_match('/^[0-9]+(:[0-9]+)?$/',$p)) {
106               return null;
107          }
108         
109          $p = explode(':',$p);
110         
111          return $p[0].(count($p) == 1 ? ':0' : ':'.$p[1]);
112     }
113     
114     public static function adjustColor($c)
115     {
116          if ($c === '') {
117               return '';
118          }
119         
120          $c = strtoupper($c);
121         
122          if (preg_match('/^[A-F0-9]{3,6}$/',$c)) {
123               $c = '#'.$c;
124          }
125         
126          if (preg_match('/^#[A-F0-9]{6}$/',$c)) {
127               return $c;
128          }
129         
130          if (preg_match('/^#[A-F0-9]{3,}$/',$c)) {
131               return '#'.substr($c,1,1).substr($c,1,1).substr($c,2,1).substr($c,2,1).substr($c,3,1).substr($c,3,1);
132          }
133         
134          return '';
135     }
136     
137     public static function imagesPath()
138     {
139          global $core;
140          return path::real($core->blog->public_path).'/blowup-images';
141     }
142     
143     public static function imagesURL()
144     {
145          global $core;
146          return $core->blog->settings->system->public_url.'/blowup-images';
147     }
148     
149     public static function canWriteImages($create=false)
150     {
151          global $core;
152         
153          $public = path::real($core->blog->public_path);
154          $imgs = self::imagesPath();
155         
156          if (!function_exists('imagecreatetruecolor') || !function_exists('imagepng') || !function_exists('imagecreatefrompng')) {
157               $core->error->add(__('At least one of the following functions is not available: '.
158                    'imagecreatetruecolor, imagepng & imagecreatefrompng.'));
159               return false;
160          }
161         
162          if (!is_dir($public)) {
163               $core->error->add(__('The \'public\' directory does not exist.'));
164               return false;
165          }
166         
167          if (!is_dir($imgs)) {
168               if (!is_writable($public)) {
169                    $core->error->add(sprintf(__('The \'%s\' directory cannot be modified.'),'public'));
170                    return false;
171               }
172               if ($create) {
173                    files::makeDir($imgs);
174               }
175               return true;
176          }
177         
178          if (!is_writable($imgs)) {
179               $core->error->add(sprintf(__('The \'%s\' directory cannot be modified.'),'public/blowup-images'));
180               return false;
181          }
182         
183          return true;
184     }
185     
186     public static function uploadImage($f)
187     {
188          if (!self::canWriteImages(true)) {
189               throw new Exception(__('Unable to create images.'));
190          }
191         
192          $name = $f['name'];
193          $type = files::getMimeType($name);
194         
195          if ($type != 'image/jpeg' && $type != 'image/png') {
196               throw new Exception(__('Invalid file type.'));
197          }
198         
199          $dest = self::imagesPath().'/uploaded'.($type == 'image/png' ? '.png' : '.jpg');
200         
201          if (@move_uploaded_file($f['tmp_name'],$dest) === false) {
202               throw new Exception(__('An error occurred while writing the file.'));
203          }
204         
205          $s = getimagesize($dest);
206          if ($s[0] != 800) {
207               throw new Exception(__('Uploaded image is not 800 pixels wide.'));
208          }
209         
210          return $dest;
211     }
212     
213     public static function createImages(&$config,$uploaded)
214     {
215          $body_color = $config['body_bg_c'];
216          $prelude_color = $config['prelude_c'];
217          $gradient = $config['body_bg_g'];
218          $comment_color = $config['post_comment_bg_c'];
219          $comment_color_my = $config['post_commentmy_bg_c'];
220          $top_image = $config['top_image'];
221         
222          $config['top_height'] = null;
223         
224          if ($top_image != 'custom' && !isset(self::$top_images[$top_image])) {
225               $top_image = 'default';
226          }
227          if ($uploaded && !is_file($uploaded)) {
228               $uploaded = null;
229          }
230         
231          if (!self::canWriteImages(true)) {
232               throw new Exception(__('Unable to create images.'));
233          }
234         
235          $body_fill = array(
236               'light' => dirname(__FILE__).'/../alpha-img/gradient-l.png',
237               'medium' => dirname(__FILE__).'/../alpha-img/gradient-m.png',
238               'dark' => dirname(__FILE__).'/../alpha-img/gradient-d.png'
239          );
240         
241          $body_g = isset($body_fill[$gradient]) ? $body_fill[$gradient] : false;
242         
243          if ($top_image == 'custom' && $uploaded) {
244               $page_t = $uploaded;
245          } else {
246               $page_t = dirname(__FILE__).'/../alpha-img/page-t/'.$top_image.'.png';
247          }
248         
249          $body_bg = dirname(__FILE__).'/../alpha-img/body-bg.png';
250          $page_t_mask = dirname(__FILE__).'/../alpha-img/page-t/image-mask.png';
251          $page_b = dirname(__FILE__).'/../alpha-img/page-b.png';
252          $comment_t = dirname(__FILE__).'/../alpha-img/comment-t.png';
253          $comment_b = dirname(__FILE__).'/../alpha-img/comment-b.png';
254          $default_bg = '#e0e0e0';
255          $default_prelude = '#ededed';
256         
257          self::dropImage(basename($body_bg));
258          self::dropImage('page-t.png');
259          self::dropImage(basename($page_b));
260          self::dropImage(basename($comment_t));
261          self::dropImage(basename($comment_b));
262         
263          $body_color = self::adjustColor($body_color);
264          $prelude_color = self::adjustColor($prelude_color);
265          $comment_color = self::adjustColor($comment_color);
266         
267          if ($top_image || $body_color || $gradient != 'light' || $prelude_color || $uploaded)
268          {
269               if (!$body_color) {
270                    $body_color = $default_bg;
271               }
272               $body_color = sscanf($body_color,'#%2X%2X%2X');
273               
274               # Create body gradient with color
275               $d_body_bg = imagecreatetruecolor(50,180);
276               $fill = imagecolorallocate($d_body_bg,$body_color[0],$body_color[1],$body_color[2]);
277               imagefill($d_body_bg,0,0,$fill);
278               
279               # User choosed a gradient
280               if ($body_g) {
281                    $s_body_bg = imagecreatefrompng($body_g);
282                    imagealphablending($s_body_bg,true);
283                    imagecopy($d_body_bg,$s_body_bg,0,0,0,0,50,180);
284                    imagedestroy($s_body_bg);
285               }
286               
287               if (!$prelude_color) {
288                    $prelude_color = $default_prelude;
289               }
290               $prelude_color = sscanf($prelude_color,'#%2X%2X%2X');
291               
292               $s_prelude = imagecreatetruecolor(50,30);
293               $fill = imagecolorallocate($s_prelude,$prelude_color[0],$prelude_color[1],$prelude_color[2]);
294               imagefill($s_prelude,0,0,$fill);
295               imagecopy($d_body_bg,$s_prelude,0,0,0,0,50,30);
296               
297               imagepng($d_body_bg,self::imagesPath().'/'.basename($body_bg));
298          }
299         
300          if ($top_image || $body_color || $gradient != 'light')
301          {
302               # Create top image from uploaded image
303               $size = getimagesize($page_t);
304               $size = $size[1];
305               $type = files::getMimeType($page_t);
306               
307               $d_page_t = imagecreatetruecolor(800,$size);
308               
309               if ($type == 'image/png') {
310                    $s_page_t = @imagecreatefrompng($page_t);
311               } else {
312                    $s_page_t = @imagecreatefromjpeg($page_t);
313               }
314               
315               if (!$s_page_t) {
316                    throw new exception(__('Unable to open image.'));
317               }
318               
319               $fill = imagecolorallocate($d_page_t,$body_color[0],$body_color[1],$body_color[2]);
320               imagefill($d_page_t,0,0,$fill);
321               
322               if ($type == 'image/png')
323               {
324                    # PNG, we only add body gradient and image
325                    imagealphablending($s_page_t,true);
326                    imagecopyresized($d_page_t,$d_body_bg,0,0,0,50,800,130,50,130);
327                    imagecopy($d_page_t,$s_page_t,0,0,0,0,800,$size);
328               }
329               else
330               {
331                    # JPEG, we add image and a frame with rounded corners
332                    imagecopy($d_page_t,$s_page_t,0,0,0,0,800,$size);
333                   
334                    imagecopy($d_page_t,$d_body_bg,0,0,0,50,8,4);
335                    imagecopy($d_page_t,$d_body_bg,0,4,0,54,4,4);
336                    imagecopy($d_page_t,$d_body_bg,792,0,0,50,8,4);
337                    imagecopy($d_page_t,$d_body_bg,796,4,0,54,4,4);
338                   
339                    $mask = imagecreatefrompng($page_t_mask);
340                    imagealphablending($mask,true);
341                    imagecopy($d_page_t,$mask,0,0,0,0,800,11);
342                    imagedestroy($mask);
343                   
344                    $fill = imagecolorallocate($d_page_t,255,255,255);
345                    imagefilledrectangle($d_page_t,0,11,3,$size-1,$fill);
346                    imagefilledrectangle($d_page_t,796,11,799,$size-1,$fill);
347                    imagefilledrectangle($d_page_t,0,$size-9,799,$size-1,$fill);
348               }
349               
350               $config['top_height'] = ($size).'px';
351               
352               imagepng($d_page_t,self::imagesPath().'/page-t.png');
353               
354               imagedestroy($d_body_bg);
355               imagedestroy($d_page_t);
356               imagedestroy($s_page_t);
357               
358               # Create bottom image with color
359               $d_page_b = imagecreatetruecolor(800,8);
360               $fill = imagecolorallocate($d_page_b,$body_color[0],$body_color[1],$body_color[2]);
361               imagefill($d_page_b,0,0,$fill);
362               
363               $s_page_b = imagecreatefrompng($page_b);
364               imagealphablending($s_page_b,true);
365               imagecopy($d_page_b,$s_page_b,0,0,0,0,800,160);
366               
367               imagepng($d_page_b,self::imagesPath().'/'.basename($page_b));
368               
369               imagedestroy($d_page_b);
370               imagedestroy($s_page_b);
371          }
372         
373          if ($comment_color) {
374               self::commentImages($comment_color,$comment_t,$comment_b,basename($comment_t),basename($comment_b));
375          }
376          if ($comment_color_my) {
377               self::commentImages($comment_color_my,$comment_t,$comment_b,'commentmy-t.png','commentmy-b.png');
378          }
379     }
380     
381     protected static function commentImages($comment_color,$comment_t,$comment_b,$dest_t,$dest_b)
382     {
383          $comment_color = sscanf($comment_color,'#%2X%2X%2X');
384               
385          $d_comment_t = imagecreatetruecolor(500,25);
386          $fill = imagecolorallocate($d_comment_t,$comment_color[0],$comment_color[1],$comment_color[2]);
387          imagefill($d_comment_t,0,0,$fill);
388         
389          $s_comment_t = imagecreatefrompng($comment_t);
390          imagealphablending($s_comment_t,true);
391          imagecopy($d_comment_t,$s_comment_t,0,0,0,0,500,25);
392         
393          imagepng($d_comment_t,self::imagesPath().'/'.$dest_t);
394          imagedestroy($d_comment_t);
395          imagedestroy($s_comment_t);
396         
397          $d_comment_b = imagecreatetruecolor(500,7);
398          $fill = imagecolorallocate($d_comment_b,$comment_color[0],$comment_color[1],$comment_color[2]);
399          imagefill($d_comment_b,0,0,$fill);
400         
401          $s_comment_b = imagecreatefrompng($comment_b);
402          imagealphablending($s_comment_b,true);
403          imagecopy($d_comment_b,$s_comment_b,0,0,0,0,500,7);
404         
405          imagepng($d_comment_b,self::imagesPath().'/'.$dest_b);
406          imagedestroy($d_comment_b);
407          imagedestroy($s_comment_b);
408     }
409     
410     public static function dropImage($img)
411     {
412          $img = path::real(self::imagesPath().'/'.$img);
413          if (is_writable(dirname($img))) {
414               @unlink($img);
415               @unlink(dirname($img).'/.'.basename($img,'.png').'_sq.jpg');
416               @unlink(dirname($img).'/.'.basename($img,'.png').'_m.jpg');
417               @unlink(dirname($img).'/.'.basename($img,'.png').'_s.jpg');
418               @unlink(dirname($img).'/.'.basename($img,'.png').'_sq.jpg');
419               @unlink(dirname($img).'/.'.basename($img,'.png').'_t.jpg');
420          }
421     }
422}
423?>
Note: See TracBrowser for help on using the repository browser.

Sites map