Skip to content

Commit 52a2e33

Browse files
committed
Add 'colorist' tag
1 parent f2051e5 commit 52a2e33

File tree

4 files changed

+72
-21
lines changed

4 files changed

+72
-21
lines changed

README.md

+21-12
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,20 @@ $profile = $image->identify();
6161

6262
For further details, have a look at the following sections.
6363

64+
#### Configuration
65+
You may also change certain options from your `config.php` globally (`'fundevogel.colorist.optionName'`):
66+
67+
| Option | Type | Default | Description |
68+
| ------------ | ----------- | --------------------------- | -------------------------------------------------------------------------------------- |
69+
| `'bin'` | string | `__DIR__ . '/bin/colorist'` | Path to `colorist` executable |
70+
| `'formats'` | array | `['webp']` | Default file formats to be used on image uploads |
71+
| `'speed'` | integer | `0` | Quality/speed tradeoff when encoding (AVIF only); `0` = best quality, `10` = fastest |
72+
| `'template'` | string | `'image'` | Set file blueprint for generated images |
73+
| `'tonemap'` | string | `'off'` | Set tonemapping (`'on'` or `'off'`) |
74+
| `'yuv'` | string | `'420'` | Choose yuv output format for supported formats (`'444'`, `'422'`, `'420'` or `'yv12'`) |
75+
76+
The `colorist` library has [much more](https://github.com/joedrago/colorist/blob/master/docs/Usage.md) to offer, and more options will be made available in time - if one of it's many features you really feel is missing, feel free to open a PR!
77+
6478
#### Methods
6579
For now, the following methods are available:
6680

@@ -79,25 +93,20 @@ Checks if `$file` has image of given `$format`, returns `bool`.
7993
##### `isFormat (string $format)`
8094
Checks if `$file` is image of given `$format`, returns `bool`.
8195

82-
#### Configuration
83-
You may also change certain options from your `config.php` globally (`'fundevogel.colorist.optionName'`):
96+
#### Hooks
97+
On image upload, files are automatically converted to all formats in the `'fundevogel.colorist.formats'` option (`['webp']` by default).
8498

85-
| Option | Type | Default | Description |
86-
| ------------ | ----------- | --------------------------- | -------------------------------------------------------------------------------------- |
87-
| `'bin'` | string | `__DIR__ . '/bin/colorist'` | Path to `colorist` executable |
88-
| `'formats'` | array | `['webp']` | Default file formats to be used on image uploads |
89-
| `'speed'` | integer | `0` | Quality/speed tradeoff when encoding (AVIF only); `0` = best quality, `10` = fastest |
90-
| `'template'` | string | `'image'` | Set file blueprint for generated images |
91-
| `'tonemap'` | string | `'off'` | Set tonemapping (`'on'` or `'off'`) |
92-
| `'yuv'` | string | `'420'` | Choose yuv output format for supported formats (`'444'`, `'422'`, `'420'` or `'yv12'`) |
99+
#### Tag
100+
The `(colorist: example.jpg)` tag supports converting / resizing right from the editor.
93101

94-
The `colorist` library has [much more](https://github.com/joedrago/colorist/blob/master/docs/Usage.md) to offer, and more options will be made available in time - if one of it's many features you really feel is missing, feel free to open a PR!
102+
##### Options
103+
WIP
95104

96105

97106
## Roadmap
98107
- [ ] Add tests
99108
- [x] ~~Add hooks for file upload/update~~
100-
- [ ] Add tag for editor use
109+
- [x] ~~Add tag for editor use~~
101110
- [x] ~~Add compatibility with 'Focus' plugin by @flokosiol~~
102111
- [ ] Add methods for editing ICC color profile
103112

composer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"description": "Kirby v3 wrapper for colorist",
44
"type": "kirby-plugin",
55
"license": "MIT",
6-
"version": "1.4.0",
6+
"version": "1.5.0",
77
"keywords": ["kirby3", "image", "graphics"],
88
"homepage": "https://github.com/Fundevogel/kirby3-colorist#readme",
99
"authors": [

index.php

+43-5
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Kirby\Cms\Filename;
77
use Kirby\Cms\FileVersion;
88
use Kirby\Data\Data;
9+
use Kirby\Http\Url;
910
use Kirby\Image\Darkroom;
1011
use Kirby\Toolkit\F;
1112
use Kirby\Toolkit\Str;
@@ -42,7 +43,7 @@
4243
'bin' => __DIR__ . '/bin/colorist',
4344
'sizes' => [1920, 1140, 640, 320],
4445
'template' => 'image',
45-
'uploadFormats' => ['webp'],
46+
'formats' => ['webp'],
4647
],
4748
'snippets' => [
4849
'colorist' => __DIR__ . '/snippets/colorist.php',
@@ -152,7 +153,7 @@
152153
},
153154
'toFormats' => function (array $formats) {
154155
if (empty($formats)) {
155-
$formats = option('fundevogel.colorist.uploadFormats');
156+
$formats = option('fundevogel.colorist.formats');
156157
}
157158

158159
$files = [];
@@ -186,7 +187,7 @@
186187
},
187188
'toFormats' => function (array $formats) {
188189
if (empty($formats)) {
189-
$formats = option('fundevogel.colorist.uploadFormats');
190+
$formats = option('fundevogel.colorist.formats');
190191
}
191192

192193
$files = [];
@@ -202,10 +203,47 @@
202203
],
203204
'hooks' => [
204205
'file.create:after' => function ($file) {
205-
$file->toFormats(option('fundevogel.colorist.uploadFormats'));
206+
$file->toFormats(option('fundevogel.colorist.formats'));
206207
},
207208
'file.replace:after' => function ($newFile, $oldFile) {
208-
$newFile->toFormats(option('fundevogel.colorist.uploadFormats'));
209+
$newFile->toFormats(option('fundevogel.colorist.formats'));
209210
},
210211
],
212+
'tags' => [
213+
'colorist' => [
214+
'attr' => [
215+
'alt',
216+
'class',
217+
'fallback',
218+
'height',
219+
'imgclass',
220+
'title',
221+
'width',
222+
],
223+
'html' => function($tag) {
224+
if ($tag->file = $tag->file($tag->value)) {
225+
$tag->alt = $tag->alt ?? $tag->file->alt()->or(' ')->value();
226+
$tag->fallback = $tag->fallback ?? 'jpg';
227+
$tag->height = $tag->height ?? $tag->file($tag->value)->height();
228+
$tag->sizes = $tag->sizes ? $tag->sizes : option('fundevogel.colorist.sizes');
229+
$tag->src = $tag->file($tag->value);
230+
$tag->title = $tag->title ?? $tag->file->title()->or(' ')->value();
231+
$tag->width = $tag->width ?? $tag->file($tag->value)->width();
232+
} else {
233+
$tag->src = Url::to($tag->value);
234+
}
235+
236+
return snippet('colorist', [
237+
'alt' => $tag->alt,
238+
'class' => $tag->class,
239+
'height' => $tag->height,
240+
'sizes' => $tag->sizes,
241+
'src' => $tag->src,
242+
'title' => $tag->title,
243+
'type' => $tag->fallback,
244+
'width' => $tag->width
245+
], false);
246+
},
247+
],
248+
],
211249
]);

snippets/colorist.php

+7-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
2-
$formats = $src->toFormats('avif', 'webp');
3-
$sizes = option('fundevogel.colorist.sizes');
2+
$formats = $src->toFormats(['avif', 'webp']);
3+
$sizes = $sizes ?? option('fundevogel.colorist.sizes');
44
?>
55

66
<picture>
@@ -34,5 +34,9 @@
3434
>
3535
<?php endforeach?>
3636

37-
<img src="<?= $src->url() ?>">
37+
<img
38+
src="<?= $src->resize($width, $height)->url() ?>"
39+
title="<?= $src->title() ?>" alt="<?= $src->alt() ?>"
40+
width="<?= $width ?>" height="<?= $height ?>"
41+
>
3842
</picture>

0 commit comments

Comments
 (0)