Skip to content

Commit 5f4d766

Browse files
committed
Add 'jobs', 'cmm', 'deflum', 'hlglum' & 'bpc'
1 parent 52a2e33 commit 5f4d766

File tree

3 files changed

+184
-56
lines changed

3 files changed

+184
-56
lines changed

README.md

+27-5
Original file line numberDiff line numberDiff line change
@@ -62,19 +62,41 @@ $profile = $image->identify();
6262
For further details, have a look at the following sections.
6363

6464
#### Configuration
65-
You may also change certain options from your `config.php` globally (`'fundevogel.colorist.optionName'`):
65+
You may also change certain options from your `config.php` globally, like this: `'fundevogel.colorist.optionName'` (or simply pass them to the `thumb()` method:
6666

67-
| Option | Type | Default | Description |
67+
| Option | Type | Default(s to) | Description |
6868
| ------------ | ----------- | --------------------------- | -------------------------------------------------------------------------------------- |
6969
| `'bin'` | string | `__DIR__ . '/bin/colorist'` | Path to `colorist` executable |
70+
| `'bpc'` | integer | `'auto'` | Set bits-per-channel (J2K/JP2 only); ranging from `8` to `16` |
7071
| `'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+
| `'deflum'` | integer | `80` | default/fallback luminance value in nits |
73+
| `'hlglum'` | integer | `null` | Like `'deflum'`, but uses an appropriate diffuse white based on peak HLG |
74+
| `'jobs'` | integer | `0` | Number of jobs to use when working (`0` = unlimited) |
75+
| `'speed'` | integer | `'auto'` | Quality/speed tradeoff when encoding (AVIF only); `0` = best quality, `10` = fastest |
7276
| `'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'`) |
77+
| `'tonemap'` | string|bool | `'auto'` | Set tonemapping (`'on'` or `'off'`, but `true` & `false` are possible, too) |
78+
| `'yuv'` | string | `'auto'` | Choose yuv output format for supported formats (`'444'`, `'422'`, `'420'` or `'yv12'`) |
7579

7680
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!
7781

82+
**Note:** When working with multiple formats, you may want to turn `thumbs.quality` into an array and pass the desired `format` explicitly, like this:
83+
84+
```php
85+
// config.php
86+
87+
return [
88+
// ..
89+
'thumbs.quality' => [
90+
'avif' => 60,
91+
'webp' => 80,
92+
],
93+
];
94+
95+
96+
// template.php
97+
$image->thumb(['width' => 300, 'format' => 'avif']);
98+
```
99+
78100
#### Methods
79101
For now, the following methods are available:
80102

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.5.0",
6+
"version": "1.6.0",
77
"keywords": ["kirby3", "image", "graphics"],
88
"homepage": "https://github.com/Fundevogel/kirby3-colorist#readme",
99
"authors": [

src/Colorist.php

+156-50
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,14 @@ class Colorist extends Darkroom
2121
protected function defaults(): array
2222
{
2323
return parent::defaults() + [
24+
'cmm' => option('fundevogel.colorist.cmm', null),
25+
'deflum' => option('fundevogel.colorist.deflum', null),
2426
'format' => null,
25-
'speed' => option('fundevogel.colorist.speed', 10),
26-
'tonemap' => option('fundevogel.colorist.tonemap', 'off'),
27-
'yuv' => option('fundevogel.colorist.yuv', '420'),
27+
'hlglum' => option('fundevogel.colorist.hlglum', null),
28+
'jobs' => option('fundevogel.colorist.jobs', 0),
29+
'speed' => option('fundevogel.colorist.speed', null),
30+
'tonemap' => option('fundevogel.colorist.tonemap', null),
31+
'yuv' => option('fundevogel.colorist.yuv', null),
2832
];
2933
}
3034

@@ -64,8 +68,6 @@ public static function exec_enabled(): bool
6468

6569

6670
/**
67-
* Helpers
68-
*
6971
* Building command strings
7072
*/
7173

@@ -74,37 +76,34 @@ protected function convert(string $file): string
7476
return sprintf(option('fundevogel.colorist.bin') . ' convert %s', $file);
7577
}
7678

77-
protected function format(array $options): string
79+
protected function save(string $file): string
7880
{
79-
$formats = [
80-
'avif',
81-
'bmp',
82-
'jpg',
83-
'jp2',
84-
'j2k',
85-
'png',
86-
'tiff',
87-
'webp',
88-
];
81+
return sprintf('%s', $file);
82+
}
8983

90-
if (in_array($options['format'], $formats)) {
91-
return '--format ' . $options['format'];
92-
}
9384

94-
return '';
95-
}
85+
/**
86+
* Overwriting Darkroom options
87+
*/
9688

89+
# https://github.com/joedrago/colorist/blob/master/docs/Usage.md#-q---quality
9790
protected function quality(array $options): string
9891
{
9992
$quality = $options['quality'];
10093

101-
if (is_array($quality) && in_array($options['format'], $quality)) {
102-
$quality = $quality[$options['format']];
94+
if (is_array($quality) === true) {
95+
if (isset($options['format']) === true && in_array($options['format'], $quality) === true) {
96+
$quality = $quality[$options['format']];
97+
} else {
98+
return '';
99+
}
103100
}
104101

105102
return '--quality ' . $quality;
106103
}
107104

105+
# See https://github.com/joedrago/colorist/blob/master/docs/Usage.md#--resize
106+
# See https://github.com/joedrago/colorist/blob/master/docs/Usage.md#-z---rect---crop
108107
protected function resize(string $file, array $options): string
109108
{
110109
if ($options['crop'] === false) {
@@ -252,34 +251,91 @@ protected function resize(string $file, array $options): string
252251
return $command;
253252
}
254253

255-
protected function tonemap(array $options): string
254+
255+
/**
256+
* Implementing Colorist options
257+
*
258+
* (1) Basic options
259+
* (2) Output format options
260+
*/
261+
262+
# (1) Basic options
263+
264+
# See https://github.com/joedrago/colorist/blob/master/docs/Usage.md#-j---jobs
265+
protected function jobs(array $options): string
256266
{
257-
$tonemap = [
258-
'on',
259-
'off',
267+
return '--jobs ' . $options['jobs'];
268+
}
269+
270+
# See https://github.com/joedrago/colorist/blob/master/docs/Usage.md#--cmm---cms
271+
protected function cmm(array $options): string
272+
{
273+
$modules = [
274+
'colorist',
275+
'lcms',
260276
];
261277

262-
if (in_array($options['tonemap'], $tonemap)) {
263-
return '--tonemap ' . $options['tonemap'];
278+
if (in_array($options['cmm'], $modules) === true) {
279+
return '--cmm ' . $options['cmm'];
264280
}
265281

266-
return '--tonemap auto';
282+
return '';
267283
}
268284

269-
protected function yuv(array $options): string
285+
# See https://github.com/joedrago/colorist/blob/master/docs/Usage.md#--deflum---hlglum
286+
protected function deflum(array $options): string
270287
{
271-
$yuv = [
272-
'444',
273-
'422',
274-
'420',
275-
'yv12',
288+
if ($options['deflum'] !== null) {
289+
return '--deflum ' . $options['deflum'];
290+
}
291+
292+
return '';
293+
}
294+
295+
# See https://github.com/joedrago/colorist/blob/master/docs/Usage.md#--deflum---hlglum
296+
protected function hlglum(array $options): string
297+
{
298+
if ($options['hlglum'] !== null) {
299+
return '--hlglum ' . $options['hlglum'];
300+
}
301+
302+
return '';
303+
}
304+
305+
# (2) Output format options
306+
307+
# See https://github.com/joedrago/colorist/blob/master/docs/Usage.md#-b---bpc
308+
protected function bpc(array $options): string
309+
{
310+
$min = 8;
311+
$max = 16;
312+
313+
if (($min <= (int) $options['bpc']) && ((int) $options['bpc'] <= $max)) {
314+
return '--bpc ' . $options['bpc'];
315+
}
316+
317+
return '';
318+
}
319+
320+
# See https://github.com/joedrago/colorist/blob/master/docs/Usage.md#-f---format
321+
protected function format(array $options): string
322+
{
323+
$formats = [
324+
'avif',
325+
'bmp',
326+
'jpg',
327+
'jp2',
328+
'j2k',
329+
'png',
330+
'tiff',
331+
'webp',
276332
];
277333

278-
if (in_array($options['yuv'], $yuv)) {
279-
return '--yuv ' . $options['yuv'];
334+
if (in_array($options['format'], $formats) === true) {
335+
return '--format ' . $options['format'];
280336
}
281337

282-
return '--yuv auto';
338+
return '';
283339
}
284340

285341
protected function speed(array $options): string
@@ -291,23 +347,48 @@ protected function speed(array $options): string
291347
return '--speed ' . $options['speed'];
292348
}
293349

294-
return '--speed auto';
350+
return '';
295351
}
296352

297-
protected function save(string $file): string
353+
# See https://github.com/joedrago/colorist/blob/master/docs/Usage.md#-t---tonemap
354+
protected function tonemap(array $options): string
298355
{
299-
return sprintf('%s', $file);
356+
# Normalize booleans
357+
if ($options['tonemap'] === true) {
358+
$options['tonemap'] = 'on';
359+
}
360+
361+
if ($options['tonemap'] === false) {
362+
$options['tonemap'] = 'off';
363+
}
364+
365+
$tonemap = [
366+
'on',
367+
'off',
368+
];
369+
370+
if (in_array($options['tonemap'], $tonemap) === true) {
371+
return '--tonemap ' . $options['tonemap'];
372+
}
373+
374+
return '';
300375
}
301376

302-
public function preprocess(string $file, array $options = [])
377+
# See https://github.com/joedrago/colorist/blob/master/docs/Usage.md#--yuv
378+
protected function yuv(array $options): string
303379
{
304-
$options = $this->options($options);
380+
$yuv = [
381+
'444',
382+
'422',
383+
'420',
384+
'yv12',
385+
];
305386

306-
# TODO: As the underlying PHP function `getimagesize`
307-
# doesn't recognize next-gen image formats (like AVIF) yet,
308-
# this has to suffice ..
387+
if (in_array($options['yuv'], $yuv) === true) {
388+
return '--yuv ' . $options['yuv'];
389+
}
309390

310-
return $options;
391+
return '';
311392
}
312393

313394

@@ -317,6 +398,7 @@ public function preprocess(string $file, array $options = [])
317398

318399
public static function identify(string $file, bool $asArray = true)
319400
{
401+
# Build `identify` command
320402
$command = sprintf(option('fundevogel.colorist.bin') . ' identify --json %s', $file);
321403

322404
exec($command, $output, $status);
@@ -328,15 +410,28 @@ public static function identify(string $file, bool $asArray = true)
328410
return json_decode($output[0], $asArray);
329411
}
330412

413+
public function preprocess(string $file, array $options = [])
414+
{
415+
$options = $this->options($options);
416+
417+
# TODO: As the underlying PHP function `getimagesize`
418+
# doesn't recognize next-gen image formats (like AVIF) yet,
419+
# we need to overwrite this function, at least for now
420+
421+
return $options;
422+
}
423+
331424
public function toFormat(string $src, string $dst, string $format)
332425
{
333426
$options = $this->preprocess($src, ['format' => $format]);
334427
$command = [];
335428

429+
# Build `convert` command
336430
$command[] = $this->convert($src, $options);
337431
$command[] = $this->format($options);
338432
$command[] = $this->save($dst);
339433

434+
# Let's get ready to rumble
340435
# (1) Remove falsey entries
341436
# (2) Convert command to string
342437
$command = implode(' ', array_filter($command));
@@ -354,15 +449,26 @@ public function process(string $file, array $options = []): array
354449
$options = $this->preprocess($file, $options);
355450
$command = [];
356451

452+
# Build `convert` command
357453
$command[] = $this->convert($file);
358-
$command[] = $this->format($options);
454+
# (1) Darkroom options
359455
$command[] = $this->quality($options);
360-
$command[] = $this->speed($options);
361456
$command[] = $this->resize($file, $options);
457+
# (2) Basic options
458+
$command[] = $this->jobs($options);
459+
$command[] = $this->cmm($options);
460+
$command[] = $this->deflum($options);
461+
$command[] = $this->hlglum($options);
462+
# (3) Output format options
463+
$command[] = $this->bpc($options);
464+
$command[] = $this->format($options);
465+
$command[] = $this->speed($options);
362466
$command[] = $this->tonemap($options);
363467
$command[] = $this->yuv($options);
468+
# (4) Save image
364469
$command[] = $this->save($file);
365470

471+
# Let's get ready to rumble
366472
# (1) Remove falsey entries
367473
# (2) Convert command to string
368474
$command = implode(' ', array_filter($command));

0 commit comments

Comments
 (0)