‪TYPO3CMS  ‪main
ImageProcessingInstructionsTest.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
20 use PHPUnit\Framework\Attributes\DataProvider;
21 use PHPUnit\Framework\Attributes\Test;
23 use TYPO3\CMS\Core\Imaging\ImageProcessingInstructions;
24 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
25 
26 final class ‪ImageProcessingInstructionsTest extends UnitTestCase
27 {
34  public static function fromCropScaleValuesImageDataProvider(): iterable
35  {
36  ‪$result = new ImageProcessingInstructions(
37  width: 150,
38  height: 120,
39  );
40  ‪yield 'Get image scale for a width of 150px' => [
41  170,
42  136,
43  '150',
44  '',
45  [],
46  true,
47  ‪$result,
48  ];
49 
50  ‪$result = new ImageProcessingInstructions(
51  width: 100,
52  height: 80,
53  );
54  ‪yield 'Get image scale with a maximum width of 100px' => [
55  170,
56  136,
57  '',
58  '',
59  ['maxW' => 100],
60  true,
61  ‪$result,
62  ];
63 
64  ‪$result = new ImageProcessingInstructions(
65  width: 200,
66  height: 160,
67  );
68  ‪yield 'Get image scale with a minimum width of 200px' => [
69  170,
70  136,
71  '',
72  '',
73  ['minW' => 200],
74  true,
75  ‪$result,
76  ];
77 
78  ‪$result = new ImageProcessingInstructions(
79  width: 0,
80  height: 0,
81  );
82  ‪yield 'No PHP warning for zero in input dimensions when scaling' => [
83  0,
84  0,
85  '50',
86  '',
87  [],
88  true,
89  ‪$result,
90  ];
91 
92  ‪$result = new ImageProcessingInstructions(
93  width: 50,
94  height: 450,
95  );
96  ‪yield 'width from original image and explicitly given scales an image down' => [
97  100,
98  900,
99  '50',
100  '',
101  [],
102  true,
103  ‪$result,
104  ];
105 
106  ‪$result = new ImageProcessingInstructions(
107  width: 33,
108  height: 300,
109  );
110  ‪yield 'width from original image with maxH set, also fills "origH" value' => [
111  100,
112  900,
113  '50',
114  '',
115  ['maxH' => 300],
116  true,
117  ‪$result,
118  ];
119 
120  ‪$result = new ImageProcessingInstructions(
121  width: 150,
122  height: 1350,
123  );
124  ‪yield 'width from original image and explicitly given scales an image up' => [
125  100,
126  900,
127  '150',
128  '',
129  [],
130  true,
131  ‪$result,
132  ];
133 
134  ‪$result = new ImageProcessingInstructions(
135  width: 100,
136  height: 900,
137  );
138  ‪yield 'width from original image and explicitly given scales an image up but is disabled' => [
139  100,
140  900,
141  '150',
142  '',
143  [],
144  false,
145  ‪$result,
146  ];
147 
148  ‪$result = new ImageProcessingInstructions(
149  width: 100,
150  height: 900,
151  );
152  ‪yield 'min width explicitly given scales an image up but is disabled' => [
153  100,
154  900,
155  '',
156  '',
157  ['minW' => 150],
158  false,
159  ‪$result,
160  ];
161 
162  ‪$result = new ImageProcessingInstructions(
163  width: 0,
164  height: 0,
165  );
166  ‪yield 'no orig image given monitors "origW"' => [
167  0,
168  0,
169  '50',
170  '100',
171  [],
172  true,
173  ‪$result,
174  ];
175 
176  ‪$result = new ImageProcessingInstructions(
177  width: 50,
178  height: 450,
179  );
180  ‪yield 'Incoming instructions use "m" in width with given height' => [
181  100,
182  900,
183  '50m',
184  '800',
185  [],
186  true,
187  ‪$result,
188  ];
189 
190  ‪$result = new ImageProcessingInstructions(
191  width: 50,
192  height: 450,
193  );
194  ‪yield 'Incoming instructions use "m" in width without height' => [
195  100,
196  900,
197  '50m',
198  '',
199  [],
200  true,
201  ‪$result,
202  ];
203 
204  ‪$result = new ImageProcessingInstructions(
205  width: 50,
206  height: 800,
207  cropArea: new ‪Area(21.875, 0, 56.25 /* 900 / 800 * 50 */, 900),
208  );
209  ‪yield 'Incoming instructions use "c" in width with given height' => [
210  100,
211  900,
212  '50c',
213  '800',
214  [],
215  true,
216  ‪$result,
217  ];
218 
219  ‪$result = new ImageProcessingInstructions(
220  width: 50,
221  height: 450,
222  cropArea: null,
223  );
224  ‪yield 'Incoming instructions use "c" in width but without height' => [
225  100,
226  900,
227  '50c',
228  '',
229  [],
230  true,
231  ‪$result,
232  ];
233 
234  ‪$result = new ImageProcessingInstructions(
235  width: 50,
236  height: 650,
237  cropArea: new ‪Area(18.461538461538456, 0, 69.23076923076924 /* 900 / 650 * 50 */, 900),
238  );
239  ‪yield 'Incoming instructions use "c" in width on both sides' => [
240  100,
241  900,
242  '50c20',
243  '650c40',
244  [],
245  true,
246  ‪$result,
247  ];
248 
249  ‪$result = new ImageProcessingInstructions(
250  width: 50,
251  height: 800,
252  cropArea: new ‪Area(26.25, 0, 56.25 /* 900 / 800 * 50 */, 900),
253  );
254  ‪yield 'Incoming instructions use "c" in width on both sides with given height' => [
255  100,
256  900,
257  '50c20',
258  '800',
259  [],
260  true,
261  ‪$result,
262  ];
263  }
264 
269  #[DataProvider('fromCropScaleValuesImageDataProvider')]
270  #[Test]
271  public function ‪fromCropScaleValuesTest(
272  int $incomingWidth,
273  int $incomingHeight,
274  string $width,
275  string $height,
276  array $options,
277  bool $allowUpscaling,
278  ImageProcessingInstructions $expected
279  ): void {
280  ‪$GLOBALS['TYPO3_CONF_VARS']['GFX']['processor_allowUpscaling'] = $allowUpscaling;
281  ‪$result = ImageProcessingInstructions::fromCropScaleValues($incomingWidth, $incomingHeight, $width, $height, $options);
282  self::assertEquals($expected, ‪$result);
283  }
284 }
‪TYPO3\CMS\Core\Tests\Unit\Imaging\ImageProcessingInstructionsTest\yield
‪yield
Definition: ImageProcessingInstructionsTest.php:54
‪TYPO3\CMS\Core\Imaging\ImageManipulation\Area
Definition: Area.php:23
‪TYPO3\CMS\Core\Tests\Unit\Imaging\ImageProcessingInstructionsTest\$result
‪$result
Definition: ImageProcessingInstructionsTest.php:50
‪TYPO3\CMS\Core\Tests\Unit\Imaging\ImageProcessingInstructionsTest\fromCropScaleValuesTest
‪fromCropScaleValuesTest(int $incomingWidth, int $incomingHeight, string $width, string $height, array $options, bool $allowUpscaling, ImageProcessingInstructions $expected)
Definition: ImageProcessingInstructionsTest.php:271
‪TYPO3\CMS\Core\Tests\Unit\Imaging
Definition: DimensionTest.php:18
‪TYPO3\CMS\Core\Tests\Unit\Imaging\ImageProcessingInstructionsTest
Definition: ImageProcessingInstructionsTest.php:27
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25