Skip to content

Commit

Permalink
fix: update options namespace with fallbacks
Browse files Browse the repository at this point in the history
to match Kirby plugin recommendations
  • Loading branch information
johannschopplich committed Sep 20, 2022
1 parent 49b61f3 commit 660219c
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 20 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace KirbyExtended;
namespace JohannSchopplich;

class BlurryPlaceholder
{
Expand All @@ -10,7 +10,7 @@ class BlurryPlaceholder
public static function image(\Kirby\Cms\File $file, float|null $ratio = null): string
{
$kirby = kirby();
$pixelTarget = $kirby->option('kirby-extended.blurry-placeholder.pixel-target', 60);
$pixelTarget = $kirby->option('johannschopplich.blurry-placeholder.pixel-target') ?? $kirby->option('kirby-extended.blurry-placeholder.pixel-target', 60);

// Aims for an image of ~P pixels (w * h = ~P)
$height = sqrt($pixelTarget / ($ratio ?? $file->ratio()));
Expand Down
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -180,21 +180,21 @@ So should you implement a "blur down" transition? **Probably not.** If you do, s

## Options

> All options listed have to be prefixed with `kirby-extended.blurry-placeholder.` in your `config.php`.
> ℹ️ The plugin's options namespace was changed to `johannschopplich.blurry-placeholder` in v2.0.1. All options from the former `kirby-extended.blurry-placeholder` are deprecated, but still supported as a fallback.
| Option | Default | Description |
| ------------------------ | ------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| `pixel-target` | `60` | Aim for a placeholder image of ~P pixels (`w \* h = ~P`). |
| `kirbytag.srcset-preset` | `null` | A preset passed to [Kirby's `srcset` method](https://getkirby.com/docs/reference/objects/cms/file/srcset#define-presets) when using the Kirbytag. If `null` (default), a `src` attribute will be rendered instead of `srcset`. |
| `kirbytag.sizes` | `auto` | String for the `data-sizes` attribute if the Kirbytag works with `srcset`'s. |
| Option (`johannschopplich.blurry-placeholder.<option>`) | Default | Description |
| ------------------------------------------------------- | ------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| `pixel-target` | `60` | Aim for a placeholder image of ~P pixels (`w \* h = ~P`). |
| `kirbytag.srcset-preset` | `null` | A preset passed to [Kirby's `srcset` method](https://getkirby.com/docs/reference/objects/cms/file/srcset#define-presets) when using the Kirbytag. If `null` (default), a `src` attribute will be rendered instead of `srcset`. |
| `kirbytag.sizes` | `auto` | String for the `data-sizes` attribute if the Kirbytag works with `srcset`'s. |

> All of the `srcset` options have to be wrapped in an array.
To give an example for your `config.php`:

```php
return [
'kirby-extended.blurry-placeholder' => [
'johannschopplich.blurry-placeholder' => [
'pixel-target' => 60,
'kirbytag' => [
'srcset-preset' => 'article'
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "johannschopplich/kirby-blurry-placeholder",
"description": "Blurry image placeholders and lazy loading for better UX",
"description": "Blurry image placeholders for better UX",
"type": "kirby-plugin",
"version": "2.0.0",
"keywords": [
Expand Down
14 changes: 10 additions & 4 deletions index.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
<?php

use JohannSchopplich\BlurryPlaceholder;

load([
'KirbyExtended\\BlurryPlaceholder' => 'classes/KirbyExtended/BlurryPlaceholder.php'
'JohannSchopplich\\BlurryPlaceholder' => 'BlurryPlaceholder.php'
], __DIR__);

\Kirby\Cms\App::plugin('kirby-extended/blurry-placeholder', [
\Kirby\Cms\App::plugin('johannschopplich/blurry-placeholder', [
'fileMethods' => [
'placeholder' => fn (float|null $ratio = null) => \KirbyExtended\BlurryPlaceholder::image($this, $ratio),
'placeholderUri' => fn (float|null $ratio = null) => \KirbyExtended\BlurryPlaceholder::uri($this, $ratio)
'placeholder' => function (float|null $ratio = null) {
return BlurryPlaceholder::image($this, $ratio);
},
'placeholderUri' => function (float|null $ratio = null) {
return BlurryPlaceholder::uri($this, $ratio);
}
],
'tags' => [
'blurryimage' => require __DIR__ . '/tags/blurryimage.php'
Expand Down
11 changes: 5 additions & 6 deletions tags/blurryimage.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,13 @@

if ($tag->file !== null) {
$dataUri = $tag->file->placeholderUri();
$preset = $tag->kirby()->option('kirby-extended.blurry-placeholder.kirbytag.srcset-preset');
$sizes = $tag->kirby()->option('kirby-extended.blurry-placeholder.kirbytag.sizes', 'auto');
$hasPreset = $preset !== null;
$preset = $tag->kirby()->option('johannschopplich.blurry-placeholder.kirbytag.srcset-preset') ?? $tag->kirby()->option('kirby-extended.blurry-placeholder.kirbytag.srcset-preset');
$sizes = $tag->kirby()->option('johannschopplich.blurry-placeholder.kirbytag.sizes') ?? $tag->kirby()->option('kirby-extended.blurry-placeholder.kirbytag.sizes', 'auto');

$image = Html::img($dataUri, A::merge($imageAttr, [
'data-src' => !$hasPreset ? $tag->src : null,
'data-srcset' => $hasPreset ? $tag->file->srcset($preset) : null,
'data-sizes' => $hasPreset ? $sizes : null,
'data-src' => $preset === null ? $tag->src : null,
'data-srcset' => $preset ? $tag->file->srcset($preset) : null,
'data-sizes' => $preset ? $sizes : null,
'data-lazyload' => 'true',
]));
} else {
Expand Down

0 comments on commit 660219c

Please sign in to comment.