示例#1
0
    public function action__content()
    {
        // load posts/pages
        // parse and create thumbnails
        // in the future, I could alert about unused images/refs, dead links, etc.
        $images = [];
        $subvert_options = ['image_callback' => function (&$attributes) use(&$images) {
            $src = $attributes['src'];
            if (!isset($images[$src])) {
                $images[$src] = [];
            }
            $width = isset($attributes['width']) && ctype_digit($attributes['width']) ? (int) $attributes['width'] : 0;
            $height = isset($attributes['height']) && ctype_digit($attributes['height']) ? (int) $attributes['height'] : 0;
            $key = sprintf('%sx%s', $width, $height);
            if (!isset($images[$src][$key])) {
                $images[$src][$key] = ['width' => $width, 'height' => $height];
            }
        }, 'link_callback' => function (&$attributes) {
        }];
        $res = Database::query('
			SELECT Content FROM Objects
			WHERE Type IN (:types)
		', ['types' => ['post', 'page']]);
        foreach ($res->column() as $content) {
            Subvert::Parse($content, $subvert_options);
        }
        Log::time('load+subvert');
        ksort($images, SORT_NATURAL);
        foreach ($images as $image_path => $versions) {
            foreach ($versions as $version => $dimensions) {
                if ($version !== '0x0') {
                    $parts = pathinfo($image_path);
                    $thumbnail_path = sprintf('%s/%s-%s.%s', $parts['dirname'], $parts['filename'], $version, $parts['extension']);
                    Log::debug("[resize] {$thumbnail_path}]");
                    $src = BRAMBLE_DIR . $image_path;
                    $dst = BRAMBLE_DIR . $thumbnail_path;
                    if (file_exists($src)) {
                        $image = GdImage::load($src);
                        $image->resize($dimensions['width'], $dimensions['height'])->save($dst)->dispose();
                        $image->dispose();
                    }
                }
            }
        }
        Log::time('thumbnails');
        return NULL;
    }
示例#2
0
 public function before()
 {
     parent::before();
     $this->m_manager = TemplateManager::create(BRAMBLE_TEMPLATES);
     $this->m_manager->register('subvert', function ($options, $context, $text) {
         $subvert_options = [];
         $subvert_options['image_callback'] = function (&$attributes) {
             // add dimensions to url
             $width = isset($attributes['width']) ? $attributes['width'] : '0';
             $height = isset($attributes['height']) ? $attributes['height'] : '0';
             if (ctype_digit($width) && ctype_digit($height) && ($width != 0 || $height != 0)) {
                 $parts = pathinfo($attributes['src']);
                 $attributes['src'] = sprintf('%s/%s-%sx%s.%s', $parts['dirname'], $parts['filename'], $width, $height, $parts['extension']);
             }
         };
         if ($options) {
             if (isset($options['code'])) {
                 $subvert_options['code_formatting'] = true;
             }
             if (isset($options['root'])) {
                 $subvert_options['root_url'] = BRAMBLE_URL;
             }
             if (isset($options['header'])) {
                 $subvert_options['header_level'] = (int) $options['header'];
             } else {
                 // does it make sense to have this here?
                 $subvert_options['header_level'] = 3;
             }
         }
         return Subvert::Parse($text, $subvert_options);
     });
     $this->m_manager->register('format-date', function ($options, $context, $date) {
         if (is_string($date)) {
             $date = strtotime($date);
         }
         if ($options) {
             if (isset($options['atom'])) {
                 $date = date(\DateTime::ATOM, $date);
             } else {
                 if (count($options) === 1) {
                     $date = date(trim(key($options), "'"), $date);
                 }
             }
         } else {
             $date = date('Y-m-d', $date);
         }
         return $date;
     });
     $this->m_manager->register('format-url', function ($options, $context) {
         if ($options && count($options) === 1) {
             if (isset($options['post'])) {
                 $time = $context['time'];
                 if (is_string($time)) {
                     $time = strtotime($time);
                 }
                 return self::_format_post_url($time, $context['slug']);
             } else {
                 if (isset($options['page'])) {
                     return self::_format_page_url($context['slug']);
                 } else {
                     if (isset($options['category'])) {
                         return self::_format_category_url($context['slug']);
                     } else {
                         if (isset($options['archive'])) {
                             $time = $context['month'];
                             if (is_string($time)) {
                                 $time = strtotime($time);
                             }
                             return self::_format_archive_url($time);
                         }
                     }
                 }
             }
         }
         return '';
     });
 }