Skip to content

Commit 4ea6276

Browse files
committed
Merge branch 'feature/mime'
2 parents 8a70448 + c2036f2 commit 4ea6276

File tree

2 files changed

+148
-8
lines changed

2 files changed

+148
-8
lines changed

src/Focus/GdLib.php

Lines changed: 55 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,24 @@
22

33
namespace Flokosiol\Focus;
44

5-
ini_set('memory_limit', '512M');
6-
75
use claviska\SimpleImage;
8-
use Kirby\Image\Image;
9-
use Kirby\Image\Dimensions;
6+
use Kirby\Filesystem\Mime;
107
use Kirby\Image\Darkroom;
8+
use Kirby\Image\Image;
119

1210
class GdLib extends Darkroom
1311
{
12+
/**
13+
* Processes the image with the SimpleImage library
14+
*
15+
* @param string $file
16+
* @param array $options
17+
* @return array
18+
*/
1419
public function process(string $file, array $options = []): array
1520
{
1621
$options = $this->preprocess($file, $options);
22+
$mime = $this->mime($options);
1723

1824
// original image dimension for focus cropping
1925
$originalImage = new Image($file);
@@ -30,11 +36,19 @@ public function process(string $file, array $options = []): array
3036
$image = $this->blur($image, $options);
3137
$image = $this->grayscale($image, $options);
3238

33-
$image->toFile($file, null, $options['quality']);
39+
$image->toFile($file, $mime, $options['quality']);
3440

3541
return $options;
3642
}
3743

44+
/**
45+
* Activates the autoOrient option in SimpleImage
46+
* unless this is deactivated
47+
*
48+
* @param \claviska\SimpleImage $image
49+
* @param $options
50+
* @return \claviska\SimpleImage
51+
*/
3852
protected function autoOrient(SimpleImage $image, $options)
3953
{
4054
if ($options['autoOrient'] === false) {
@@ -44,6 +58,13 @@ protected function autoOrient(SimpleImage $image, $options)
4458
return $image->autoOrient();
4559
}
4660

61+
/**
62+
* Wrapper around SimpleImage's resize and crop methods
63+
*
64+
* @param \claviska\SimpleImage $image
65+
* @param array $options
66+
* @return \claviska\SimpleImage
67+
*/
4768
protected function resize(SimpleImage $image, array $options)
4869
{
4970
if ($options['crop'] === false) {
@@ -60,6 +81,13 @@ protected function resize(SimpleImage $image, array $options)
6081

6182
}
6283

84+
/**
85+
* Applies the correct blur settings for SimpleImage
86+
*
87+
* @param \claviska\SimpleImage $image
88+
* @param array $options
89+
* @return \claviska\SimpleImage
90+
*/
6391
protected function blur(SimpleImage $image, array $options)
6492
{
6593
if ($options['blur'] === false) {
@@ -69,6 +97,13 @@ protected function blur(SimpleImage $image, array $options)
6997
return $image->blur('gaussian', (int)$options['blur']);
7098
}
7199

100+
/**
101+
* Applies grayscale conversion if activated in the options.
102+
*
103+
* @param \claviska\SimpleImage $image
104+
* @param array $options
105+
* @return \claviska\SimpleImage
106+
*/
72107
protected function grayscale(SimpleImage $image, array $options)
73108
{
74109
if ($options['grayscale'] === false) {
@@ -77,4 +112,19 @@ protected function grayscale(SimpleImage $image, array $options)
77112

78113
return $image->desaturate();
79114
}
115+
116+
/**
117+
* Returns mime type based on `format` option
118+
*
119+
* @param array $options
120+
* @return string|null
121+
*/
122+
protected function mime(array $options): ?string
123+
{
124+
if ($options['format'] === null) {
125+
return null;
126+
}
127+
128+
return Mime::fromExtension($options['format']);
129+
}
80130
}

src/Focus/ImageMagick.php

Lines changed: 93 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,39 +3,72 @@
33
namespace Flokosiol\Focus;
44

55
use Exception;
6-
use Kirby\Image\Image;
7-
use Kirby\Image\Dimensions;
6+
use Kirby\Filesystem\F;
87
use Kirby\Image\Darkroom;
9-
use Kirby\Toolkit\F;
8+
use Kirby\Image\Image;
109

1110
class ImageMagick extends Darkroom
1211
{
12+
/**
13+
* Activates imagemagick's auto-orient feature unless
14+
* it is deactivated via the options
15+
*
16+
* @param string $file
17+
* @param array $options
18+
* @return string
19+
*/
1320
protected function autoOrient(string $file, array $options)
1421
{
1522
if ($options['autoOrient'] === true) {
1623
return '-auto-orient';
1724
}
1825
}
1926

27+
/**
28+
* Applies the blur settings
29+
*
30+
* @param string $file
31+
* @param array $options
32+
* @return string
33+
*/
2034
protected function blur(string $file, array $options)
2135
{
2236
if ($options['blur'] !== false) {
2337
return '-blur 0x' . $options['blur'];
2438
}
2539
}
2640

41+
/**
42+
* Keep animated gifs
43+
*
44+
* @param string $file
45+
* @param array $options
46+
* @return string
47+
*/
2748
protected function coalesce(string $file, array $options)
2849
{
2950
if (F::extension($file) === 'gif') {
3051
return '-coalesce';
3152
}
3253
}
3354

55+
/**
56+
* Creates the convert command with the right path to the binary file
57+
*
58+
* @param string $file
59+
* @param array $options
60+
* @return string
61+
*/
3462
protected function convert(string $file, array $options): string
3563
{
3664
return sprintf($options['bin'] . ' "%s"', $file);
3765
}
3866

67+
/**
68+
* Returns additional default parameters for imagemagick
69+
*
70+
* @return array
71+
*/
3972
protected function defaults(): array
4073
{
4174
return parent::defaults() + [
@@ -44,20 +77,44 @@ protected function defaults(): array
4477
];
4578
}
4679

80+
/**
81+
* Applies the correct settings for grayscale images
82+
*
83+
* @param string $file
84+
* @param array $options
85+
* @return string
86+
*/
4787
protected function grayscale(string $file, array $options)
4888
{
4989
if ($options['grayscale'] === true) {
5090
return '-colorspace gray';
5191
}
5292
}
5393

94+
/**
95+
* Applies the correct settings for interlaced JPEGs if
96+
* activated via options
97+
*
98+
* @param string $file
99+
* @param array $options
100+
* @return string
101+
*/
54102
protected function interlace(string $file, array $options)
55103
{
56104
if ($options['interlace'] === true) {
57105
return '-interlace line';
58106
}
59107
}
60108

109+
/**
110+
* Creates and runs the full imagemagick command
111+
* to process the image
112+
*
113+
* @param string $file
114+
* @param array $options
115+
* @return array
116+
* @throws \Exception
117+
*/
61118
public function process(string $file, array $options = []): array
62119
{
63120
$options = $this->preprocess($file, $options);
@@ -96,11 +153,26 @@ public function process(string $file, array $options = []): array
96153
return $options;
97154
}
98155

156+
/**
157+
* Applies the correct JPEG compression quality settings
158+
*
159+
* @param string $file
160+
* @param array $options
161+
* @return string
162+
*/
99163
protected function quality(string $file, array $options): string
100164
{
101165
return '-quality ' . $options['quality'];
102166
}
103167

168+
/**
169+
* Creates the correct options to crop or resize the image
170+
* and translates the crop positions for imagemagick
171+
*
172+
* @param string $file
173+
* @param array $options
174+
* @return string
175+
*/
104176
protected function resize(string $file, array $options): string
105177
{
106178
// simple resize
@@ -135,11 +207,29 @@ protected function resize(string $file, array $options): string
135207
return $command;
136208
}
137209

210+
/**
211+
* Makes sure to not process too many images at once
212+
* which could crash the server
213+
*
214+
* @param string $file
215+
* @param array $options
216+
* @return string
217+
*/
138218
protected function save(string $file, array $options): string
139219
{
220+
if ($options['format'] !== null) {
221+
$file = basename($file) . '.' . $options['format'];
222+
}
140223
return sprintf('-limit thread 1 "%s"', $file);
141224
}
142225

226+
/**
227+
* Removes all metadata from the image
228+
*
229+
* @param string $file
230+
* @param array $options
231+
* @return string
232+
*/
143233
protected function strip(string $file, array $options): string
144234
{
145235
return '-strip';

0 commit comments

Comments
 (0)