]> git.bts.cx Git - garden.git/blobdiff - garden/garden.php
Added image support
[garden.git] / garden / garden.php
index e73804a9061c358297fbc94b8a226fbf073a854e..ada43bb5290854cd9bc6ebc8dc0a38fc9131d5cd 100644 (file)
@@ -220,11 +220,13 @@ function garden_make_directories($content_items) {
                        mkdir($directory , 0777, true); // FIXME, permissions...
                }
        }
                        mkdir($directory , 0777, true); // FIXME, permissions...
                }
        }
+
+       return $content_items;
 }
 
 function garden_move_raw($content_items) {
        foreach ($content_items as $item) {
 }
 
 function garden_move_raw($content_items) {
        foreach ($content_items as $item) {
-               if ($item->type != GardenItemType::Raw && $item->type != GardenItemType::Image) {
+               if ($item->type != GardenItemType::Raw && $item->type != GardenItemType::Image) { // FIXME, do we need to copy images?
                        continue;
                }
 
                        continue;
                }
 
@@ -233,6 +235,8 @@ function garden_move_raw($content_items) {
                        error('Failed to copy file: filename="', $item->source_filename, '"');
                }
        }
                        error('Failed to copy file: filename="', $item->source_filename, '"');
                }
        }
+
+       return $content_items;
 }
 
 ///////////////////////////////////////////////////////////////////////////////
 }
 
 ///////////////////////////////////////////////////////////////////////////////
@@ -281,6 +285,34 @@ function garden_template_render($name, $variables = null) {
        return $output;
 }
 
        return $output;
 }
 
+///////////////////////////////////////////////////////////////////////////////
+// Images
+///////////////////////////////////////////////////////////////////////////////
+
+class GardenImage {
+       public $content_item;
+       public $width;
+       public $height;
+       public $url;
+       public $target_filename;
+       
+       public function __construct($content_item, $width, $height, $url, $target_filename) {
+               $this->content_item = $content_item;
+               $this->width = $width;
+               $this->height = $height;
+               $this->url = $url;
+               $this->target_filename = $target_filename;
+       }
+}
+
+function garden_make_images($post_process_items) {
+       foreach ($post_process_items as $item) {
+               $image = new Imagick($item->content_item->source_filename);
+               $image->thumbnailImage($item->width, $item->height);
+               $image->writeImage($item->target_filename);
+       }
+}
+
 ///////////////////////////////////////////////////////////////////////////////
 // Template helpers
 ///////////////////////////////////////////////////////////////////////////////
 ///////////////////////////////////////////////////////////////////////////////
 // Template helpers
 ///////////////////////////////////////////////////////////////////////////////
@@ -295,6 +327,10 @@ function garden_template_content() {
        return $garden_template_content();
 }
 
        return $garden_template_content();
 }
 
+function garden_site_url(...$url_segments) {
+       return GARDEN_SITE_BASE_URL . garden_url(...$url_segments);
+}
+
 ///////////////////////////////////////////////////////////////////////////////
 // HTML
 ///////////////////////////////////////////////////////////////////////////////
 ///////////////////////////////////////////////////////////////////////////////
 // HTML
 ///////////////////////////////////////////////////////////////////////////////
