33namespace Flokosiol \Focus ;
44
55use Exception ;
6- use Kirby \Image \Image ;
7- use Kirby \Image \Dimensions ;
6+ use Kirby \Filesystem \F ;
87use Kirby \Image \Darkroom ;
9- use Kirby \Toolkit \ F ;
8+ use Kirby \Image \ Image ;
109
1110class 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