Skip to content

Commit a7dae07

Browse files
authored
Merge pull request #5 from pboivin/feat/render-config
Add render options configuration
2 parents 78878b9 + fc0950a commit a7dae07

20 files changed

+362
-34
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
*.swp
33

44
/__tmp__
5-
/tests/fixtures/__cache__
5+
/tests/Fixtures/__cache__
66
/vendor
77
/composer.lock
88
/.php_cs.cache

CHANGELOG.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,21 @@
11
# Release Notes
22

3+
## [v1.2.0](https://github.com/pboivin/flou/compare/v1.1.0...v1.2.0) - 2022-08-24
4+
5+
#### Added
6+
7+
- Accept render options as array (`ImageFactory`)
8+
- Add missing setter for padding element CSS class (`ImgRenderable`)
9+
10+
#### Changed
11+
12+
- Ensure base64 LQIP is usable with wrapper option (`ImgRenderable`)
13+
14+
#### Chores
15+
16+
- Document various configuration options
17+
18+
319
## [v1.1.0](https://github.com/pboivin/flou/compare/v1.0.1...v1.1.0) - 2022-08-10
420

521
#### Added

README.md

Lines changed: 49 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,12 +91,24 @@ $flou = new ImageFactory([
9191
]);
9292
```
9393

94+
95+
#### Configuration
96+
9497
The required options are:
9598

96-
- `sourcePath`: The full path to the source images.
97-
- `cachePath`: The full path where Glide will store the image transformations.
98-
- `sourceUrlBase`: The base URL for the source images.
99-
- `cacheUrlBase`: The base URL for the transformed images.
99+
| Name | Type | Description |
100+
|---|---|---|
101+
| `sourcePath` | string | The full path to the source images. |
102+
| `cachePath` | string | The full path where Glide will store the image transformations. |
103+
| `sourceUrlBase` | string | The base URL for the source images. |
104+
| `cacheUrlBase` | string | The base URL for the transformed images. |
105+
106+
Other options:
107+
108+
| Name | Type | Description |
109+
|---|---|---|
110+
| `glideParams` | array | [Default Glide parameters for LQIP elements.](#default-glide-parameters) |
111+
| `renderOptions` | array | [Default render options for all images.](#default-render-options) |
100112

101113

102114
#### Framework Integration
@@ -351,6 +363,39 @@ The `ImageRender` object can be configured to optimize the HTML output:
351363
<br>
352364

353365

366+
#### Default render options
367+
368+
You can customize the default render options for all images in the `ImageFactory` configuration:
369+
370+
```php
371+
$flou = new ImageFactory([
372+
// ...
373+
'renderOptions' => [
374+
'aspectRatio' => 16 / 9,
375+
'wrapper' => true,
376+
'base64Lqip' => true,
377+
// ...
378+
],
379+
]);
380+
```
381+
382+
<details>
383+
<summary>See Available Options</summary>
384+
385+
| Name | Type | Description |
386+
|---|---|---|
387+
| `baseClass` | string | CSS class for `img` element. Default: `'lazyload'` |
388+
| `wrapperClass` | string | CSS class for wrapper element. Default: `'lazyload-wrapper'` |
389+
| `lqipClass` | string | CSS class for LQIP element. Default: `'lazyload-lqip'` |
390+
| `paddingClass` | string | CSS class for padding-specific wrapper element. Default: `'lazyload-padding'` |
391+
| `aspectRatio` | boolean or number | Use aspect ratio. Default: `false` |
392+
| `paddingTop` | boolean or number | Use padding-top workaround. Default: `false` |
393+
| `wrapper` | boolean | Use wrapper element. Default: `false` |
394+
| `base64Lqip` | boolean | Use Base64 LQIP value. Default: `false` |
395+
</details>
396+
<br>
397+
398+
354399
#### Noscript variation
355400

356401
Use the `noScript()` method to render an `img` element without any lazy loading behavior:

src/Image.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44

55
class Image
66
{
7+
protected $renderClass = ImageRender::class;
8+
9+
protected $renderOptions = [];
10+
711
public function __construct(protected ImageFile $source, protected ImageFile $cached)
812
{
913
}
@@ -18,9 +22,19 @@ public function cached(): ImageFile
1822
return $this->cached;
1923
}
2024

25+
public function setRenderClass(string $cls): void
26+
{
27+
$this->renderClass = $cls;
28+
}
29+
30+
public function setRenderOptions(array $options): void
31+
{
32+
$this->renderOptions = $options;
33+
}
34+
2135
public function render(): ImageRender
2236
{
23-
return new ImageRender($this);
37+
return new ($this->renderClass)($this, $this->renderOptions);
2438
}
2539

2640
public function toArray(): array

src/ImageFactory.php

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ class ImageFactory
2424

2525
protected $inspector;
2626

27+
protected $renderOptions;
28+
2729
final public function __construct(array $config = [])
2830
{
2931
if ($config) {
@@ -171,6 +173,11 @@ public function setInspector(ImageFileInspector $inspector): self
171173
return $this;
172174
}
173175

176+
public function setRenderOptions(array $options): void
177+
{
178+
$this->renderOptions = $options;
179+
}
180+
174181
public function image(string $sourceFileName, ?array $glideParams = null): Image
175182
{
176183
$glideParams ??= $this->glideParams();
@@ -179,15 +186,27 @@ public function image(string $sourceFileName, ?array $glideParams = null): Image
179186

180187
$cachedFileName = $server->makeImage($sourceFileName, $glideParams);
181188

182-
return new Image(
189+
$image = new Image(
183190
$this->sourceImageFile($sourceFileName),
184191
$this->cachedImageFile($cachedFileName)
185192
);
193+
194+
if ($this->renderOptions) {
195+
$image->setRenderOptions($this->renderOptions);
196+
}
197+
198+
return $image;
186199
}
187200

188201
public function imageSet(array $config): ImageSet
189202
{
190-
return new ImageSet($config, $this);
203+
$set = new ImageSet($config, $this);
204+
205+
if ($this->renderOptions) {
206+
$set->setRenderOptions($this->renderOptions);
207+
}
208+
209+
return $set;
191210
}
192211

193212
public function sourceImageFile(string $fileName): ImageFile

src/ImageRender.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,11 @@
44

55
class ImageRender extends ImgRenderable
66
{
7-
public function __construct(protected Image $image)
7+
public function __construct(protected Image $image, protected array $config = [])
88
{
9+
if ($config) {
10+
$this->acceptRenderConfig($config);
11+
}
912
}
1013

1114
public function main(): ImageFile

src/ImageSet.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ class ImageSet
88
{
99
public const DEFAULT_SIZES_VALUE = '100vw';
1010

11+
protected $renderClass = ImageSetRender::class;
12+
13+
protected $renderOptions = [];
14+
1115
protected $image;
1216

1317
protected $sizes;
@@ -110,8 +114,18 @@ public function toArray(): array
110114
];
111115
}
112116

117+
public function setRenderClass(string $cls): void
118+
{
119+
$this->renderClass = $cls;
120+
}
121+
122+
public function setRenderOptions(array $options): void
123+
{
124+
$this->renderOptions = $options;
125+
}
126+
113127
public function render(): ImageSetRender
114128
{
115-
return new ImageSetRender($this);
129+
return new ($this->renderClass)($this, $this->renderOptions);
116130
}
117131
}

src/ImageSetRender.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,13 @@ class ImageSetRender extends ImgRenderable
66
{
77
protected $data;
88

9-
public function __construct(protected ImageSet $imageSet)
9+
public function __construct(protected ImageSet $imageSet, protected array $config = [])
1010
{
1111
$this->data = $this->imageSet->data();
12+
13+
if ($config) {
14+
$this->acceptRenderConfig($config);
15+
}
1216
}
1317

1418
public function main(): ImageFile

src/ImgRenderable.php

Lines changed: 40 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Pboivin\Flou;
44

5+
use InvalidArgumentException;
56
use Stringable;
67

78
abstract class ImgRenderable implements Stringable
@@ -22,32 +23,53 @@ abstract class ImgRenderable implements Stringable
2223

2324
protected $wrapper = false;
2425

26+
protected $base64Lqip = false;
27+
28+
/* Internal */
2529
protected $includeLqip = true;
2630

27-
protected $base64Lqip = false;
31+
protected function acceptRenderConfig(array $config): void
32+
{
33+
foreach ($config as $key => $value) {
34+
if (method_exists($this, $method = "set{$key}")) {
35+
$this->$method($value);
36+
} elseif (method_exists($this, $method = "use{$key}")) {
37+
$this->$method($value);
38+
} else {
39+
throw new InvalidArgumentException("Invalid option '$key'.");
40+
}
41+
}
42+
}
2843

2944
public function __toString(): string
3045
{
3146
return $this->img();
3247
}
3348

34-
public function setBaseClass(string $cls): self
49+
public function setBaseClass(string $cssClass): self
3550
{
36-
$this->baseClass = $cls;
51+
$this->baseClass = $cssClass;
3752

3853
return $this;
3954
}
4055

41-
public function setWrapperClass(string $cls): self
56+
public function setWrapperClass(string $cssClass): self
4257
{
43-
$this->wrapperClass = $cls;
58+
$this->wrapperClass = $cssClass;
4459

4560
return $this;
4661
}
4762

48-
public function setLqipClass(string $cls): self
63+
public function setLqipClass(string $cssClass): self
4964
{
50-
$this->lqipClass = $cls;
65+
$this->lqipClass = $cssClass;
66+
67+
return $this;
68+
}
69+
70+
public function setPaddingClass(string $cssClass): self
71+
{
72+
$this->paddingClass = $cssClass;
5173

5274
return $this;
5375
}
@@ -59,18 +81,24 @@ public function useWrapper(bool $value = true): self
5981
return $this;
6082
}
6183

62-
public function useAspectRatio(?float $value = null): self
84+
public function useAspectRatio(mixed $value = true): self
6385
{
64-
$this->aspectRatio = is_null($value) ? $this->main()->ratio() : $value;
86+
if ($value === true) {
87+
$this->aspectRatio = $this->main()->ratio();
88+
} elseif ($value === false) {
89+
$this->aspectRatio = false;
90+
} else {
91+
$this->aspectRatio = $value;
92+
}
6593

6694
return $this;
6795
}
6896

69-
public function usePaddingTop(?float $value = null): self
97+
public function usePaddingTop(mixed $value = true): self
7098
{
7199
$this->useAspectRatio($value);
72100

73-
$this->paddingTop = true;
101+
$this->paddingTop = !!$value;
74102

75103
return $this;
76104
}
@@ -182,7 +210,7 @@ protected function handleWrapper(string $input): string
182210
$this->includeLqip
183211
? $this->htmlTag('img', [
184212
'class' => $this->lqipClass,
185-
'src' => $this->lqip()->url(),
213+
'src' => $this->lqipUrl(),
186214
])
187215
: '',
188216
])

tests/Fixtures/TestImageRender.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
namespace Pboivin\Flou\Tests\Fixtures;
4+
5+
use Pboivin\Flou\ImageRender;
6+
7+
class TestImageRender extends ImageRender
8+
{
9+
}

0 commit comments

Comments
 (0)