@@ -302,9 +338,13 @@ function garden_template_content() {
 require_once(garden_path(__DIR__, 'third_party', 'parsedown', 'Parsedown.php'));
 
 class GardenExtendedParsedown extends Parsedown {
 require_once(garden_path(__DIR__, 'third_party', 'parsedown', 'Parsedown.php'));
 
 class GardenExtendedParsedown extends Parsedown {
+       public $post_process_items;
+       private $output_directory;
        private $content_items;
 
        private $content_items;
 
-       public function __construct($content_items) {
+       public function __construct($output_directory, $content_items) {
+               $this->post_process_items = [];
+               $this->output_directory = $output_directory;
                $this->content_items = $content_items;
                $this->InlineTypes['!'][] = 'Youtube';
                $this->InlineTypes['!'][] = 'Image';
                $this->content_items = $content_items;
                $this->InlineTypes['!'][] = 'Youtube';
                $this->InlineTypes['!'][] = 'Image';
@@ -323,18 +363,31 @@ class GardenExtendedParsedown extends Parsedown {
                                return;
                        }
 
                                return;
                        }
 
-                       $target_url = null;
+                       $image_content_item = null;
                        foreach ($this->content_items as $content_item) {
                                if ($content_item->path->basename == $image_name) {
                        foreach ($this->content_items as $content_item) {
                                if ($content_item->path->basename == $image_name) {
-                                       $target_url = $content_item->url;
+                                       $image_content_item = $content_item;
                                        break;
                                }
                        }
 
                                        break;
                                }
                        }
 
-                       if ($target_url == null) {
+                       if ($image_content_item == null) {
                                return;
                        }
 
                                return;
                        }
 
+                       $target_url = $image_content_item->url;
+                       if ($image_width != null && $image_height != null) {
+                               $original_path = $image_content_item->path;
+
+                               $target_basename = $original_path->filename . '_' . $image_width . 'x' . $image_height . '.png';
+
+                               $target_url = GARDEN_SITE_BASE_URL . garden_url($original_path->path, $target_basename);
+                               $target_path = garden_path($this->output_directory, garden_url($original_path->path, $target_basename));
+
+                               $post_process_item = new GardenImage($image_content_item, $image_width, $image_height, $target_url, $target_path);
+                               $this->post_process_items[] = $post_process_item;
+                       }
+
                        return array(
                                'extent' => strlen($matches[0]), 
                                'element' => array(
                        return array(
                                'extent' => strlen($matches[0]), 
                                'element' => array(
@@ -411,8 +464,8 @@ class GardenExtendedParsedown extends Parsedown {
        }
 }
 
        }
 }
 
-function garden_generate_html($content_items) {
-       $parsedown = new GardenExtendedParsedown($content_items);
+function garden_generate_html($output_directory, $content_items) {
+       $output_items = [];
 
        foreach ($content_items as $item) {
                if ($item->type != GardenItemType::Article) {
 
        foreach ($content_items as $item) {
                if ($item->type != GardenItemType::Article) {
@@ -420,12 +473,20 @@ function garden_generate_html($content_items) {
                }
 
                $markdown_data = garden_read_file($item->source_filename);
                }
 
                $markdown_data = garden_read_file($item->source_filename);
+
+               $parsedown = new GardenExtendedParsedown($output_directory, $content_items);
                $markdown_html = $parsedown->text($markdown_data);
                $markdown_html = $parsedown->text($markdown_data);
-               
+
+               if (count($parsedown->post_process_items) > 0) {
+                       array_push($output_items, ...$parsedown->post_process_items);
+               }
+
                $html_data = garden_template_render('article', [ 'article' => $item, 'article_content' => $markdown_html ]);
 
                garden_write_file($item->target_filename, $html_data);
        }
                $html_data = garden_template_render('article', [ 'article' => $item, 'article_content' => $markdown_html ]);
 
                garden_write_file($item->target_filename, $html_data);
        }
+
+       return $output_items;
 }
 
 ///////////////////////////////////////////////////////////////////////////////
 }
 
 ///////////////////////////////////////////////////////////////////////////////
@@ -438,7 +499,8 @@ function garden() {
        $process_items = garden_make_process_items(GARDEN_OUTPUT_DIR, $content_files);
        garden_make_directories($process_items);
        garden_move_raw($process_items);
        $process_items = garden_make_process_items(GARDEN_OUTPUT_DIR, $content_files);
        garden_make_directories($process_items);
        garden_move_raw($process_items);
-       garden_generate_html($process_items);
+       $post_process_items = garden_generate_html(GARDEN_OUTPUT_DIR, $process_items);
+       garden_make_images($post_process_items);
 }
 
 ///////////////////////////////////////////////////////////////////////////////
 }
 
 ///////////////////////////////////////////////////////////////////////////////