1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @license LGPLv3, https://opensource.org/licenses/LGPL-3.0 |
5
|
|
|
* @copyright Aimeos (aimeos.org), 2017-2023 |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
|
9
|
|
|
namespace Aimeos\MW\Media\Image; |
10
|
|
|
|
11
|
|
|
|
12
|
|
|
class ImagickTest extends \PHPUnit\Framework\TestCase |
13
|
|
|
{ |
14
|
|
|
public static function setUpBeforeClass() : void |
15
|
|
|
{ |
16
|
|
|
$dir = dirname( dirname( dirname( __DIR__ ) ) ) . '/tmp'; |
17
|
|
|
|
18
|
|
|
if( !is_dir( $dir ) ) { |
19
|
|
|
mkdir( $dir, 0755, true ); |
20
|
|
|
} |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
|
24
|
|
|
protected function setUp() : void |
25
|
|
|
{ |
26
|
|
|
if( !class_exists( \Imagick::class ) ) { |
27
|
|
|
$this->markTestSkipped( 'Imagick not available' ); |
28
|
|
|
} |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
|
32
|
|
|
public function testConstructGif() |
33
|
|
|
{ |
34
|
|
|
$ds = DIRECTORY_SEPARATOR; |
35
|
|
|
$content = file_get_contents( dirname( __DIR__ ) . $ds . '_testfiles' . $ds . 'image.gif' ); |
36
|
|
|
|
37
|
|
|
$media = new \Aimeos\MW\Media\Image\Imagick( $content, 'image/gif', [] ); |
38
|
|
|
|
39
|
|
|
$this->assertEquals( 'image/gif', $media->getMimetype() ); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
|
43
|
|
|
public function testConstructJpeg() |
44
|
|
|
{ |
45
|
|
|
$ds = DIRECTORY_SEPARATOR; |
46
|
|
|
$content = file_get_contents( dirname( __DIR__ ) . $ds . '_testfiles' . $ds . 'image.jpg' ); |
47
|
|
|
|
48
|
|
|
$media = new \Aimeos\MW\Media\Image\Imagick( $content, 'image/jpeg', [] ); |
49
|
|
|
|
50
|
|
|
$this->assertEquals( 'image/jpeg', $media->getMimetype() ); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
|
54
|
|
|
public function testConstructPng() |
55
|
|
|
{ |
56
|
|
|
$ds = DIRECTORY_SEPARATOR; |
57
|
|
|
$content = file_get_contents( dirname( __DIR__ ) . $ds . '_testfiles' . $ds . 'image.png' ); |
58
|
|
|
|
59
|
|
|
$media = new \Aimeos\MW\Media\Image\Imagick( $content, 'image/png', [] ); |
60
|
|
|
|
61
|
|
|
$this->assertEquals( 'image/png', $media->getMimetype() ); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
|
65
|
|
|
public function testConstructWebp() |
66
|
|
|
{ |
67
|
|
|
if( empty( \Aimeos\MW\Media\Image\Imagick::supports( 'image/webp' ) ) ) { |
68
|
|
|
$this->markTestSkipped( 'WEBP image format not supported' ); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
$ds = DIRECTORY_SEPARATOR; |
72
|
|
|
$content = file_get_contents( dirname( __DIR__ ) . $ds . '_testfiles' . $ds . 'image.webp' ); |
73
|
|
|
|
74
|
|
|
$media = new \Aimeos\MW\Media\Image\Imagick( $content, 'image/webp', [] ); |
75
|
|
|
|
76
|
|
|
$this->assertEquals( 'image/webp', $media->getMimetype() ); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
|
80
|
|
|
public function testConstructImageException() |
81
|
|
|
{ |
82
|
|
|
$ds = DIRECTORY_SEPARATOR; |
83
|
|
|
$content = file_get_contents( dirname( __DIR__ ) . $ds . '_testfiles' . $ds . 'application.txt' ); |
84
|
|
|
|
85
|
|
|
$this->expectException( \Aimeos\MW\Media\Exception::class ); |
86
|
|
|
new \Aimeos\MW\Media\Image\Imagick( $content, 'text/plain', [] ); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
|
90
|
|
|
public function testGetHeight() |
91
|
|
|
{ |
92
|
|
|
$ds = DIRECTORY_SEPARATOR; |
93
|
|
|
$content = file_get_contents( dirname( __DIR__ ) . $ds . '_testfiles' . $ds . 'image.jpg' ); |
94
|
|
|
|
95
|
|
|
$media = new \Aimeos\MW\Media\Image\Imagick( $content, 'image/jpeg', [] ); |
96
|
|
|
|
97
|
|
|
$this->assertEquals( 10, $media->getHeight() ); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
|
101
|
|
|
public function testGetWidth() |
102
|
|
|
{ |
103
|
|
|
$ds = DIRECTORY_SEPARATOR; |
104
|
|
|
$content = file_get_contents( dirname( __DIR__ ) . $ds . '_testfiles' . $ds . 'image.jpg' ); |
105
|
|
|
|
106
|
|
|
$media = new \Aimeos\MW\Media\Image\Imagick( $content, 'image/jpeg', [] ); |
107
|
|
|
|
108
|
|
|
$this->assertEquals( 10, $media->getWidth() ); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
|
112
|
|
|
public function testSaveGif() |
113
|
|
|
{ |
114
|
|
|
$ds = DIRECTORY_SEPARATOR; |
115
|
|
|
$content = file_get_contents( dirname( __DIR__ ) . $ds . '_testfiles' . $ds . 'image.png' ); |
116
|
|
|
$dest = dirname( dirname( dirname( __DIR__ ) ) ) . $ds . 'tmp' . $ds . 'media.gif'; |
117
|
|
|
|
118
|
|
|
$media = new \Aimeos\MW\Media\Image\Imagick( $content, 'image/png', [] ); |
119
|
|
|
$media->save( $dest, 'image/gif' ); |
120
|
|
|
|
121
|
|
|
$this->assertEquals( true, file_exists( $dest ) ); |
122
|
|
|
unlink( $dest ); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
|
126
|
|
|
public function testSaveGifInvalidDest() |
127
|
|
|
{ |
128
|
|
|
$ds = DIRECTORY_SEPARATOR; |
129
|
|
|
$content = file_get_contents( dirname( __DIR__ ) . $ds . '_testfiles' . $ds . 'image.png' ); |
130
|
|
|
$dest = __DIR__ . $ds . 'notexisting' . $ds . 'media.gif'; |
131
|
|
|
|
132
|
|
|
$media = new \Aimeos\MW\Media\Image\Imagick( $content, 'image/png', [] ); |
133
|
|
|
|
134
|
|
|
$this->expectException( \Aimeos\MW\Media\Exception::class ); |
135
|
|
|
$media->save( $dest, 'image/gif' ); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
|
139
|
|
|
public function testSaveJpeg() |
140
|
|
|
{ |
141
|
|
|
$ds = DIRECTORY_SEPARATOR; |
142
|
|
|
$content = file_get_contents( dirname( __DIR__ ) . $ds . '_testfiles' . $ds . 'image.png' ); |
143
|
|
|
$dest = dirname( dirname( dirname( __DIR__ ) ) ) . $ds . 'tmp' . $ds . 'media.jpg'; |
144
|
|
|
|
145
|
|
|
$media = new \Aimeos\MW\Media\Image\Imagick( $content, 'image/gif', [] ); |
146
|
|
|
$media->save( $dest, 'image/jpeg' ); |
147
|
|
|
|
148
|
|
|
$this->assertEquals( true, file_exists( $dest ) ); |
149
|
|
|
unlink( $dest ); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
|
153
|
|
|
public function testSaveJpegInvalidDest() |
154
|
|
|
{ |
155
|
|
|
$ds = DIRECTORY_SEPARATOR; |
156
|
|
|
$content = file_get_contents( dirname( __DIR__ ) . $ds . '_testfiles' . $ds . 'image.png' ); |
157
|
|
|
$dest = __DIR__ . $ds . 'notexisting' . $ds . 'media.jpg'; |
158
|
|
|
|
159
|
|
|
$media = new \Aimeos\MW\Media\Image\Imagick( $content, 'image/png', [] ); |
160
|
|
|
|
161
|
|
|
$this->expectException( \Aimeos\MW\Media\Exception::class ); |
162
|
|
|
$media->save( $dest, 'image/jpeg' ); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
|
166
|
|
|
public function testSavePng() |
167
|
|
|
{ |
168
|
|
|
$ds = DIRECTORY_SEPARATOR; |
169
|
|
|
$content = file_get_contents( dirname( __DIR__ ) . $ds . '_testfiles' . $ds . 'image.gif' ); |
170
|
|
|
$dest = dirname( dirname( dirname( __DIR__ ) ) ) . $ds . 'tmp' . $ds . 'media.png'; |
171
|
|
|
|
172
|
|
|
$media = new \Aimeos\MW\Media\Image\Imagick( $content, 'image/gif', [] ); |
173
|
|
|
$media->save( $dest, 'image/png' ); |
174
|
|
|
|
175
|
|
|
$this->assertEquals( true, file_exists( $dest ) ); |
176
|
|
|
unlink( $dest ); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
|
180
|
|
|
public function testSavePngInvalidDest() |
181
|
|
|
{ |
182
|
|
|
$ds = DIRECTORY_SEPARATOR; |
183
|
|
|
$content = file_get_contents( dirname( __DIR__ ) . $ds . '_testfiles' . $ds . 'image.gif' ); |
184
|
|
|
$dest = __DIR__ . $ds . 'notexisting' . $ds . 'media.png'; |
185
|
|
|
|
186
|
|
|
$media = new \Aimeos\MW\Media\Image\Imagick( $content, 'image/gif', [] ); |
187
|
|
|
|
188
|
|
|
$this->expectException( \Aimeos\MW\Media\Exception::class ); |
189
|
|
|
$media->save( $dest, 'image/png' ); |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
|
193
|
|
|
public function testSaveWebp() |
194
|
|
|
{ |
195
|
|
|
if( empty( \Aimeos\MW\Media\Image\Imagick::supports( 'image/webp' ) ) ) { |
196
|
|
|
$this->markTestSkipped( 'WEBP image format not supported' ); |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
$ds = DIRECTORY_SEPARATOR; |
200
|
|
|
$content = file_get_contents( dirname( __DIR__ ) . $ds . '_testfiles' . $ds . 'image.png' ); |
201
|
|
|
$dest = dirname( dirname( dirname( __DIR__ ) ) ) . $ds . 'tmp' . $ds . 'media.webp'; |
202
|
|
|
|
203
|
|
|
$media = new \Aimeos\MW\Media\Image\Imagick( $content, 'image/gif', [] ); |
204
|
|
|
$media->save( $dest, 'image/webp' ); |
205
|
|
|
|
206
|
|
|
$this->assertEquals( true, file_exists( $dest ) ); |
207
|
|
|
unlink( $dest ); |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
|
211
|
|
|
public function testSaveWebpInvalidDest() |
212
|
|
|
{ |
213
|
|
|
$ds = DIRECTORY_SEPARATOR; |
214
|
|
|
$content = file_get_contents( dirname( __DIR__ ) . $ds . '_testfiles' . $ds . 'image.png' ); |
215
|
|
|
$dest = __DIR__ . $ds . 'notexisting' . $ds . 'media.webp'; |
216
|
|
|
|
217
|
|
|
$media = new \Aimeos\MW\Media\Image\Imagick( $content, 'image/png', [] ); |
218
|
|
|
|
219
|
|
|
$this->expectException( \Aimeos\MW\Media\Exception::class ); |
220
|
|
|
$media->save( $dest, 'image/webp' ); |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
|
224
|
|
|
public function testSaveWatermarkNotfound() |
225
|
|
|
{ |
226
|
|
|
$ds = DIRECTORY_SEPARATOR; |
227
|
|
|
$basedir = dirname( __DIR__ ); |
228
|
|
|
|
229
|
|
|
$content = file_get_contents( $basedir . $ds . '_testfiles' . $ds . 'image.jpg' ); |
230
|
|
|
$options = ['image' => ['watermark' => $basedir . $ds . 'notexisting' . $ds . 'image.png']]; |
231
|
|
|
|
232
|
|
|
$this->expectException( \Aimeos\MW\Media\Exception::class ); |
233
|
|
|
$media = new \Aimeos\MW\Media\Image\Imagick( $content, 'image/png', $options ); |
|
|
|
|
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
|
237
|
|
|
public function testSaveWatermarkInvalid() |
238
|
|
|
{ |
239
|
|
|
$ds = DIRECTORY_SEPARATOR; |
240
|
|
|
$basedir = dirname( __DIR__ ); |
241
|
|
|
|
242
|
|
|
$content = file_get_contents( $basedir . $ds . '_testfiles' . $ds . 'image.jpg' ); |
243
|
|
|
$options = ['image' => ['watermark' => $basedir . $ds . '_testfiles' . $ds . 'application.txt']]; |
244
|
|
|
|
245
|
|
|
$this->expectException( \Aimeos\MW\Media\Exception::class ); |
246
|
|
|
$media = new \Aimeos\MW\Media\Image\Imagick( $content, 'image/png', $options ); |
|
|
|
|
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
|
250
|
|
|
public function testSaveWatermark() |
251
|
|
|
{ |
252
|
|
|
$ds = DIRECTORY_SEPARATOR; |
253
|
|
|
$basedir = dirname( __DIR__ ); |
254
|
|
|
|
255
|
|
|
$content = file_get_contents( $basedir . $ds . '_testfiles' . $ds . 'image.jpg' ); |
256
|
|
|
$options = ['image' => ['watermark' => $basedir . $ds . '_testfiles' . $ds . 'image.png']]; |
257
|
|
|
$dest = dirname( dirname( $basedir ) ) . $ds . 'tmp' . $ds . 'media.jpg'; |
258
|
|
|
|
259
|
|
|
$media = new \Aimeos\MW\Media\Image\Imagick( $content, 'image/png', $options ); |
260
|
|
|
$media->save( $dest, 'image/jpeg' ); |
261
|
|
|
|
262
|
|
|
$this->assertEquals( true, file_exists( $dest ) ); |
263
|
|
|
unlink( $dest ); |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
|
267
|
|
|
public function testScaleFit() |
268
|
|
|
{ |
269
|
|
|
$ds = DIRECTORY_SEPARATOR; |
270
|
|
|
$content = file_get_contents( dirname( __DIR__ ) . $ds . '_testfiles' . $ds . 'image.png' ); |
271
|
|
|
|
272
|
|
|
$media = new \Aimeos\MW\Media\Image\Imagick( $content, 'image/png', [] ); |
273
|
|
|
$info = getimagesizefromstring( $media->scale( 100, 100, 1 )->save( null, 'image/png' ) ); |
274
|
|
|
|
275
|
|
|
$this->assertEquals( 100, $info[0] ); |
276
|
|
|
$this->assertEquals( 100, $info[1] ); |
277
|
|
|
} |
278
|
|
|
|
279
|
|
|
|
280
|
|
|
public function testScaleDown() |
281
|
|
|
{ |
282
|
|
|
$ds = DIRECTORY_SEPARATOR; |
283
|
|
|
$content = file_get_contents( dirname( __DIR__ ) . $ds . '_testfiles' . $ds . 'image.png' ); |
284
|
|
|
|
285
|
|
|
$media = new \Aimeos\MW\Media\Image\Imagick( $content, 'image/png', [] ); |
286
|
|
|
$info = getimagesizefromstring( $media->scale( 5, 100 )->save( null, 'image/png' ) ); |
287
|
|
|
|
288
|
|
|
$this->assertEquals( 5, $info[0] ); |
289
|
|
|
$this->assertEquals( 5, $info[1] ); |
290
|
|
|
} |
291
|
|
|
|
292
|
|
|
|
293
|
|
|
public function testScaleInside() |
294
|
|
|
{ |
295
|
|
|
$ds = DIRECTORY_SEPARATOR; |
296
|
|
|
$content = file_get_contents( dirname( __DIR__ ) . $ds . '_testfiles' . $ds . 'image.png' ); |
297
|
|
|
|
298
|
|
|
$media = new \Aimeos\MW\Media\Image\Imagick( $content, 'image/png', [] ); |
299
|
|
|
$info = getimagesizefromstring( $media->scale( 100, 100 )->save( null, 'image/png' ) ); |
300
|
|
|
|
301
|
|
|
$this->assertEquals( 10, $info[0] ); |
302
|
|
|
$this->assertEquals( 10, $info[1] ); |
303
|
|
|
} |
304
|
|
|
|
305
|
|
|
|
306
|
|
|
public function testScaleFitCrop() |
307
|
|
|
{ |
308
|
|
|
$ds = DIRECTORY_SEPARATOR; |
309
|
|
|
$content = file_get_contents( dirname( __DIR__ ) . $ds . '_testfiles' . $ds . 'image.png' ); |
310
|
|
|
|
311
|
|
|
$media = new \Aimeos\MW\Media\Image\Imagick( $content, 'image/png', [] ); |
312
|
|
|
$info = getimagesizefromstring( $media->scale( 100, 50, 2 )->save( null, 'image/png' ) ); |
313
|
|
|
|
314
|
|
|
$this->assertEquals( 100, $info[0] ); |
315
|
|
|
$this->assertEquals( 50, $info[1] ); |
316
|
|
|
} |
317
|
|
|
|
318
|
|
|
|
319
|
|
|
public function testSupports() |
320
|
|
|
{ |
321
|
|
|
$this->assertGreaterThan( 0, count( \Aimeos\MW\Media\Image\Imagick::supports() ) ); |
322
|
|
|
$this->assertEquals( 1, count( \Aimeos\MW\Media\Image\Imagick::supports( 'image/jpeg' ) ) ); |
323
|
|
|
|
324
|
|
|
$result = \Aimeos\MW\Media\Image\Standard::supports( ['image/gif', 'image/png', 'image/jpeg'] ); |
325
|
|
|
$this->assertEquals( ['image/gif', 'image/png', 'image/jpeg'], $result ); |
326
|
|
|
} |
327
|
|
|
} |
328
|
|
|
|