-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
ImageMagickHandlerTest.php
445 lines (376 loc) · 15.4 KB
/
ImageMagickHandlerTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
<?php
/**
* This file is part of CodeIgniter 4 framework.
*
* (c) CodeIgniter Foundation <admin@codeigniter.com>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace CodeIgniter\Images;
use CodeIgniter\Config\Services;
use CodeIgniter\Images\Exceptions\ImageException;
use CodeIgniter\Images\Handlers\BaseHandler;
use CodeIgniter\Test\CIUnitTestCase;
use Config\Images;
use Imagick;
/**
* Unit testing for the ImageMagick image handler.
* It is impractical to programmatically inspect the results of the
* different transformations, so we have to rely on the underlying package.
* We can make sure that we can call it without blowing up,
* and we can make sure the code coverage is good.
*
* Was unable to test fontPath & related logic.
*
* @internal
*/
final class ImageMagickHandlerTest extends CIUnitTestCase
{
private string $root;
private string $origin;
private string $path;
private BaseHandler $handler;
protected function setUp(): void
{
if (! extension_loaded('imagick')) {
$this->markTestSkipped('The ImageMagick extension is not available.');
}
$this->root = WRITEPATH . 'cache/';
// cleanup everything
helper('filesystem');
delete_files($this->root, false, true);
// copy our support files
$this->origin = SUPPORTPATH . 'Images/';
$this->path = $this->origin . 'ci-logo.png';
$handlerConfig = new Images();
if (is_file('/usr/bin/convert')) {
$handlerConfig->libraryPath = '/usr/bin/convert';
}
$this->handler = Services::image('imagick', $handlerConfig, false);
}
public function testGetVersion()
{
$version = $this->handler->getVersion();
// make sure that the call worked
$this->assertNotFalse($version);
// we should have a numeric version, greater than 6
$this->assertGreaterThanOrEqual(0, version_compare($version, '6.0.0'));
$this->assertLessThan(0, version_compare($version, '99.0.0'));
}
public function testImageProperties()
{
$this->handler->withFile($this->path);
$file = $this->handler->getFile();
$props = $file->getProperties(true);
$this->assertSame(155, $this->handler->getWidth());
$this->assertSame(155, $props['width']);
$this->assertSame(155, $file->origWidth);
$this->assertSame(200, $this->handler->getHeight());
$this->assertSame(200, $props['height']);
$this->assertSame(200, $file->origHeight);
$this->assertSame('width="155" height="200"', $props['size_str']);
}
public function testImageTypeProperties()
{
$this->handler->withFile($this->path);
$file = $this->handler->getFile();
$props = $file->getProperties(true);
$this->assertSame(IMAGETYPE_PNG, $props['image_type']);
$this->assertSame('image/png', $props['mime_type']);
}
public function testResizeIgnored()
{
$this->handler->withFile($this->path);
$this->handler->resize(155, 200); // 155x200 result
$this->assertSame(155, $this->handler->getWidth());
$this->assertSame(200, $this->handler->getHeight());
}
public function testResizeAbsolute()
{
$this->handler->withFile($this->path);
$this->handler->resize(123, 456, false); // 123x456 result
$this->assertSame(123, $this->handler->getWidth());
$this->assertSame(456, $this->handler->getHeight());
}
public function testResizeAspect()
{
$this->handler->withFile($this->path);
$this->handler->resize(123, 456, true); // 123x159 result
$this->assertSame(123, $this->handler->getWidth());
$this->assertSame(159, $this->handler->getHeight());
}
public function testResizeAspectWidth()
{
$this->handler->withFile($this->path);
$this->handler->resize(123, 0, true); // 123x159 result
$this->assertSame(123, $this->handler->getWidth());
$this->assertSame(159, $this->handler->getHeight());
}
public function testResizeAspectHeight()
{
$this->handler->withFile($this->path);
$this->handler->resize(0, 456, true); // 353x456 result
$this->assertSame(353, $this->handler->getWidth());
$this->assertSame(456, $this->handler->getHeight());
}
public function testCropTopLeft()
{
$this->handler->withFile($this->path);
$this->handler->crop(100, 100); // 100x100 result
$this->assertSame(100, $this->handler->getWidth());
$this->assertSame(100, $this->handler->getHeight());
}
public function testCropMiddle()
{
$this->handler->withFile($this->path);
$this->handler->crop(100, 100, 50, 50, false); // 100x100 result
$this->assertSame(100, $this->handler->getWidth());
$this->assertSame(100, $this->handler->getHeight());
}
public function testCropMiddlePreserved()
{
$this->handler->withFile($this->path);
$this->handler->crop(100, 100, 50, 50, true); // 78x100 result
$this->assertSame(78, $this->handler->getWidth());
$this->assertSame(100, $this->handler->getHeight());
}
public function testCropTopLeftPreserveAspect()
{
$this->handler->withFile($this->path);
$this->handler->crop(100, 100); // 100x100 result
$this->assertSame(100, $this->handler->getWidth());
$this->assertSame(100, $this->handler->getHeight());
}
public function testCropNothing()
{
$this->handler->withFile($this->path);
$this->handler->crop(155, 200); // 155x200 result
$this->assertSame(155, $this->handler->getWidth());
$this->assertSame(200, $this->handler->getHeight());
}
public function testCropOutOfBounds()
{
$this->handler->withFile($this->path);
$this->handler->crop(100, 100, 100); // 55x100 result in 100x100
$this->assertSame(100, $this->handler->getWidth());
$this->assertSame(100, $this->handler->getHeight());
}
public function testRotate()
{
$this->handler->withFile($this->path); // 155x200
$this->assertSame(155, $this->handler->getWidth());
$this->assertSame(200, $this->handler->getHeight());
// first rotation
$this->handler->rotate(90); // 200x155
$this->assertSame(200, $this->handler->getWidth());
// check image size again after another rotation
$this->handler->rotate(180); // 200x155
$this->assertSame(200, $this->handler->getWidth());
}
public function testRotateBadAngle()
{
$this->handler->withFile($this->path);
$this->expectException(ImageException::class);
$this->handler->rotate(77);
}
public function testFlatten()
{
$this->handler->withFile($this->path);
$this->handler->flatten();
$this->assertSame(155, $this->handler->getWidth());
$this->assertSame(200, $this->handler->getHeight());
}
public function testFlip()
{
$this->handler->withFile($this->path);
$this->handler->flip();
$this->assertSame(155, $this->handler->getWidth());
$this->assertSame(200, $this->handler->getHeight());
}
public function testHorizontal()
{
$this->handler->withFile($this->path);
$this->handler->flip('horizontal');
$this->assertSame(155, $this->handler->getWidth());
$this->assertSame(200, $this->handler->getHeight());
}
public function testFlipVertical()
{
$this->handler->withFile($this->path);
$this->handler->flip('vertical');
$this->assertSame(155, $this->handler->getWidth());
$this->assertSame(200, $this->handler->getHeight());
}
public function testFlipUnknown()
{
$this->handler->withFile($this->path);
$this->expectException(ImageException::class);
$this->handler->flip('bogus');
}
public function testFit()
{
$this->handler->withFile($this->path);
$this->handler->fit(100, 100);
$this->assertSame(100, $this->handler->getWidth());
$this->assertSame(100, $this->handler->getHeight());
}
public function testFitTaller()
{
$this->handler->withFile($this->path);
$this->handler->fit(100, 400);
$this->assertSame(100, $this->handler->getWidth());
$this->assertSame(400, $this->handler->getHeight());
}
public function testFitAutoHeight()
{
$this->handler->withFile($this->path);
$this->handler->fit(100);
$this->assertSame(100, $this->handler->getWidth());
$this->assertSame(129, $this->handler->getHeight());
}
public function testFitPositions()
{
$choices = [
'top-left',
'top',
'top-right',
'left',
'center',
'right',
'bottom-left',
'bottom',
'bottom-right',
];
$this->handler->withFile($this->path);
foreach ($choices as $position) {
$this->handler->fit(100, 100, $position);
$this->assertSame(100, $this->handler->getWidth(), 'Position ' . $position . ' failed');
$this->assertSame(100, $this->handler->getHeight(), 'Position ' . $position . ' failed');
}
}
public function testText()
{
$this->handler->withFile($this->path);
$this->handler->text('vertical', ['hAlign' => 'right', 'vAlign' => 'bottom']);
$this->assertSame(155, $this->handler->getWidth());
$this->assertSame(200, $this->handler->getHeight());
}
public function testMoreText()
{
$this->handler->withFile($this->path);
$this->handler->text('vertical', ['vAlign' => 'middle', 'withShadow' => 'sure', 'shadowOffset' => 3]);
$this->assertSame(155, $this->handler->getWidth());
$this->assertSame(200, $this->handler->getHeight());
}
public function testImageCreation()
{
foreach (['gif', 'jpeg', 'png', 'webp'] as $type) {
if ($type === 'webp' && ! in_array('WEBP', Imagick::queryFormats(), true)) {
$this->expectException(ImageException::class);
$this->expectExceptionMessage('Your server does not support the GD function required to process this type of image.');
}
$this->handler->withFile($this->origin . 'ci-logo.' . $type);
$this->handler->text('vertical');
$this->assertSame(155, $this->handler->getWidth());
$this->assertSame(200, $this->handler->getHeight());
}
}
public function testImageCopy()
{
foreach (['gif', 'jpeg', 'png', 'webp'] as $type) {
if ($type === 'webp' && ! in_array('WEBP', Imagick::queryFormats(), true)) {
$this->expectException(ImageException::class);
$this->expectExceptionMessage('Your server does not support the GD function required to process this type of image.');
}
$this->handler->withFile($this->origin . 'ci-logo.' . $type);
$this->handler->save($this->root . 'ci-logo.' . $type);
$this->assertFileExists($this->root . 'ci-logo.' . $type);
$this->assertNotSame(
file_get_contents($this->origin . 'ci-logo.' . $type),
file_get_contents($this->root . 'ci-logo.' . $type)
);
}
}
public function testImageCopyWithNoTargetAndMaxQuality()
{
foreach (['gif', 'jpeg', 'png', 'webp'] as $type) {
$this->handler->withFile($this->origin . 'ci-logo.' . $type);
$this->handler->save(null, 100);
$this->assertFileExists($this->origin . 'ci-logo.' . $type);
$this->assertSame(
file_get_contents($this->origin . 'ci-logo.' . $type),
file_get_contents($this->origin . 'ci-logo.' . $type)
);
}
}
public function testImageCompressionGetResource()
{
foreach (['gif', 'jpeg', 'png', 'webp'] as $type) {
if ($type === 'webp' && ! in_array('WEBP', Imagick::queryFormats(), true)) {
$this->expectException(ImageException::class);
$this->expectExceptionMessage('Your server does not support the GD function required to process this type of image.');
}
$this->handler->withFile($this->origin . 'ci-logo.' . $type);
$this->handler->getResource(); // make sure resource is loaded
$this->handler->save($this->root . 'ci-logo.' . $type);
$this->assertFileExists($this->root . 'ci-logo.' . $type);
$this->assertNotSame(
file_get_contents($this->origin . 'ci-logo.' . $type),
file_get_contents($this->root . 'ci-logo.' . $type)
);
}
}
public function testImageCompressionWithResource()
{
foreach (['gif', 'jpeg', 'png', 'webp'] as $type) {
if ($type === 'webp' && ! in_array('WEBP', Imagick::queryFormats(), true)) {
$this->expectException(ImageException::class);
$this->expectExceptionMessage('Your server does not support the GD function required to process this type of image.');
}
$this->handler->withFile($this->origin . 'ci-logo.' . $type)
->withResource() // make sure resource is loaded
->save($this->root . 'ci-logo.' . $type);
$this->assertFileExists($this->root . 'ci-logo.' . $type);
$this->assertNotSame(
file_get_contents($this->origin . 'ci-logo.' . $type),
file_get_contents($this->root . 'ci-logo.' . $type)
);
}
}
public function testImageConvert()
{
$this->handler->withFile($this->origin . 'ci-logo.jpeg');
$this->handler->convert(IMAGETYPE_PNG);
$this->handler->save($this->root . 'ci-logo.png');
$this->assertSame(exif_imagetype($this->root . 'ci-logo.png'), IMAGETYPE_PNG);
}
public function testImageReorientLandscape()
{
for ($i = 0; $i <= 8; $i++) {
$source = $this->origin . 'EXIFsamples/landscape_' . '' . $i . '.jpg';
$result = $this->root . 'landscape_' . $i . '_reoriented.jpg';
$this->handler->withFile($source);
$this->handler->reorient();
$resource = imagecreatefromstring(file_get_contents($this->handler->getResource()));
$point = imagecolorat($resource, 0, 0);
$rgb = imagecolorsforindex($resource, $point);
$this->handler->save($result);
$this->assertSame(['red' => 62, 'green' => 62, 'blue' => 62, 'alpha' => 0], $rgb);
}
}
public function testImageReorientPortrait()
{
for ($i = 0; $i <= 8; $i++) {
$source = $this->origin . 'EXIFsamples/portrait_' . '' . $i . '.jpg';
$result = $this->root . 'portrait_' . $i . '_reoriented.jpg';
$this->handler->withFile($source);
$this->handler->reorient();
$resource = imagecreatefromstring(file_get_contents($this->handler->getResource()));
$point = imagecolorat($resource, 0, 0);
$rgb = imagecolorsforindex($resource, $point);
$this->handler->save($result);
$this->assertSame(['red' => 62, 'green' => 62, 'blue' => 62, 'alpha' => 0], $rgb);
}
}
}