From: Ben Sherratt Date: Sat, 27 Dec 2025 10:54:05 +0000 (+0000) Subject: Added image support X-Git-Url: https://git.bts.cx/garden.git/commitdiff_plain?ds=inline Added image support --- diff --git a/garden/garden.php b/garden/garden.php index e73804a..ada43bb 100644 --- a/garden/garden.php +++ b/garden/garden.php @@ -220,11 +220,13 @@ function garden_make_directories($content_items) { mkdir($directory , 0777, true); // FIXME, permissions... } } + + return $content_items; } 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; } @@ -233,6 +235,8 @@ function garden_move_raw($content_items) { 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; } +/////////////////////////////////////////////////////////////////////////////// +// 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 /////////////////////////////////////////////////////////////////////////////// @@ -295,6 +327,10 @@ function garden_template_content() { return $garden_template_content(); } +function garden_site_url(...$url_segments) { + return GARDEN_SITE_BASE_URL . garden_url(...$url_segments); +} + /////////////////////////////////////////////////////////////////////////////// // HTML /////////////////////////////////////////////////////////////////////////////// @@ -302,9 +338,13 @@ function garden_template_content() { require_once(garden_path(__DIR__, 'third_party', 'parsedown', 'Parsedown.php')); class GardenExtendedParsedown extends Parsedown { + public $post_process_items; + private $output_directory; 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'; @@ -323,18 +363,31 @@ class GardenExtendedParsedown extends Parsedown { return; } - $target_url = null; + $image_content_item = null; 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; } } - if ($target_url == null) { + if ($image_content_item == null) { 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( @@ -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) { @@ -420,12 +473,20 @@ function garden_generate_html($content_items) { } $markdown_data = garden_read_file($item->source_filename); + + $parsedown = new GardenExtendedParsedown($output_directory, $content_items); $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); } + + 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); - garden_generate_html($process_items); + $post_process_items = garden_generate_html(GARDEN_OUTPUT_DIR, $process_items); + garden_make_images($post_process_items); } /////////////////////////////////////////////////////////////////////////////// diff --git a/templates/base.php b/templates/base.php index 83217f2..95cd625 100644 --- a/templates/base.php +++ b/templates/base.php @@ -6,6 +6,9 @@ + + + <?= site_name ?> - <?= $article->title; ?> diff --git a/templates/style/screen.css b/templates/style/screen.css new file mode 100644 index 0000000..e69de29