From 30d09d610453de5c32bab3353982b74dd51af276 Mon Sep 17 00:00:00 2001 From: isUnknown Date: Wed, 10 Dec 2025 16:00:58 +0100 Subject: [PATCH] Add responsive image optimization with WebP support MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Configure thumbs presets in config.php (quality 85%, WebP format) - Create picture snippet with srcset and WebP support - Add product-card and product-detail presets - Update templates to use optimized images - Implement native lazy loading for product cards - Add aspect-ratio CSS for layout stability Improvements over reference project: - Complete PHPDoc documentation - Native CSS aspect-ratio support - Optimized lazy loading strategy - Product-specific image presets 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Sonnet 4.5 --- site/config/config.php | 31 +++++++++++++++++++++ site/snippets/picture.php | 56 ++++++++++++++++++++++++++++++++++++++ site/templates/home.php | 8 +++++- site/templates/product.php | 8 +++++- 4 files changed, 101 insertions(+), 2 deletions(-) create mode 100644 site/snippets/picture.php diff --git a/site/config/config.php b/site/config/config.php index 26626f2..b0a1197 100644 --- a/site/config/config.php +++ b/site/config/config.php @@ -5,6 +5,37 @@ return [ 'languages' => true, + 'thumbs' => [ + 'quality' => 85, + 'format' => 'webp', + 'presets' => [ + 'product-card' => [ + 'width' => 600, + 'height' => 600, + 'crop' => true, + 'format' => 'webp' + ], + 'product-detail' => [ + 'width' => 1200, + 'format' => 'webp' + ], + 'default' => [ + 'width' => 1024, + 'format' => 'webp' + ], + ], + 'srcsets' => [ + 'default' => [400, 600, 800, 1024, 1440, 2048], + 'webp' => [ + '400w' => ['width' => 400, 'format' => 'webp'], + '600w' => ['width' => 600, 'format' => 'webp'], + '800w' => ['width' => 800, 'format' => 'webp'], + '1024w' => ['width' => 1024, 'format' => 'webp'], + '1440w' => ['width' => 1440, 'format' => 'webp'], + ], + ], + ], + 'routes' => [ [ 'pattern' => 'snipcart-webhook', diff --git a/site/snippets/picture.php b/site/snippets/picture.php new file mode 100644 index 0000000..efe596b --- /dev/null +++ b/site/snippets/picture.php @@ -0,0 +1,56 @@ +alt()->or($file->filename())->value(); +$class = isset($class) ? 'class="' . $class . '"' : ''; +$lazy = $lazy ?? true; +$preset = $preset ?? 'default'; + +// Sizes attribute - determines which image size the browser should load +if (isset($size)) { + $sizes = is_numeric($size) + ? "(min-width: 1024px) {$size}vw, 100vw" + : $size; +} else { + $sizes = "(min-width: 1024px) 50vw, 100vw"; +} + +// Generate srcsets for WebP and fallback +$webpSrcset = $file->srcset('webp'); +$srcset = $file->srcset('default'); + +// Get optimized source URL +$src = $file->thumb($preset)->url(); + +// Get dimensions to avoid layout shift +$width = $file->width(); +$height = $file->height(); +$aspectRatio = $height > 0 ? ($height / $width) * 100 : 0; +?> + + data-id="uuid() ?>"> + + <?= $alt ?> + style="aspect-ratio: / ;" + > + diff --git a/site/templates/home.php b/site/templates/home.php index 26ed9c6..ca3ff36 100644 --- a/site/templates/home.php +++ b/site/templates/home.php @@ -10,7 +10,13 @@
images()->first()): ?> - <?= $product->title()->html() ?> + $image, + 'alt' => $product->title()->html(), + 'preset' => 'product-card', + 'size' => 25, + 'lazy' => true + ]) ?>

title()->html() ?>

diff --git a/site/templates/product.php b/site/templates/product.php index cf1fd98..b980451 100644 --- a/site/templates/product.php +++ b/site/templates/product.php @@ -70,7 +70,13 @@
images()->first()): ?> - <?= $page->title()->html() ?> + $image, + 'alt' => $page->title()->html(), + 'preset' => 'product-detail', + 'size' => 50, + 'lazy' => false + ]) ?>