[0] | 1 | <?php |
---|
| 2 | # -- BEGIN LICENSE BLOCK --------------------------------------- |
---|
| 3 | # |
---|
| 4 | # This file is part of Dotclear 2. |
---|
| 5 | # |
---|
[1179] | 6 | # Copyright (c) 2003-2013 Olivier Meunier & Association Dotclear |
---|
[0] | 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 | ), |
---|
[295] | 24 | |
---|
[0] | 25 | 'serif' => array( |
---|
| 26 | 's1' => 'Times, "Times New Roman", serif', |
---|
| 27 | 's2' => 'Georgia, serif', |
---|
| 28 | 's3' => 'Baskerville, "Palatino Linotype", serif' |
---|
| 29 | ), |
---|
[295] | 30 | |
---|
[0] | 31 | 'monospace' => array( |
---|
| 32 | 'm1' => '"Andale Mono", "Courier New", monospace', |
---|
| 33 | 'm2' => '"Courier New", Courier, mono, monospace' |
---|
| 34 | ) |
---|
| 35 | ); |
---|
[295] | 36 | |
---|
[0] | 37 | protected static $fonts_combo = array(); |
---|
| 38 | protected static $fonts_list = array(); |
---|
[295] | 39 | |
---|
[0] | 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', |
---|
[696] | 55 | 'typo' => 'Typo' |
---|
[0] | 56 | ); |
---|
[295] | 57 | |
---|
[0] | 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 | } |
---|
[295] | 72 | |
---|
[0] | 73 | return self::$fonts_combo; |
---|
| 74 | } |
---|
[295] | 75 | |
---|
[0] | 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 | } |
---|
[295] | 87 | |
---|
[0] | 88 | return isset(self::$fonts_list[$c]) ? self::$fonts_list[$c] : null; |
---|
| 89 | } |
---|
[295] | 90 | |
---|
[0] | 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 | } |
---|
[295] | 99 | |
---|
[0] | 100 | return null; |
---|
| 101 | } |
---|
[295] | 102 | |
---|
[0] | 103 | public static function adjustPosition($p) |
---|
| 104 | { |
---|
| 105 | if (!preg_match('/^[0-9]+(:[0-9]+)?$/',$p)) { |
---|
| 106 | return null; |
---|
| 107 | } |
---|
[295] | 108 | |
---|
[0] | 109 | $p = explode(':',$p); |
---|
[295] | 110 | |
---|
[0] | 111 | return $p[0].(count($p) == 1 ? ':0' : ':'.$p[1]); |
---|
| 112 | } |
---|
[295] | 113 | |
---|
[0] | 114 | public static function adjustColor($c) |
---|
| 115 | { |
---|
| 116 | if ($c === '') { |
---|
| 117 | return ''; |
---|
| 118 | } |
---|
[295] | 119 | |
---|
[0] | 120 | $c = strtoupper($c); |
---|
[295] | 121 | |
---|
[0] | 122 | if (preg_match('/^[A-F0-9]{3,6}$/',$c)) { |
---|
| 123 | $c = '#'.$c; |
---|
| 124 | } |
---|
[295] | 125 | |
---|
[0] | 126 | if (preg_match('/^#[A-F0-9]{6}$/',$c)) { |
---|
| 127 | return $c; |
---|
| 128 | } |
---|
[295] | 129 | |
---|
[0] | 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 | } |
---|
[295] | 133 | |
---|
[0] | 134 | return ''; |
---|
| 135 | } |
---|
[295] | 136 | |
---|
| 137 | public static function cleanCSS($css) |
---|
| 138 | { |
---|
| 139 | // TODO ? |
---|
| 140 | return $css; |
---|
| 141 | } |
---|
| 142 | |
---|
[0] | 143 | public static function imagesPath() |
---|
| 144 | { |
---|
| 145 | global $core; |
---|
| 146 | return path::real($core->blog->public_path).'/blowup-images'; |
---|
| 147 | } |
---|
[295] | 148 | |
---|
[0] | 149 | public static function imagesURL() |
---|
| 150 | { |
---|
| 151 | global $core; |
---|
| 152 | return $core->blog->settings->system->public_url.'/blowup-images'; |
---|
| 153 | } |
---|
[295] | 154 | |
---|
[0] | 155 | public static function canWriteImages($create=false) |
---|
| 156 | { |
---|
| 157 | global $core; |
---|
[295] | 158 | |
---|
[0] | 159 | $public = path::real($core->blog->public_path); |
---|
| 160 | $imgs = self::imagesPath(); |
---|
[295] | 161 | |
---|
[0] | 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 | } |
---|
[295] | 167 | |
---|
[0] | 168 | if (!is_dir($public)) { |
---|
| 169 | $core->error->add(__('The \'public\' directory does not exist.')); |
---|
| 170 | return false; |
---|
| 171 | } |
---|
[295] | 172 | |
---|
[0] | 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 | } |
---|
[295] | 183 | |
---|
[0] | 184 | if (!is_writable($imgs)) { |
---|
| 185 | $core->error->add(sprintf(__('The \'%s\' directory cannot be modified.'),'public/blowup-images')); |
---|
| 186 | return false; |
---|
| 187 | } |
---|
[295] | 188 | |
---|
[0] | 189 | return true; |
---|
| 190 | } |
---|
[295] | 191 | |
---|
[0] | 192 | public static function uploadImage($f) |
---|
| 193 | { |
---|
| 194 | if (!self::canWriteImages(true)) { |
---|
| 195 | throw new Exception(__('Unable to create images.')); |
---|
| 196 | } |
---|
[295] | 197 | |
---|
[0] | 198 | $name = $f['name']; |
---|
| 199 | $type = files::getMimeType($name); |
---|
[295] | 200 | |
---|
[0] | 201 | if ($type != 'image/jpeg' && $type != 'image/png') { |
---|
| 202 | throw new Exception(__('Invalid file type.')); |
---|
| 203 | } |
---|
[295] | 204 | |
---|
[0] | 205 | $dest = self::imagesPath().'/uploaded'.($type == 'image/png' ? '.png' : '.jpg'); |
---|
[295] | 206 | |
---|
[0] | 207 | if (@move_uploaded_file($f['tmp_name'],$dest) === false) { |
---|
| 208 | throw new Exception(__('An error occurred while writing the file.')); |
---|
| 209 | } |
---|
[295] | 210 | |
---|
[0] | 211 | $s = getimagesize($dest); |
---|
| 212 | if ($s[0] != 800) { |
---|
| 213 | throw new Exception(__('Uploaded image is not 800 pixels wide.')); |
---|
| 214 | } |
---|
[295] | 215 | |
---|
[0] | 216 | return $dest; |
---|
| 217 | } |
---|
[295] | 218 | |
---|
[0] | 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']; |
---|
[295] | 227 | |
---|
[0] | 228 | $config['top_height'] = null; |
---|
[295] | 229 | |
---|
[0] | 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 | } |
---|
[295] | 236 | |
---|
[0] | 237 | if (!self::canWriteImages(true)) { |
---|
| 238 | throw new Exception(__('Unable to create images.')); |
---|
| 239 | } |
---|
[295] | 240 | |
---|
[0] | 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 | ); |
---|
[295] | 246 | |
---|
[0] | 247 | $body_g = isset($body_fill[$gradient]) ? $body_fill[$gradient] : false; |
---|
[295] | 248 | |
---|
[0] | 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 | } |
---|
[295] | 254 | |
---|
[0] | 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'; |
---|
[295] | 262 | |
---|
[0] | 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)); |
---|
[295] | 268 | |
---|
[0] | 269 | $body_color = self::adjustColor($body_color); |
---|
| 270 | $prelude_color = self::adjustColor($prelude_color); |
---|
| 271 | $comment_color = self::adjustColor($comment_color); |
---|
[295] | 272 | |
---|
[0] | 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'); |
---|
[295] | 279 | |
---|
[0] | 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); |
---|
[295] | 284 | |
---|
[0] | 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 | } |
---|
[295] | 292 | |
---|
[0] | 293 | if (!$prelude_color) { |
---|
| 294 | $prelude_color = $default_prelude; |
---|
| 295 | } |
---|
| 296 | $prelude_color = sscanf($prelude_color,'#%2X%2X%2X'); |
---|
[295] | 297 | |
---|
[0] | 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); |
---|
[295] | 302 | |
---|
[0] | 303 | imagepng($d_body_bg,self::imagesPath().'/'.basename($body_bg)); |
---|
| 304 | } |
---|
[295] | 305 | |
---|
[0] | 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); |
---|
[295] | 312 | |
---|
[0] | 313 | $d_page_t = imagecreatetruecolor(800,$size); |
---|
[295] | 314 | |
---|
[0] | 315 | if ($type == 'image/png') { |
---|
| 316 | $s_page_t = @imagecreatefrompng($page_t); |
---|
| 317 | } else { |
---|
| 318 | $s_page_t = @imagecreatefromjpeg($page_t); |
---|
| 319 | } |
---|
[295] | 320 | |
---|
[0] | 321 | if (!$s_page_t) { |
---|
| 322 | throw new exception(__('Unable to open image.')); |
---|
| 323 | } |
---|
[295] | 324 | |
---|
[0] | 325 | $fill = imagecolorallocate($d_page_t,$body_color[0],$body_color[1],$body_color[2]); |
---|
| 326 | imagefill($d_page_t,0,0,$fill); |
---|
[295] | 327 | |
---|
[0] | 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); |
---|
[295] | 339 | |
---|
[0] | 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); |
---|
[295] | 344 | |
---|
[0] | 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); |
---|
[295] | 349 | |
---|
[0] | 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 | } |
---|
[295] | 355 | |
---|
[0] | 356 | $config['top_height'] = ($size).'px'; |
---|
[295] | 357 | |
---|
[0] | 358 | imagepng($d_page_t,self::imagesPath().'/page-t.png'); |
---|
[295] | 359 | |
---|
[0] | 360 | imagedestroy($d_body_bg); |
---|
| 361 | imagedestroy($d_page_t); |
---|
| 362 | imagedestroy($s_page_t); |
---|
[295] | 363 | |
---|
[0] | 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); |
---|
[295] | 368 | |
---|
[0] | 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); |
---|
[295] | 372 | |
---|
[0] | 373 | imagepng($d_page_b,self::imagesPath().'/'.basename($page_b)); |
---|
[295] | 374 | |
---|
[0] | 375 | imagedestroy($d_page_b); |
---|
| 376 | imagedestroy($s_page_b); |
---|
| 377 | } |
---|
[295] | 378 | |
---|
[0] | 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 | } |
---|
[295] | 386 | |
---|
[0] | 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'); |
---|
[295] | 390 | |
---|
[0] | 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); |
---|
[295] | 394 | |
---|
[0] | 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); |
---|
[295] | 398 | |
---|
[0] | 399 | imagepng($d_comment_t,self::imagesPath().'/'.$dest_t); |
---|
| 400 | imagedestroy($d_comment_t); |
---|
| 401 | imagedestroy($s_comment_t); |
---|
[295] | 402 | |
---|
[0] | 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); |
---|
[295] | 406 | |
---|
[0] | 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); |
---|
[295] | 410 | |
---|
[0] | 411 | imagepng($d_comment_b,self::imagesPath().'/'.$dest_b); |
---|
| 412 | imagedestroy($d_comment_b); |
---|
| 413 | imagedestroy($s_comment_b); |
---|
| 414 | } |
---|
[295] | 415 | |
---|
[0] | 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 | } |
---|
[295] | 429 | ?> |
---|