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