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