1 | <?php |
---|
2 | # -- BEGIN LICENSE BLOCK --------------------------------------- |
---|
3 | # |
---|
4 | # This file is part of Dotclear 2. |
---|
5 | # |
---|
6 | # Copyright (c) 2003-2013 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 ----------------------------------------- |
---|
12 | if (!defined('DC_RC_PATH')) { return; } |
---|
13 | |
---|
14 | class 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 cleanCSS($css) |
---|
138 | { |
---|
139 | // TODO ? |
---|
140 | return $css; |
---|
141 | } |
---|
142 | |
---|
143 | public static function imagesPath() |
---|
144 | { |
---|
145 | global $core; |
---|
146 | return path::real($core->blog->public_path).'/blowup-images'; |
---|
147 | } |
---|
148 | |
---|
149 | public static function imagesURL() |
---|
150 | { |
---|
151 | global $core; |
---|
152 | return $core->blog->settings->system->public_url.'/blowup-images'; |
---|
153 | } |
---|
154 | |
---|
155 | public static function canWriteImages($create=false) |
---|
156 | { |
---|
157 | global $core; |
---|
158 | |
---|
159 | $public = path::real($core->blog->public_path); |
---|
160 | $imgs = self::imagesPath(); |
---|
161 | |
---|
162 | if (!function_exists('imagecreatetruecolor') || !function_exists('imagepng') || !function_exists('imagecreatefrompng')) { |
---|
163 | $core->error->add(__('At least one of the following functions is not available: '. |
---|
164 | 'imagecreatetruecolor, imagepng & imagecreatefrompng.')); |
---|
165 | return false; |
---|
166 | } |
---|
167 | |
---|
168 | if (!is_dir($public)) { |
---|
169 | $core->error->add(__('The \'public\' directory does not exist.')); |
---|
170 | return false; |
---|
171 | } |
---|
172 | |
---|
173 | if (!is_dir($imgs)) { |
---|
174 | if (!is_writable($public)) { |
---|
175 | $core->error->add(sprintf(__('The \'%s\' directory cannot be modified.'),'public')); |
---|
176 | return false; |
---|
177 | } |
---|
178 | if ($create) { |
---|
179 | files::makeDir($imgs); |
---|
180 | } |
---|
181 | return true; |
---|
182 | } |
---|
183 | |
---|
184 | if (!is_writable($imgs)) { |
---|
185 | $core->error->add(sprintf(__('The \'%s\' directory cannot be modified.'),'public/blowup-images')); |
---|
186 | return false; |
---|
187 | } |
---|
188 | |
---|
189 | return true; |
---|
190 | } |
---|
191 | |
---|
192 | public static function uploadImage($f) |
---|
193 | { |
---|
194 | if (!self::canWriteImages(true)) { |
---|
195 | throw new Exception(__('Unable to create images.')); |
---|
196 | } |
---|
197 | |
---|
198 | $name = $f['name']; |
---|
199 | $type = files::getMimeType($name); |
---|
200 | |
---|
201 | if ($type != 'image/jpeg' && $type != 'image/png') { |
---|
202 | throw new Exception(__('Invalid file type.')); |
---|
203 | } |
---|
204 | |
---|
205 | $dest = self::imagesPath().'/uploaded'.($type == 'image/png' ? '.png' : '.jpg'); |
---|
206 | |
---|
207 | if (@move_uploaded_file($f['tmp_name'],$dest) === false) { |
---|
208 | throw new Exception(__('An error occurred while writing the file.')); |
---|
209 | } |
---|
210 | |
---|
211 | $s = getimagesize($dest); |
---|
212 | if ($s[0] != 800) { |
---|
213 | throw new Exception(__('Uploaded image is not 800 pixels wide.')); |
---|
214 | } |
---|
215 | |
---|
216 | return $dest; |
---|
217 | } |
---|
218 | |
---|
219 | public static function createImages(&$config,$uploaded) |
---|
220 | { |
---|
221 | $body_color = $config['body_bg_c']; |
---|
222 | $prelude_color = $config['prelude_c']; |
---|
223 | $gradient = $config['body_bg_g']; |
---|
224 | $comment_color = $config['post_comment_bg_c']; |
---|
225 | $comment_color_my = $config['post_commentmy_bg_c']; |
---|
226 | $top_image = $config['top_image']; |
---|
227 | |
---|
228 | $config['top_height'] = null; |
---|
229 | |
---|
230 | if ($top_image != 'custom' && !isset(self::$top_images[$top_image])) { |
---|
231 | $top_image = 'default'; |
---|
232 | } |
---|
233 | if ($uploaded && !is_file($uploaded)) { |
---|
234 | $uploaded = null; |
---|
235 | } |
---|
236 | |
---|
237 | if (!self::canWriteImages(true)) { |
---|
238 | throw new Exception(__('Unable to create images.')); |
---|
239 | } |
---|
240 | |
---|
241 | $body_fill = array( |
---|
242 | 'light' => dirname(__FILE__).'/../alpha-img/gradient-l.png', |
---|
243 | 'medium' => dirname(__FILE__).'/../alpha-img/gradient-m.png', |
---|
244 | 'dark' => dirname(__FILE__).'/../alpha-img/gradient-d.png' |
---|
245 | ); |
---|
246 | |
---|
247 | $body_g = isset($body_fill[$gradient]) ? $body_fill[$gradient] : false; |
---|
248 | |
---|
249 | if ($top_image == 'custom' && $uploaded) { |
---|
250 | $page_t = $uploaded; |
---|
251 | } else { |
---|
252 | $page_t = dirname(__FILE__).'/../alpha-img/page-t/'.$top_image.'.png'; |
---|
253 | } |
---|
254 | |
---|
255 | $body_bg = dirname(__FILE__).'/../alpha-img/body-bg.png'; |
---|
256 | $page_t_mask = dirname(__FILE__).'/../alpha-img/page-t/image-mask.png'; |
---|
257 | $page_b = dirname(__FILE__).'/../alpha-img/page-b.png'; |
---|
258 | $comment_t = dirname(__FILE__).'/../alpha-img/comment-t.png'; |
---|
259 | $comment_b = dirname(__FILE__).'/../alpha-img/comment-b.png'; |
---|
260 | $default_bg = '#e0e0e0'; |
---|
261 | $default_prelude = '#ededed'; |
---|
262 | |
---|
263 | self::dropImage(basename($body_bg)); |
---|
264 | self::dropImage('page-t.png'); |
---|
265 | self::dropImage(basename($page_b)); |
---|
266 | self::dropImage(basename($comment_t)); |
---|
267 | self::dropImage(basename($comment_b)); |
---|
268 | |
---|
269 | $body_color = self::adjustColor($body_color); |
---|
270 | $prelude_color = self::adjustColor($prelude_color); |
---|
271 | $comment_color = self::adjustColor($comment_color); |
---|
272 | |
---|
273 | if ($top_image || $body_color || $gradient != 'light' || $prelude_color || $uploaded) |
---|
274 | { |
---|
275 | if (!$body_color) { |
---|
276 | $body_color = $default_bg; |
---|
277 | } |
---|
278 | $body_color = sscanf($body_color,'#%2X%2X%2X'); |
---|
279 | |
---|
280 | # Create body gradient with color |
---|
281 | $d_body_bg = imagecreatetruecolor(50,180); |
---|
282 | $fill = imagecolorallocate($d_body_bg,$body_color[0],$body_color[1],$body_color[2]); |
---|
283 | imagefill($d_body_bg,0,0,$fill); |
---|
284 | |
---|
285 | # User choosed a gradient |
---|
286 | if ($body_g) { |
---|
287 | $s_body_bg = imagecreatefrompng($body_g); |
---|
288 | imagealphablending($s_body_bg,true); |
---|
289 | imagecopy($d_body_bg,$s_body_bg,0,0,0,0,50,180); |
---|
290 | imagedestroy($s_body_bg); |
---|
291 | } |
---|
292 | |
---|
293 | if (!$prelude_color) { |
---|
294 | $prelude_color = $default_prelude; |
---|
295 | } |
---|
296 | $prelude_color = sscanf($prelude_color,'#%2X%2X%2X'); |
---|
297 | |
---|
298 | $s_prelude = imagecreatetruecolor(50,30); |
---|
299 | $fill = imagecolorallocate($s_prelude,$prelude_color[0],$prelude_color[1],$prelude_color[2]); |
---|
300 | imagefill($s_prelude,0,0,$fill); |
---|
301 | imagecopy($d_body_bg,$s_prelude,0,0,0,0,50,30); |
---|
302 | |
---|
303 | imagepng($d_body_bg,self::imagesPath().'/'.basename($body_bg)); |
---|
304 | } |
---|
305 | |
---|
306 | if ($top_image || $body_color || $gradient != 'light') |
---|
307 | { |
---|
308 | # Create top image from uploaded image |
---|
309 | $size = getimagesize($page_t); |
---|
310 | $size = $size[1]; |
---|
311 | $type = files::getMimeType($page_t); |
---|
312 | |
---|
313 | $d_page_t = imagecreatetruecolor(800,$size); |
---|
314 | |
---|
315 | if ($type == 'image/png') { |
---|
316 | $s_page_t = @imagecreatefrompng($page_t); |
---|
317 | } else { |
---|
318 | $s_page_t = @imagecreatefromjpeg($page_t); |
---|
319 | } |
---|
320 | |
---|
321 | if (!$s_page_t) { |
---|
322 | throw new exception(__('Unable to open image.')); |
---|
323 | } |
---|
324 | |
---|
325 | $fill = imagecolorallocate($d_page_t,$body_color[0],$body_color[1],$body_color[2]); |
---|
326 | imagefill($d_page_t,0,0,$fill); |
---|
327 | |
---|
328 | if ($type == 'image/png') |
---|
329 | { |
---|
330 | # PNG, we only add body gradient and image |
---|
331 | imagealphablending($s_page_t,true); |
---|
332 | imagecopyresized($d_page_t,$d_body_bg,0,0,0,50,800,130,50,130); |
---|
333 | imagecopy($d_page_t,$s_page_t,0,0,0,0,800,$size); |
---|
334 | } |
---|
335 | else |
---|
336 | { |
---|
337 | # JPEG, we add image and a frame with rounded corners |
---|
338 | imagecopy($d_page_t,$s_page_t,0,0,0,0,800,$size); |
---|
339 | |
---|
340 | imagecopy($d_page_t,$d_body_bg,0,0,0,50,8,4); |
---|
341 | imagecopy($d_page_t,$d_body_bg,0,4,0,54,4,4); |
---|
342 | imagecopy($d_page_t,$d_body_bg,792,0,0,50,8,4); |
---|
343 | imagecopy($d_page_t,$d_body_bg,796,4,0,54,4,4); |
---|
344 | |
---|
345 | $mask = imagecreatefrompng($page_t_mask); |
---|
346 | imagealphablending($mask,true); |
---|
347 | imagecopy($d_page_t,$mask,0,0,0,0,800,11); |
---|
348 | imagedestroy($mask); |
---|
349 | |
---|
350 | $fill = imagecolorallocate($d_page_t,255,255,255); |
---|
351 | imagefilledrectangle($d_page_t,0,11,3,$size-1,$fill); |
---|
352 | imagefilledrectangle($d_page_t,796,11,799,$size-1,$fill); |
---|
353 | imagefilledrectangle($d_page_t,0,$size-9,799,$size-1,$fill); |
---|
354 | } |
---|
355 | |
---|
356 | $config['top_height'] = ($size).'px'; |
---|
357 | |
---|
358 | imagepng($d_page_t,self::imagesPath().'/page-t.png'); |
---|
359 | |
---|
360 | imagedestroy($d_body_bg); |
---|
361 | imagedestroy($d_page_t); |
---|
362 | imagedestroy($s_page_t); |
---|
363 | |
---|
364 | # Create bottom image with color |
---|
365 | $d_page_b = imagecreatetruecolor(800,8); |
---|
366 | $fill = imagecolorallocate($d_page_b,$body_color[0],$body_color[1],$body_color[2]); |
---|
367 | imagefill($d_page_b,0,0,$fill); |
---|
368 | |
---|
369 | $s_page_b = imagecreatefrompng($page_b); |
---|
370 | imagealphablending($s_page_b,true); |
---|
371 | imagecopy($d_page_b,$s_page_b,0,0,0,0,800,160); |
---|
372 | |
---|
373 | imagepng($d_page_b,self::imagesPath().'/'.basename($page_b)); |
---|
374 | |
---|
375 | imagedestroy($d_page_b); |
---|
376 | imagedestroy($s_page_b); |
---|
377 | } |
---|
378 | |
---|
379 | if ($comment_color) { |
---|
380 | self::commentImages($comment_color,$comment_t,$comment_b,basename($comment_t),basename($comment_b)); |
---|
381 | } |
---|
382 | if ($comment_color_my) { |
---|
383 | self::commentImages($comment_color_my,$comment_t,$comment_b,'commentmy-t.png','commentmy-b.png'); |
---|
384 | } |
---|
385 | } |
---|
386 | |
---|
387 | protected static function commentImages($comment_color,$comment_t,$comment_b,$dest_t,$dest_b) |
---|
388 | { |
---|
389 | $comment_color = sscanf($comment_color,'#%2X%2X%2X'); |
---|
390 | |
---|
391 | $d_comment_t = imagecreatetruecolor(500,25); |
---|
392 | $fill = imagecolorallocate($d_comment_t,$comment_color[0],$comment_color[1],$comment_color[2]); |
---|
393 | imagefill($d_comment_t,0,0,$fill); |
---|
394 | |
---|
395 | $s_comment_t = imagecreatefrompng($comment_t); |
---|
396 | imagealphablending($s_comment_t,true); |
---|
397 | imagecopy($d_comment_t,$s_comment_t,0,0,0,0,500,25); |
---|
398 | |
---|
399 | imagepng($d_comment_t,self::imagesPath().'/'.$dest_t); |
---|
400 | imagedestroy($d_comment_t); |
---|
401 | imagedestroy($s_comment_t); |
---|
402 | |
---|
403 | $d_comment_b = imagecreatetruecolor(500,7); |
---|
404 | $fill = imagecolorallocate($d_comment_b,$comment_color[0],$comment_color[1],$comment_color[2]); |
---|
405 | imagefill($d_comment_b,0,0,$fill); |
---|
406 | |
---|
407 | $s_comment_b = imagecreatefrompng($comment_b); |
---|
408 | imagealphablending($s_comment_b,true); |
---|
409 | imagecopy($d_comment_b,$s_comment_b,0,0,0,0,500,7); |
---|
410 | |
---|
411 | imagepng($d_comment_b,self::imagesPath().'/'.$dest_b); |
---|
412 | imagedestroy($d_comment_b); |
---|
413 | imagedestroy($s_comment_b); |
---|
414 | } |
---|
415 | |
---|
416 | public static function dropImage($img) |
---|
417 | { |
---|
418 | $img = path::real(self::imagesPath().'/'.$img); |
---|
419 | if (is_writable(dirname($img))) { |
---|
420 | @unlink($img); |
---|
421 | @unlink(dirname($img).'/.'.basename($img,'.png').'_sq.jpg'); |
---|
422 | @unlink(dirname($img).'/.'.basename($img,'.png').'_m.jpg'); |
---|
423 | @unlink(dirname($img).'/.'.basename($img,'.png').'_s.jpg'); |
---|
424 | @unlink(dirname($img).'/.'.basename($img,'.png').'_sq.jpg'); |
---|
425 | @unlink(dirname($img).'/.'.basename($img,'.png').'_t.jpg'); |
---|
426 | } |
---|
427 | } |
---|
428 | } |
---|
429 | ?> |
---|