Passed
Push — master ( 8e0f36...cdb6de )
by Aimeos
06:20
created

ImagickTest::testSavePng()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

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