-
Notifications
You must be signed in to change notification settings - Fork 191
/
ImageTests.php
executable file
·490 lines (411 loc) · 13.8 KB
/
ImageTests.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
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
<?php
use Gregwar\Cache\Cache;
use Gregwar\Cache\CacheInterface;
use Gregwar\Image\Image;
use Gregwar\Image\ImageColor;
/**
* Unit testing for Image.
*/
class ImageTests extends \PHPUnit\Framework\TestCase
{
/**
* Testing the basic width & height.
*/
public function testBasics(): void
{
$image = $this->open('monalisa.jpg');
self::assertSame($image->width(), 771);
self::assertSame($image->height(), 961);
}
/**
* Testing the resize.
*/
public function testResize(): void
{
$image = $this->open('monalisa.jpg');
$out = $this->output('monalisa_small.jpg');
$image
->resize(300, 200)
->save($out)
;
self::assertFileExists($out);
$i = imagecreatefromjpeg($out);
self::assertSame(300, imagesx($i));
self::assertSame(200, imagesy($i));
}
/**
* Testing the resize %.
*/
public function testResizePercent(): void
{
$image = $this->open('monalisa.jpg');
$out = $this->output('monalisa_small.jpg');
$image
->resize('50%')
->save($out)
;
self::assertFileExists($out);
$i = imagecreatefromjpeg($out);
self::assertSame(386, imagesx($i));
self::assertSame(481, imagesy($i));
}
/**
* Testing to create an image, jpeg, gif and png.
*/
public function testCreateImage(): void
{
$black = $this->output('black.jpg');
Image::create(150, 200)
->fill('black')
->save($black, 100);
$i = imagecreatefromjpeg($black);
self::assertFileExists($black);
self::assertSame(150, imagesx($i));
self::assertSame(200, imagesy($i));
$j = imagecolorat($i, 40, 40);
self::assertSame(0, $j);
$black = $this->output('black.png');
Image::create(150, 200)
->fill('black')
->save($black, 'png');
$i = imagecreatefrompng($black);
self::assertFileExists($black);
self::assertSame(150, imagesx($i));
self::assertSame(200, imagesy($i));
$black = $this->output('black.gif');
Image::create(150, 200)
->fill('black')
->save($black, 'gif');
$i = imagecreatefromgif($black);
self::assertFileExists($black);
self::assertSame(150, imagesx($i));
self::assertSame(200, imagesy($i));
}
/**
* Testing type guess.
*/
public function testGuess(): void
{
$image = $this->open('monalisa.jpg');
self::assertSame('jpeg', $image->guessType());
$image = $this->open('monalisa.png');
self::assertSame('png', $image->guessType());
$image = $this->open('monalisa.gif');
self::assertSame('gif', $image->guessType());
}
public function testDefaultCacheSystem(): void
{
$image = $this->open('monalisa.jpg');
self::assertInstanceOf('Gregwar\Cache\Cache', $image->getCacheSystem());
}
public function testCustomCacheSystem(): void
{
$image = $this->open('monalisa.jpg');
$cache = new Cache();
$image->setCacheSystem($cache);
self::assertInstanceOf(Gregwar\Cache\CacheInterface::class, $image->getCacheSystem());
}
/**
* Testing that caching an image without operations will result
* in the original image when force cache is disabled.
*/
public function testNoCache(): void
{
$monalisa = __DIR__.'/files/monalisa.jpg';
$image = $this->open('monalisa.jpg')->setForceCache(false);
self::assertSame($monalisa, $image->guess());
$image = $this->open('monalisa.jpg');
self::assertNotSame($monalisa, $image->guess());
$image = $this->open('monalisa.jpg')->setForceCache(true);
self::assertNotSame($monalisa, $image->guess());
}
public function testActualCache(): void
{
$output = $this->open('monalisa.jpg')
->setCacheDir('/magic/path/to/cache/')
->resize(100, 50)->negate()
->guess();
self::assertStringContainsString('/magic/path/to/cache', $output);
$file = str_replace('/magic/path/to', __DIR__.'/output/', $output);
self::assertFileExists($file);
}
public function testCacheData(): void
{
$output = $this->open('monalisa.jpg')
->resize(300, 200)
->cacheData();
$jpg = imagecreatefromstring($output);
self::assertSame(300, imagesx($jpg));
self::assertSame(200, imagesy($jpg));
}
/**
* Testing using cache.
*/
public function testCache(): void
{
$output = $this->open('monalisa.jpg')
->resize(100, 50)->negate()
->guess();
self::assertFileExists($output);
$i = imagecreatefromjpeg($output);
self::assertSame(100, imagesx($i));
self::assertSame(50, imagesy($i));
$output2 = $this->open('monalisa.jpg')
->resize(100, 50)->negate()
->guess();
self::assertSame($output, $output2);
$output3 = $this->open('monalisa.jpg')
->resize(100, 50)->negate()
->png();
self::assertFileExists($output);
$i = imagecreatefrompng($output3);
self::assertSame(100, imagesx($i));
self::assertSame(50, imagesy($i));
$output4 = $this->open('monalisa.jpg')
->resize(100, 50)->negate()
->gif();
self::assertFileExists($output);
$i = imagecreatefromgif($output4);
self::assertSame(100, imagesx($i));
self::assertSame(50, imagesy($i));
}
/**
* Testing Gaussian blur filter.
*/
public function testGaussianBlur(): void
{
$image = $this->open('monalisa.jpg')
->gaussianBlur();
$secondImage = $this->open('monalisa.jpg')
->gaussianBlur(5);
self::assertFileExists($image);
self::assertFileExists($secondImage);
}
/**
* Testing creating image from data.
*/
public function testData(): void
{
$data = file_get_contents(__DIR__.'/files/monalisa.jpg');
$output = $this->output('mona.jpg');
$image = Image::fromData($data);
$image->save($output);
self::assertFileExists($output);
$i = imagecreatefromjpeg($output);
self::assertSame(771, imagesx($i));
self::assertSame(961, imagesy($i));
}
/**
* Opening an image.
*/
protected function open(string $file): Image
{
$image = Image::open(__DIR__.'/files/'.$file);
$image->setCacheDir(__DIR__.'/output/cache/');
$image->setActualCacheDir(__DIR__.'/output/cache/');
return $image;
}
/**
* Testing transparent image.
*/
public function testTransparent(): void
{
$gif = $this->output('transparent.gif');
$i = Image::create(200, 100)
->fill('transparent')
->save($gif, 'gif');
self::assertFileExists($gif);
$img = imagecreatefromgif($gif);
self::assertSame(200, imagesx($img));
self::assertSame(100, imagesy($img));
$index = imagecolorat($img, 2, 2);
$color = imagecolorsforindex($img, $index);
self::assertSame(127, $color['alpha']);
}
public function testNonExistingFile(): void
{
$jpg = $this->output('a.jpg');
$img = $this->open('non_existing_file.jpg')
->negate();
$error = $img->save($jpg);
self::assertFileExists($error);
}
public function testNonExistingFileNoFallback(): void
{
$this->expectException(\UnexpectedValueException::class);
Image::open('non_existing_file.jpg')
->useFallback(false)
->save($this->output('a.jpg'));
}
/**
* Test that image::save returns the file name.
*/
public function testSaveReturn(): void
{
$red = $this->output('red.jpg');
$result = Image::create(10, 10)
->fill('red')
->save($red);
self::assertSame($red, $result);
}
/**
* Testing merge.
*/
public function testMerge(): void
{
$out = $this->output('merge.jpg');
Image::create(100, 100)
->fill('red')
->merge(Image::create(50, 50)
->fill('black')
)
->save($out);
// Merge file exists
self::assertFileExists($out);
// Test that the upper left zone contain a black pixel, and the lower
// down contains a red one
$img = imagecreatefromjpeg($out);
$this->assertColorEquals('black', imagecolorat($img, 5, 12));
$this->assertColorEquals('red', imagecolorat($img, 55, 62));
}
/**
* Test that dependencies are well generated.
*/
public function testDependencies(): void
{
self::assertSame(array(), Image::create(100, 100)
->getDependencies());
self::assertSame(array(), Image::create(100, 100)
->merge(Image::create(100, 50))
->getDependencies());
self::assertSame(array('toto.jpg'), Image::open('toto.jpg')
->merge(Image::create(100, 50))
->getDependencies());
self::assertSame(array('toto.jpg', 'titi.jpg'), Image::open('toto.jpg')
->merge(Image::open('titi.jpg'))
->getDependencies());
self::assertSame(array('toto.jpg', 'titi.jpg', 'tata.jpg'), Image::open('toto.jpg')
->merge(Image::open('titi.jpg')
->merge(Image::open('tata.jpg')))
->getDependencies());
}
/**
* Test that pretty name (for referencing) is well respected.
*/
public function testPrettyName(): void
{
$output = $this->open('monalisa.jpg')
->resize(100, 50)->negate()
->setPrettyName('davinci', false)
->guess();
self::assertStringContainsString('davinci', $output);
$output2 = $this->open('monalisa.jpg')
->resize(100, 55)->negate()
->setPrettyName('davinci', false)
->guess();
self::assertSame($output, $output2);
$prefix1 = $this->open('monalisa.jpg')
->resize(100, 50)->negate()
->setPrettyName('davinci')
->guess();
$prefix2 = $this->open('monalisa.jpg')
->resize(100, 55)->negate()
->setPrettyName('davinci')
->guess();
self::assertStringContainsString('davinci', $prefix1);
self::assertStringContainsString('davinci', $prefix2);
self::assertNotSame($prefix1, $prefix2);
$transliterator = '\Behat\Transliterator\Transliterator';
if (class_exists($transliterator)) {
$nonLatinName1 = 'ダヴィンチ';
$nonLatinOutput1 = $this->open('monalisa.jpg')
->resize(100, 50)->negate()
->setPrettyName($nonLatinName1, false)
->guess();
self::assertContains(
$transliterator::urlize($transliterator::transliterate($nonLatinName1)),
$nonLatinOutput1
);
$nonLatinName2 = 'давинчи';
$nonLatinOutput2 = $this->open('monalisa.jpg')
->resize(100, 55)->negate()
->setPrettyName($nonLatinName2)
->guess();
self::assertContains(
$transliterator::urlize($transliterator::transliterate($nonLatinName2)),
$nonLatinOutput2
);
}
}
/**
* Testing inlining.
*/
public function testInline(): void
{
$output = $this->open('monalisa.jpg')
->resize(20, 20)
->inline();
self::assertSame(0, strpos($output, 'data:image/jpeg;base64,'));
$data = base64_decode(substr($output, 23));
$image = imagecreatefromstring($data);
self::assertSame(20, imagesx($image));
self::assertSame(20, imagesy($image));
}
/**
* Testing that width() can be called after cache
*/
public function testWidthPostCache(): void
{
$this->open('monalisa.jpg')
->resize(100, 50)->negate()
->png();
$dummy2 = $this->open('monalisa.jpg')
->resize(100, 50)->negate();
$png = $dummy2->png();
$i = imagecreatefrompng($png);
self::assertEquals(imagesx($i), $dummy2->width());
}
/**
* Asserting that two colors are equals
* (JPG compression is not preserving colors for instance, so we
* need a non-strict way to compare it).
*/
protected function assertColorEquals($c1, $c2, $delta = 8): void
{
$c1 = ImageColor::parse($c1);
$c2 = ImageColor::parse($c2);
list($r1, $g1, $b1) = $this->toRGB($c1);
list($r2, $g2, $b2) = $this->toRGB($c2);
self::assertLessThan($delta, abs($r1 - $r2));
self::assertLessThan($delta, abs($g1 - $g2));
self::assertLessThan($delta, abs($b1 - $b2));
}
protected function toRGB($color): array
{
$b = $color & 0xff;
$g = ($color >> 8) & 0xff;
$r = ($color >> 16) & 0xff;
return array($r, $g, $b);
}
/**
* Outputting an image to a file.
*/
protected function output(string $file): string
{
return __DIR__.'/output/'.$file;
}
/**
* Reinitialize the output dir.
*/
public function setUp(): void
{
$dir = $this->output('');
`rm -rf $dir`;
if( !mkdir($dir) && !is_dir($dir) ){
throw new \RuntimeException(sprintf('Directory "%s" was not created', $dir));
}
if( !mkdir($concurrentDirectory = $this->output('cache')) && !is_dir($concurrentDirectory) ){
throw new \RuntimeException(sprintf('Directory "%s" was not created', $concurrentDirectory));
}
}
}