1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace TraderInteractive\Util; |
4
|
|
|
|
5
|
|
|
use PHPUnit\Framework\TestCase; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* @coversDefaultClass \TraderInteractive\Util\Image |
9
|
|
|
*/ |
10
|
|
|
final class ImageTest extends TestCase |
11
|
|
|
{ |
12
|
|
|
private $sourceFilesDir; |
13
|
|
|
private $tempDir; |
14
|
|
|
|
15
|
|
|
public function setUp() |
16
|
|
|
{ |
17
|
|
|
$this->sourceFilesDir = __DIR__ . '/_files'; |
18
|
|
|
$this->tempDir = sys_get_temp_dir() . '/imageUtilTest'; |
19
|
|
|
if (is_dir($this->tempDir)) { |
20
|
|
|
foreach (glob("{$this->tempDir}/*") as $file) { |
21
|
|
|
unlink($file); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
rmdir($this->tempDir); |
25
|
|
|
} |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Downsize ratio 2.0 to 0.25 |
30
|
|
|
* |
31
|
|
|
* @test |
32
|
|
|
* @covers ::resize |
33
|
|
|
* @covers ::resizeMulti |
34
|
|
|
*/ |
35
|
|
|
public function resizeDownsizeToMoreVerticalAspect() |
36
|
|
|
{ |
37
|
|
|
$source = new \Imagick('pattern:gray0'); |
38
|
|
|
$source->scaleImage(100, 50); |
39
|
|
|
|
40
|
|
|
$imagick = Image::resize($source, 10, 40, ['color' => 'white', 'maxWidth' => 10000, 'maxHeight' => 10000]); |
41
|
|
|
|
42
|
|
|
//making sure source didnt resize |
43
|
|
|
$this->assertSame(100, $source->getImageWidth()); |
44
|
|
|
$this->assertSame(50, $source->getImageHeight()); |
45
|
|
|
|
46
|
|
|
$this->assertSame(10, $imagick->getImageWidth()); |
47
|
|
|
$this->assertSame(40, $imagick->getImageHeight()); |
48
|
|
|
|
49
|
|
|
$whiteBarTop = $imagick->getImagePixelColor(4, 16)->getHsl(); |
50
|
|
|
$whiteBarBottom = $imagick->getImagePixelColor(4, 22)->getHsl(); |
51
|
|
|
|
52
|
|
|
$imageLeft = $imagick->getImagePixelColor(0, 19)->getHsl(); |
53
|
|
|
$imageRight = $imagick->getImagePixelColor(9, 19)->getHsl(); |
54
|
|
|
$imageTop = $imagick->getImagePixelColor(4, 17)->getHsl(); |
55
|
|
|
$imageBottom = $imagick->getImagePixelColor(4, 21)->getHsl(); |
56
|
|
|
|
57
|
|
|
$this->assertGreaterThan(0.9, $whiteBarTop['luminosity']); |
58
|
|
|
$this->assertGreaterThan(0.9, $whiteBarBottom['luminosity']); |
59
|
|
|
|
60
|
|
|
$this->assertLessThan(0.1, $imageLeft['luminosity']); |
61
|
|
|
$this->assertLessThan(0.1, $imageRight['luminosity']); |
62
|
|
|
$this->assertLessThan(0.1, $imageTop['luminosity']); |
63
|
|
|
$this->assertLessThan(0.1, $imageBottom['luminosity']); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Downsize ratio 2.0 to 4.0 |
68
|
|
|
* |
69
|
|
|
* @test |
70
|
|
|
* @covers ::resize |
71
|
|
|
* @covers ::resizeMulti |
72
|
|
|
*/ |
73
|
|
View Code Duplication |
public function resizeDownsizeToMoreHorizontalAspect() |
|
|
|
|
74
|
|
|
{ |
75
|
|
|
$source = new \Imagick('pattern:gray0'); |
76
|
|
|
$source->scaleImage(100, 50); |
77
|
|
|
|
78
|
|
|
$imagick = Image::resize($source, 40, 10); |
79
|
|
|
|
80
|
|
|
//making sure source didnt resize |
81
|
|
|
$this->assertSame(100, $source->getImageWidth()); |
82
|
|
|
$this->assertSame(50, $source->getImageHeight()); |
83
|
|
|
|
84
|
|
|
$this->assertSame(40, $imagick->getImageWidth()); |
85
|
|
|
$this->assertSame(10, $imagick->getImageHeight()); |
86
|
|
|
|
87
|
|
|
$whiteBarLeft = $imagick->getImagePixelColor(9, 4)->getHsl(); |
88
|
|
|
$whiteBarRight = $imagick->getImagePixelColor(30, 4)->getHsl(); |
89
|
|
|
|
90
|
|
|
$imageLeft = $imagick->getImagePixelColor(10, 4)->getHsl(); |
91
|
|
|
$imageRight = $imagick->getImagePixelColor(29, 4)->getHsl(); |
92
|
|
|
$imageTop = $imagick->getImagePixelColor(19, 0)->getHsl(); |
93
|
|
|
$imageBottom = $imagick->getImagePixelColor(19, 9)->getHsl(); |
94
|
|
|
|
95
|
|
|
$this->assertGreaterThan(0.9, $whiteBarLeft['luminosity']); |
96
|
|
|
$this->assertGreaterThan(0.9, $whiteBarRight['luminosity']); |
97
|
|
|
|
98
|
|
|
$this->assertLessThan(0.1, $imageLeft['luminosity']); |
99
|
|
|
$this->assertLessThan(0.1, $imageRight['luminosity']); |
100
|
|
|
$this->assertLessThan(0.1, $imageTop['luminosity']); |
101
|
|
|
$this->assertLessThan(0.1, $imageBottom['luminosity']); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Upsize ratio 2.0 to 4.0 |
106
|
|
|
* |
107
|
|
|
* @test |
108
|
|
|
* @covers ::resize |
109
|
|
|
* @covers ::resizeMulti |
110
|
|
|
*/ |
111
|
|
View Code Duplication |
public function resizeUpsizeToMoreHorizontalAspectWithoutGrow() |
|
|
|
|
112
|
|
|
{ |
113
|
|
|
$source = new \Imagick('pattern:gray0'); |
114
|
|
|
$source->scaleImage(100, 50); |
115
|
|
|
|
116
|
|
|
$imagick = Image::resize($source, 400, 100); |
117
|
|
|
|
118
|
|
|
//making sure source didnt resize |
119
|
|
|
$this->assertSame(100, $source->getImageWidth()); |
120
|
|
|
$this->assertSame(50, $source->getImageHeight()); |
121
|
|
|
|
122
|
|
|
$this->assertSame(400, $imagick->getImageWidth()); |
123
|
|
|
$this->assertSame(100, $imagick->getImageHeight()); |
124
|
|
|
|
125
|
|
|
$whiteBarLeft = $imagick->getImagePixelColor(99, 49)->getHsl(); |
126
|
|
|
$whiteBarRight = $imagick->getImagePixelColor(300, 49)->getHsl(); |
127
|
|
|
|
128
|
|
|
$imageTop = $imagick->getImagePixelColor(200, 26)->getHsl(); |
129
|
|
|
$imageBottom = $imagick->getImagePixelColor(200, 74)->getHsl(); |
130
|
|
|
$imageLeft = $imagick->getImagePixelColor(151, 50)->getHsl(); |
131
|
|
|
$imageRight = $imagick->getImagePixelColor(249, 50)->getHsl(); |
132
|
|
|
|
133
|
|
|
$this->assertGreaterThan(0.9, $whiteBarLeft['luminosity']); |
134
|
|
|
$this->assertGreaterThan(0.9, $whiteBarRight['luminosity']); |
135
|
|
|
|
136
|
|
|
$this->assertLessThan(0.1, $imageLeft['luminosity']); |
137
|
|
|
$this->assertLessThan(0.1, $imageRight['luminosity']); |
138
|
|
|
$this->assertLessThan(0.1, $imageTop['luminosity']); |
139
|
|
|
$this->assertLessThan(0.1, $imageBottom['luminosity']); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Upsize ratio 2.0 to 4.0 |
144
|
|
|
* |
145
|
|
|
* @test |
146
|
|
|
* @covers ::resize |
147
|
|
|
* @covers ::resizeMulti |
148
|
|
|
*/ |
149
|
|
View Code Duplication |
public function resizeUpsizeToMoreHorizontalAspectWithGrow() |
|
|
|
|
150
|
|
|
{ |
151
|
|
|
$source = new \Imagick('pattern:gray0'); |
152
|
|
|
$source->scaleImage(100, 50); |
153
|
|
|
|
154
|
|
|
$imagick = Image::resize($source, 400, 100, ['upsize' => true]); |
155
|
|
|
|
156
|
|
|
//making sure source didnt resize |
157
|
|
|
$this->assertSame(100, $source->getImageWidth()); |
158
|
|
|
$this->assertSame(50, $source->getImageHeight()); |
159
|
|
|
|
160
|
|
|
$this->assertSame(400, $imagick->getImageWidth()); |
161
|
|
|
$this->assertSame(100, $imagick->getImageHeight()); |
162
|
|
|
|
163
|
|
|
$whiteBarLeft = $imagick->getImagePixelColor(99, 49)->getHsl(); |
164
|
|
|
$whiteBarRight = $imagick->getImagePixelColor(300, 49)->getHsl(); |
165
|
|
|
|
166
|
|
|
$imageTop = $imagick->getImagePixelColor(249, 0)->getHsl(); |
167
|
|
|
$imageBottom = $imagick->getImagePixelColor(249, 99)->getHsl(); |
168
|
|
|
$imageLeft = $imagick->getImagePixelColor(100, 49)->getHsl(); |
169
|
|
|
$imageRight = $imagick->getImagePixelColor(299, 49)->getHsl(); |
170
|
|
|
|
171
|
|
|
$this->assertGreaterThan(0.9, $whiteBarLeft['luminosity']); |
172
|
|
|
$this->assertGreaterThan(0.9, $whiteBarRight['luminosity']); |
173
|
|
|
|
174
|
|
|
$this->assertLessThan(0.1, $imageLeft['luminosity']); |
175
|
|
|
$this->assertLessThan(0.1, $imageRight['luminosity']); |
176
|
|
|
$this->assertLessThan(0.1, $imageTop['luminosity']); |
177
|
|
|
$this->assertLessThan(0.1, $imageBottom['luminosity']); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* Upsize ratio 2.0 to 4.0 |
182
|
|
|
* |
183
|
|
|
* @test |
184
|
|
|
* @covers ::resize |
185
|
|
|
* @covers ::resizeMulti |
186
|
|
|
*/ |
187
|
|
View Code Duplication |
public function resizeUpsizeToMoreVerticalAspect() |
|
|
|
|
188
|
|
|
{ |
189
|
|
|
$source = new \Imagick('pattern:gray0'); |
190
|
|
|
$source->scaleImage(100, 50); |
191
|
|
|
|
192
|
|
|
$imagick = Image::resize($source, 200, 400); |
193
|
|
|
|
194
|
|
|
//making sure source didnt resize |
195
|
|
|
$this->assertSame(100, $source->getImageWidth()); |
196
|
|
|
$this->assertSame(50, $source->getImageHeight()); |
197
|
|
|
|
198
|
|
|
$this->assertSame(200, $imagick->getImageWidth()); |
199
|
|
|
$this->assertSame(400, $imagick->getImageHeight()); |
200
|
|
|
|
201
|
|
|
$whiteBarLeft = $imagick->getImagePixelColor(49, 200)->getHsl(); |
202
|
|
|
$whiteBarRight = $imagick->getImagePixelColor(151, 200)->getHsl(); |
203
|
|
|
|
204
|
|
|
$imageTop = $imagick->getImagePixelColor(100, 176)->getHsl(); |
205
|
|
|
$imageBottom = $imagick->getImagePixelColor(100, 224)->getHsl(); |
206
|
|
|
$imageLeft = $imagick->getImagePixelColor(51, 200)->getHsl(); |
207
|
|
|
$imageRight = $imagick->getImagePixelColor(149, 200)->getHsl(); |
208
|
|
|
|
209
|
|
|
$this->assertGreaterThan(0.9, $whiteBarLeft['luminosity']); |
210
|
|
|
$this->assertGreaterThan(0.9, $whiteBarRight['luminosity']); |
211
|
|
|
|
212
|
|
|
$this->assertLessThan(0.1, $imageLeft['luminosity']); |
213
|
|
|
$this->assertLessThan(0.1, $imageRight['luminosity']); |
214
|
|
|
$this->assertLessThan(0.1, $imageTop['luminosity']); |
215
|
|
|
$this->assertLessThan(0.1, $imageBottom['luminosity']); |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
/** |
219
|
|
|
* @test |
220
|
|
|
* @covers ::resize |
221
|
|
|
* @covers ::resizeMulti |
222
|
|
|
*/ |
223
|
|
|
public function resizeWithUpsizeAndBestFit() |
224
|
|
|
{ |
225
|
|
|
$source = new \Imagick('pattern:gray0'); |
226
|
|
|
$source->scaleImage(85, 45); |
227
|
|
|
$notBestFit = Image::resize($source, 300, 300, ['upsize' => true, 'bestfit' => false]); |
228
|
|
|
$this->assertSame('srgb(0,0,0)', $notBestFit->getImagePixelColor(299, 100)->getColorAsString()); |
229
|
|
|
$bestFit = Image::resize($source, 300, 300, ['upsize' => true, 'bestfit' => true]); |
230
|
|
|
$this->assertSame('srgb(255,255,255)', $bestFit->getImagePixelColor(299, 100)->getColorAsString()); |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
/** |
234
|
|
|
* @test |
235
|
|
|
* @covers ::resize |
236
|
|
|
* @covers ::resizeMulti |
237
|
|
|
*/ |
238
|
|
|
public function resizeWithBurredBackground() |
239
|
|
|
{ |
240
|
|
|
$source = new \Imagick(); |
241
|
|
|
$source->readImage(__DIR__ . '/_files/portrait.jpg'); |
242
|
|
|
$actual = Image::resize($source, 1024, 768, ['upsize' => true, 'bestfit' => false, 'blurBackground' => true]); |
243
|
|
|
|
244
|
|
|
$expected = new \Imagick(); |
245
|
|
|
$expected->readImage(__DIR__ . '/_files/blur.jpg'); |
246
|
|
|
$comparison = $expected->compareImages($actual, \Imagick::METRIC_UNDEFINED); |
247
|
|
|
$this->assertGreaterThanOrEqual(.999, $comparison[1]); |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
/** |
251
|
|
|
* @test |
252
|
|
|
* @covers ::resize |
253
|
|
|
* @covers ::resizeMulti |
254
|
|
|
*/ |
255
|
|
|
public function resizeWithBurredBackgroundWithCustomBlurValue() |
256
|
|
|
{ |
257
|
|
|
$source = new \Imagick(); |
258
|
|
|
$source->readImage(__DIR__ . '/_files/portrait.jpg'); |
259
|
|
|
$options = ['upsize' => true, 'bestfit' => false, 'blurBackground' => true, 'blurValue' => 30.0]; |
260
|
|
|
$actual = Image::resize($source, 1024, 768, $options); |
261
|
|
|
$actual->writeImage(__DIR__ . '/_files/blur-30.jpg'); |
262
|
|
|
|
263
|
|
|
$expected = new \Imagick(); |
264
|
|
|
$expected->readImage(__DIR__ . '/_files/blur-30.jpg'); |
265
|
|
|
$comparison = $expected->compareImages($actual, \Imagick::METRIC_UNDEFINED); |
266
|
|
|
$this->assertGreaterThanOrEqual(.999, $comparison[1]); |
267
|
|
|
} |
268
|
|
|
|
269
|
|
|
/** |
270
|
|
|
* @test |
271
|
|
|
* @covers ::resize |
272
|
|
|
* @covers ::resizeMulti |
273
|
|
|
* @expectedException \InvalidArgumentException |
274
|
|
|
* @expectedExceptionMessage a $boxSizes width was not between 0 and $options["maxWidth"] |
275
|
|
|
*/ |
276
|
|
|
public function resizeZeroBoxWidth() |
277
|
|
|
{ |
278
|
|
|
Image::resize(new \Imagick(), 0, 10); |
279
|
|
|
} |
280
|
|
|
|
281
|
|
|
/** |
282
|
|
|
* @test |
283
|
|
|
* @covers ::resize |
284
|
|
|
* @covers ::resizeMulti |
285
|
|
|
* @expectedException \InvalidArgumentException |
286
|
|
|
* @expectedExceptionMessage a $boxSizes width was not between 0 and $options["maxWidth"] |
287
|
|
|
*/ |
288
|
|
|
public function resizeLargeBoxWidth() |
289
|
|
|
{ |
290
|
|
|
Image::resize(new \Imagick(), 10001, 10, ['maxWidth' => 10000]); |
291
|
|
|
} |
292
|
|
|
|
293
|
|
|
/** |
294
|
|
|
* @test |
295
|
|
|
* @covers ::resize |
296
|
|
|
* @covers ::resizeMulti |
297
|
|
|
* @expectedException \InvalidArgumentException |
298
|
|
|
* @expectedExceptionMessage a $boxSizes height was not between 0 and $options["maxHeight"] |
299
|
|
|
*/ |
300
|
|
|
public function resizeZeroBoxHeight() |
301
|
|
|
{ |
302
|
|
|
Image::resize(new \Imagick(), 10, 0); |
303
|
|
|
} |
304
|
|
|
|
305
|
|
|
/** |
306
|
|
|
* @test |
307
|
|
|
* @covers ::resize |
308
|
|
|
* @covers ::resizeMulti |
309
|
|
|
* @expectedException \InvalidArgumentException |
310
|
|
|
* @expectedExceptionMessage a $boxSizes height was not between 0 and $options["maxHeight"] |
311
|
|
|
*/ |
312
|
|
|
public function resizeLargeBoxHeight() |
313
|
|
|
{ |
314
|
|
|
Image::resize(new \Imagick(), 10, 10001, ['maxHeight' => 10000]); |
315
|
|
|
} |
316
|
|
|
|
317
|
|
|
/** |
318
|
|
|
* @test |
319
|
|
|
* @covers ::resize |
320
|
|
|
* @covers ::resizeMulti |
321
|
|
|
* @expectedException \InvalidArgumentException |
322
|
|
|
* @expectedExceptionMessage $options["color"] was not a string |
323
|
|
|
*/ |
324
|
|
|
public function resizeNonStringColor() |
325
|
|
|
{ |
326
|
|
|
Image::resize(new \Imagick(), 10, 10, ['color' => 0]); |
327
|
|
|
} |
328
|
|
|
|
329
|
|
|
/** |
330
|
|
|
* @test |
331
|
|
|
* @covers ::resize |
332
|
|
|
* @covers ::resizeMulti |
333
|
|
|
* @expectedException \InvalidArgumentException |
334
|
|
|
* @expectedExceptionMessage $options["maxWidth"] was not an int |
335
|
|
|
*/ |
336
|
|
|
public function resizeonIntMaxWidth() |
337
|
|
|
{ |
338
|
|
|
Image::resize(new \Imagick(), 10, 10, ['maxWidth' => 'not int']); |
339
|
|
|
} |
340
|
|
|
|
341
|
|
|
/** |
342
|
|
|
* @test |
343
|
|
|
* @covers ::resize |
344
|
|
|
* @covers ::resizeMulti |
345
|
|
|
* @expectedException \InvalidArgumentException |
346
|
|
|
* @expectedExceptionMessage $options["maxHeight"] was not an int |
347
|
|
|
*/ |
348
|
|
|
public function resizeNonIntMaxHeight() |
349
|
|
|
{ |
350
|
|
|
Image::resize(new \Imagick(), 10, 10, ['maxHeight' => 'not int']); |
351
|
|
|
} |
352
|
|
|
|
353
|
|
|
/** |
354
|
|
|
* @test |
355
|
|
|
* @covers ::resize |
356
|
|
|
* @covers ::resizeMulti |
357
|
|
|
* @expectedException \InvalidArgumentException |
358
|
|
|
* @expectedExceptionMessage $options["upsize"] was not a bool |
359
|
|
|
*/ |
360
|
|
|
public function resizeNonBoolUpsize() |
361
|
|
|
{ |
362
|
|
|
Image::resize(new \Imagick(), 10, 10, ['upsize' => 'not bool']); |
363
|
|
|
} |
364
|
|
|
|
365
|
|
|
/** |
366
|
|
|
* @test |
367
|
|
|
* @covers ::resize |
368
|
|
|
* @covers ::resizeMulti |
369
|
|
|
* @expectedException \InvalidArgumentException |
370
|
|
|
* @expectedExceptionMessage $options["bestfit"] was not a bool |
371
|
|
|
*/ |
372
|
|
|
public function resizeNonBoolBestFit() |
373
|
|
|
{ |
374
|
|
|
Image::resize(new \Imagick(), 10, 10, ['bestfit' => 'not bool']); |
375
|
|
|
} |
376
|
|
|
|
377
|
|
|
/** |
378
|
|
|
* Verify images are rotated according to EXIF header |
379
|
|
|
* @test |
380
|
|
|
* @covers ::resize |
381
|
|
|
* @covers ::resizeMulti |
382
|
|
|
*/ |
383
|
|
|
public function resizeOrientation() |
384
|
|
|
{ |
385
|
|
|
$files = [ |
386
|
|
|
"{$this->sourceFilesDir}/bottom-right.jpg", |
387
|
|
|
"{$this->sourceFilesDir}/left-bottom.jpg", |
388
|
|
|
"{$this->sourceFilesDir}/right-top.jpg", |
389
|
|
|
"{$this->sourceFilesDir}/top-left.jpg", |
390
|
|
|
]; |
391
|
|
|
|
392
|
|
|
$imageResults = []; |
393
|
|
|
|
394
|
|
|
foreach ($files as $file) { |
395
|
|
|
$source = new \Imagick($file); |
396
|
|
|
$imageWidth = $source->getimagewidth(); |
397
|
|
|
$imageHeight = $source->getimageheight(); |
398
|
|
|
$imageResults[] = Image::resize($source, $imageWidth, $imageHeight, []); |
399
|
|
|
} |
400
|
|
|
|
401
|
|
|
$this->assertSame( |
402
|
|
|
['r' => 254, 'g' => 0, 'b' => 0, 'a' => 1], |
403
|
|
|
$imageResults[0]->getImagePixelColor(0, 0)->getColor() |
404
|
|
|
); |
405
|
|
|
$this->assertSame( |
406
|
|
|
['r' => 0, 'g' => 0, 'b' => 0, 'a' => 1], |
407
|
|
|
$imageResults[1]->getImagePixelColor(0, 0)->getColor() |
408
|
|
|
); |
409
|
|
|
$this->assertSame( |
410
|
|
|
['r' => 0, 'g' => 255, 'b' => 1, 'a' => 1], |
411
|
|
|
$imageResults[2]->getImagePixelColor(0, 0)->getColor() |
412
|
|
|
); |
413
|
|
|
$this->assertSame( |
414
|
|
|
['r' => 0, 'g' => 0, 'b' => 254, 'a' => 1], |
415
|
|
|
$imageResults[3]->getImagePixelColor(0, 0)->getColor() |
416
|
|
|
); |
417
|
|
|
} |
418
|
|
|
|
419
|
|
|
/** |
420
|
|
|
* Downsize ratio 2.0 to 0.25 and 2.0 to 4.0 |
421
|
|
|
* |
422
|
|
|
* @test |
423
|
|
|
* @covers ::resizeMulti |
424
|
|
|
*/ |
425
|
|
|
public function resizeMultiDownsizeToMoreVerticalAndMoreHorizontalAspect() |
426
|
|
|
{ |
427
|
|
|
$source = new \Imagick('pattern:gray0'); |
428
|
|
|
$source->scaleImage(100, 50); |
429
|
|
|
|
430
|
|
|
$results = Image::resizeMulti($source, [['width' => 10, 'height' => 40], ['width' => 40, 'height' => 10]]); |
431
|
|
|
$imagickOne = $results[0]; |
432
|
|
|
$imagickTwo = $results[1]; |
433
|
|
|
|
434
|
|
|
//making sure source didnt resize |
435
|
|
|
$this->assertSame(100, $source->getImageWidth()); |
436
|
|
|
$this->assertSame(50, $source->getImageHeight()); |
437
|
|
|
|
438
|
|
|
//check $imagick1 |
439
|
|
|
|
440
|
|
|
$this->assertSame(10, $imagickOne->getImageWidth()); |
441
|
|
|
$this->assertSame(40, $imagickOne->getImageHeight()); |
442
|
|
|
|
443
|
|
|
$oneWhiteBarTop = $imagickOne->getImagePixelColor(4, 16)->getHsl(); |
444
|
|
|
$oneWhiteBarBottom = $imagickOne->getImagePixelColor(4, 22)->getHsl(); |
445
|
|
|
|
446
|
|
|
$oneImageLeft = $imagickOne->getImagePixelColor(0, 19)->getHsl(); |
447
|
|
|
$oneImageRight = $imagickOne->getImagePixelColor(9, 19)->getHsl(); |
448
|
|
|
$oneImageTop = $imagickOne->getImagePixelColor(4, 17)->getHsl(); |
449
|
|
|
$oneImageBottom = $imagickOne->getImagePixelColor(4, 21)->getHsl(); |
450
|
|
|
|
451
|
|
|
$this->assertGreaterThan(0.9, $oneWhiteBarTop['luminosity']); |
452
|
|
|
$this->assertGreaterThan(0.9, $oneWhiteBarBottom['luminosity']); |
453
|
|
|
|
454
|
|
|
$this->assertLessThan(0.1, $oneImageLeft['luminosity']); |
455
|
|
|
$this->assertLessThan(0.1, $oneImageRight['luminosity']); |
456
|
|
|
$this->assertLessThan(0.1, $oneImageTop['luminosity']); |
457
|
|
|
$this->assertLessThan(0.1, $oneImageBottom['luminosity']); |
458
|
|
|
|
459
|
|
|
//check $imagick2 |
460
|
|
|
|
461
|
|
|
$this->assertSame(40, $imagickTwo->getImageWidth()); |
462
|
|
|
$this->assertSame(10, $imagickTwo->getImageHeight()); |
463
|
|
|
|
464
|
|
|
$twoWhiteBarLeft = $imagickTwo->getImagePixelColor(9, 4)->getHsl(); |
465
|
|
|
$twoWhiteBarRight = $imagickTwo->getImagePixelColor(30, 4)->getHsl(); |
466
|
|
|
|
467
|
|
|
$twoImageLeft = $imagickTwo->getImagePixelColor(10, 4)->getHsl(); |
468
|
|
|
$twoImageRight = $imagickTwo->getImagePixelColor(29, 4)->getHsl(); |
469
|
|
|
$twoImageTop = $imagickTwo->getImagePixelColor(19, 0)->getHsl(); |
470
|
|
|
$twoImageBottom = $imagickTwo->getImagePixelColor(19, 9)->getHsl(); |
471
|
|
|
|
472
|
|
|
$this->assertGreaterThan(0.9, $twoWhiteBarLeft['luminosity']); |
473
|
|
|
$this->assertGreaterThan(0.9, $twoWhiteBarRight['luminosity']); |
474
|
|
|
|
475
|
|
|
$this->assertLessThan(0.1, $twoImageLeft['luminosity']); |
476
|
|
|
$this->assertLessThan(0.1, $twoImageRight['luminosity']); |
477
|
|
|
$this->assertLessThan(0.1, $twoImageTop['luminosity']); |
478
|
|
|
$this->assertLessThan(0.1, $twoImageBottom['luminosity']); |
479
|
|
|
} |
480
|
|
|
|
481
|
|
|
/** |
482
|
|
|
* @test |
483
|
|
|
* @covers ::resizeMulti |
484
|
|
|
*/ |
485
|
|
|
public function resizeMultiPerformance() |
486
|
|
|
{ |
487
|
|
|
$source = new \Imagick('pattern:gray0'); |
488
|
|
|
$source->scaleImage(2000, 500); |
489
|
|
|
|
490
|
|
|
$count = 10; |
491
|
|
|
|
492
|
|
|
$beforeSingle = microtime(true); |
493
|
|
|
for ($i = 0; $i < $count; ++$i) { |
494
|
|
|
Image::resize($source, 1100, 400); |
495
|
|
|
Image::resize($source, 100, 400); |
496
|
|
|
Image::resize($source, 10, 40); |
497
|
|
|
} |
498
|
|
|
|
499
|
|
|
$singleTime = microtime(true) - $beforeSingle; |
500
|
|
|
|
501
|
|
|
$beforeMulti = microtime(true); |
502
|
|
|
for ($i = 0; $i < $count; ++$i) { |
503
|
|
|
Image::resizeMulti( |
504
|
|
|
$source, |
505
|
|
|
[['width' => 1100, 'height' => 400], ['width' => 100, 'height' => 400], ['width' => 10, 'height' => 40]] |
506
|
|
|
); |
507
|
|
|
} |
508
|
|
|
|
509
|
|
|
$multiTime = microtime(true) - $beforeMulti; |
510
|
|
|
|
511
|
|
|
$this->assertLessThan($singleTime, $multiTime * 0.75); |
512
|
|
|
} |
513
|
|
|
|
514
|
|
|
/** |
515
|
|
|
* @test |
516
|
|
|
* @covers ::resizeMulti |
517
|
|
|
* @expectedException \InvalidArgumentException |
518
|
|
|
* @expectedExceptionMessage a width in a $boxSizes value was not an int |
519
|
|
|
*/ |
520
|
|
|
public function resizeMultiNonIntWidth() |
521
|
|
|
{ |
522
|
|
|
Image::resizeMulti(new \Imagick(), [['width' => true, 'height' => 10]]); |
523
|
|
|
} |
524
|
|
|
|
525
|
|
|
/** |
526
|
|
|
* @test |
527
|
|
|
* @covers ::resizeMulti |
528
|
|
|
* @expectedException \InvalidArgumentException |
529
|
|
|
* @expectedExceptionMessage a height in a $boxSizes value was not an int |
530
|
|
|
*/ |
531
|
|
|
public function resizeMultiNonIntHeight() |
532
|
|
|
{ |
533
|
|
|
Image::resizeMulti(new \Imagick(), [['width' => 10, 'height' => true]]); |
534
|
|
|
} |
535
|
|
|
|
536
|
|
|
/** |
537
|
|
|
* @test |
538
|
|
|
* @covers ::write |
539
|
|
|
*/ |
540
|
|
|
public function write() |
541
|
|
|
{ |
542
|
|
|
$destPath = "{$this->tempDir}/dest.jpeg"; |
543
|
|
|
|
544
|
|
|
$source = new \Imagick("{$this->sourceFilesDir}/exif.jpg"); |
545
|
|
|
$source->setImageFormat('png'); |
546
|
|
|
|
547
|
|
|
Image::write( |
548
|
|
|
$source, |
549
|
|
|
$destPath, |
550
|
|
|
['format' => 'jpeg', 'directoryMode' => 0775, 'fileMode' => 0776, 'stripHeaders' => true] |
551
|
|
|
); |
552
|
|
|
|
553
|
|
|
$destImage = new \Imagick($destPath); |
554
|
|
|
|
555
|
|
|
$this->assertSame(0, count($destImage->getImageProperties('exif:*'))); |
556
|
|
|
$this->assertSame('JPEG', $destImage->getImageFormat()); |
557
|
|
|
|
558
|
|
|
$directoryPermissions = substr(sprintf('%o', fileperms($this->tempDir)), -4); |
559
|
|
|
$filePermissions = substr(sprintf('%o', fileperms($destPath)), -4); |
560
|
|
|
|
561
|
|
|
$this->assertSame('0775', $directoryPermissions); |
562
|
|
|
$this->assertSame('0776', $filePermissions); |
563
|
|
|
} |
564
|
|
|
|
565
|
|
|
/** |
566
|
|
|
* @test |
567
|
|
|
* @covers ::write |
568
|
|
|
* @expectedException \InvalidArgumentException |
569
|
|
|
* @expectedExceptionMessage $options["directoryMode"] was not an int |
570
|
|
|
*/ |
571
|
|
|
public function writeNonIntDirectoryMode() |
572
|
|
|
{ |
573
|
|
|
Image::write(new \Imagick(), 'not under test', ['directoryMode' => 'not int']); |
574
|
|
|
} |
575
|
|
|
|
576
|
|
|
/** |
577
|
|
|
* @test |
578
|
|
|
* @covers ::write |
579
|
|
|
* @expectedException \InvalidArgumentException |
580
|
|
|
* @expectedExceptionMessage $options["fileMode"] was not an int |
581
|
|
|
*/ |
582
|
|
|
public function writeNonIntFileMode() |
583
|
|
|
{ |
584
|
|
|
Image::write(new \Imagick(), 'not under test', ['fileMode' => 'not int']); |
585
|
|
|
} |
586
|
|
|
|
587
|
|
|
/** |
588
|
|
|
* @test |
589
|
|
|
* @covers ::write |
590
|
|
|
* @expectedException \InvalidArgumentException |
591
|
|
|
* @expectedExceptionMessage $options["format"] was not a string |
592
|
|
|
*/ |
593
|
|
|
public function writeNonStringFormat() |
594
|
|
|
{ |
595
|
|
|
Image::write(new \Imagick(), 'not under test', ['format' => true]); |
596
|
|
|
} |
597
|
|
|
|
598
|
|
|
/** |
599
|
|
|
* @test |
600
|
|
|
* @covers ::write |
601
|
|
|
* @expectedException \InvalidArgumentException |
602
|
|
|
* @expectedExceptionMessage $options["stripHeaders"] was not a bool |
603
|
|
|
*/ |
604
|
|
|
public function writeNonBoolStripHeaders() |
605
|
|
|
{ |
606
|
|
|
Image::write(new \Imagick(), 'not under test', ['stripHeaders' => 'not bool']); |
607
|
|
|
} |
608
|
|
|
|
609
|
|
|
/** |
610
|
|
|
* Verify that stripHeaders strips exif headers. |
611
|
|
|
* |
612
|
|
|
* @test |
613
|
|
|
* @covers ::stripHeaders |
614
|
|
|
*/ |
615
|
|
|
public function stripHeaders() |
616
|
|
|
{ |
617
|
|
|
$path = "{$this->tempDir}/stripHeaders.jpg"; |
618
|
|
|
|
619
|
|
|
mkdir($this->tempDir); |
620
|
|
|
copy("{$this->sourceFilesDir}/exif.jpg", $path); |
621
|
|
|
|
622
|
|
|
Image::stripHeaders($path); |
623
|
|
|
|
624
|
|
|
$imagick = new \Imagick($path); |
625
|
|
|
$this->assertSame(0, count($imagick->getImageProperties('exif:*'))); |
626
|
|
|
} |
627
|
|
|
|
628
|
|
|
/** |
629
|
|
|
* Verify that stripHeaders fails with a missing image. |
630
|
|
|
* |
631
|
|
|
* @test |
632
|
|
|
* @covers ::stripHeaders |
633
|
|
|
* @expectedException \ImagickException |
634
|
|
|
*/ |
635
|
|
|
public function stripHeadersMissingImage() |
636
|
|
|
{ |
637
|
|
|
Image::stripHeaders("{$this->tempDir}/doesnotexist.jpg"); |
638
|
|
|
} |
639
|
|
|
} |
640
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.