summaryrefslogtreecommitdiffstats
path: root/tests/lib/image.php
blob: 131a9d86f3eaef89200d003bdbff303581cd1eb1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
<?php
/**
 * Copyright (c) 2013 Christopher Schäpers <christopher@schaepers.it>
 * This file is licensed under the Affero General Public License version 3 or
 * later.
 * See the COPYING-README file.
 */

class Test_Image extends PHPUnit_Framework_TestCase {
	public static function tearDownAfterClass() {
		@unlink(OC::$SERVERROOT.'/tests/data/testimage2.png');
		@unlink(OC::$SERVERROOT.'/tests/data/testimage2.jpg');
	}

	public function testGetMimeTypeForFile() {
		$mimetype = \OC_Image::getMimeTypeForFile(OC::$SERVERROOT.'/tests/data/testimage.png');
		$this->assertEquals('image/png', $mimetype);

		$mimetype = \OC_Image::getMimeTypeForFile(OC::$SERVERROOT.'/tests/data/testimage.jpg');
		$this->assertEquals('image/jpeg', $mimetype);

		$mimetype = \OC_Image::getMimeTypeForFile(OC::$SERVERROOT.'/tests/data/testimage.gif');
		$this->assertEquals('image/gif', $mimetype);

		$mimetype = \OC_Image::getMimeTypeForFile(null);
		$this->assertEquals('', $mimetype);
	}

	public function testConstructDestruct() {
		$img = new \OC_Image(OC::$SERVERROOT.'/tests/data/testimage.png');
		$this->assertInstanceOf('\OC_Image', $img);
		unset($img);

		$imgcreate = imagecreatefromjpeg(OC::$SERVERROOT.'/tests/data/testimage.jpg');
		$img = new \OC_Image($imgcreate);
		$this->assertInstanceOf('\OC_Image', $img);
		unset($img);

		$base64 = base64_encode(file_get_contents(OC::$SERVERROOT.'/tests/data/testimage.gif'));
		$img = new \OC_Image($base64);
		$this->assertInstanceOf('\OC_Image', $img);
		unset($img);

		$img = new \OC_Image(null);
		$this->assertInstanceOf('\OC_Image', $img);
		unset($img);
	}

	public function testValid() {
		$img = new \OC_Image(OC::$SERVERROOT.'/tests/data/testimage.png');
		$this->assertTrue($img->valid());

		$text = base64_encode("Lorem ipsum dolor sir amet …");
		$img = new \OC_Image($text);
		$this->assertFalse($img->valid());

		$img = new \OC_Image(null);
		$this->assertFalse($img->valid());
	}

	public function testMimeType() {
		$img = new \OC_Image(OC::$SERVERROOT.'/tests/data/testimage.png');
		$this->assertEquals('image/png', $img->mimeType());

		$img = new \OC_Image(file_get_contents(OC::$SERVERROOT.'/tests/data/testimage.jpg'));
		$this->assertEquals('image/jpeg', $img->mimeType());

		$img = new \OC_Image(base64_encode(file_get_contents(OC::$SERVERROOT.'/tests/data/testimage.gif')));
		$this->assertEquals('image/gif', $img->mimeType());

		$img = new \OC_Image(null);
		$this->assertEquals('', $img->mimeType());
	}

	public function testWidth() {
		$img = new \OC_Image(OC::$SERVERROOT.'/tests/data/testimage.png');
		$this->assertEquals(128, $img->width());

		$img = new \OC_Image(file_get_contents(OC::$SERVERROOT.'/tests/data/testimage.jpg'));
		$this->assertEquals(1680, $img->width());

		$img = new \OC_Image(base64_encode(file_get_contents(OC::$SERVERROOT.'/tests/data/testimage.gif')));
		$this->assertEquals(64, $img->width());

		$img = new \OC_Image(null);
		$this->assertEquals(-1, $img->width());
	}

	public function testHeight() {
		$img = new \OC_Image(OC::$SERVERROOT.'/tests/data/testimage.png');
		$this->assertEquals(128, $img->height());

		$img = new \OC_Image(file_get_contents(OC::$SERVERROOT.'/tests/data/testimage.jpg'));
		$this->assertEquals(1050, $img->height());

		$img = new \OC_Image(base64_encode(file_get_contents(OC::$SERVERROOT.'/tests/data/testimage.gif')));
		$this->assertEquals(64, $img->height());

		$img = new \OC_Image(null);
		$this->assertEquals(-1, $img->height());
	}

	public function testSave() {
		$img = new \OC_Image(OC::$SERVERROOT.'/tests/data/testimage.png');
		$img->resize(16);
		$img->save(OC::$SERVERROOT.'/tests/data/testimage2.png');
		$this->assertEquals(file_get_contents(OC::$SERVERROOT.'/tests/data/testimage2.png'), $img->data());

		$img = new \OC_Image(OC::$SERVERROOT.'/tests/data/testimage.jpg');
		$img->resize(128);
		$img->save(OC::$SERVERROOT.'/tests/data/testimage2.jpg');
		$this->assertEquals(file_get_contents(OC::$SERVERROOT.'/tests/data/testimage2.jpg'), $img->data());
	}

	public function testData() {
		$img = new \OC_Image(OC::$SERVERROOT.'/tests/data/testimage.png');
		$raw = imagecreatefromstring(file_get_contents(OC::$SERVERROOT.'/tests/data/testimage.png'));
		ob_start();
		imagepng($raw);
		$expected = ob_get_clean();
		$this->assertEquals($expected, $img->data());

		$img = new \OC_Image(OC::$SERVERROOT.'/tests/data/testimage.jpg');
		$raw = imagecreatefromstring(file_get_contents(OC::$SERVERROOT.'/tests/data/testimage.jpg'));
		ob_start();
		imagejpeg($raw);
		$expected = ob_get_clean();
		$this->assertEquals($expected, $img->data());

		$img = new \OC_Image(OC::$SERVERROOT.'/tests/data/testimage.gif');
		$raw = imagecreatefromstring(file_get_contents(OC::$SERVERROOT.'/tests/data/testimage.gif'));
		ob_start();
		imagegif($raw);
		$expected = ob_get_clean();
		$this->assertEquals($expected, $img->data());
	}

	/**
	 * @depends testData
	 */
	public function testToString() {
		$img = new \OC_Image(OC::$SERVERROOT.'/tests/data/testimage.png');
		$expected = base64_encode($img->data());
		$this->assertEquals($expected, (string)$img);

		$img = new \OC_Image(file_get_contents(OC::$SERVERROOT.'/tests/data/testimage.jpg'));
		$expected = base64_encode($img->data());
		$this->assertEquals($expected, (string)$img);

		$img = new \OC_Image(OC::$SERVERROOT.'/tests/data/testimage.gif');
		$expected = base64_encode($img->data());
		$this->assertEquals($expected, (string)$img);
	}

	public function testResize() {
		$img = new \OC_Image(OC::$SERVERROOT.'/tests/data/testimage.png');
		$this->assertTrue($img->resize(32));
		$this->assertEquals(32, $img->width());
		$this->assertEquals(32, $img->height());

		$img = new \OC_Image(file_get_contents(OC::$SERVERROOT.'/tests/data/testimage.jpg'));
		$this->assertTrue($img->resize(840));
		$this->assertEquals(840, $img->width());
		$this->assertEquals(525, $img->height());

		$img = new \OC_Image(base64_encode(file_get_contents(OC::$SERVERROOT.'/tests/data/testimage.gif')));
		$this->assertTrue($img->resize(100));
		$this->assertEquals(100, $img->width());
		$this->assertEquals(100, $img->height());
	}

	public function testPreciseResize() {
		$img = new \OC_Image(OC::$SERVERROOT.'/tests/data/testimage.png');
		$this->assertTrue($img->preciseResize(128, 512));
		$this->assertEquals(128, $img->width());
		$this->assertEquals(512, $img->height());

		$img = new \OC_Image(file_get_contents(OC::$SERVERROOT.'/tests/data/testimage.jpg'));
		$this->assertTrue($img->preciseResize(64, 840));
		$this->assertEquals(64, $img->width());
		$this->assertEquals(840, $img->height());

		$img = new \OC_Image(base64_encode(file_get_contents(OC::$SERVERROOT.'/tests/data/testimage.gif')));
		$this->assertTrue($img->preciseResize(1000, 1337));
		$this->assertEquals(1000, $img->width());
		$this->assertEquals(1337, $img->height());
	}

	public function testCenterCrop() {
		$img = new \OC_Image(OC::$SERVERROOT.'/tests/data/testimage.png');
		$img->centerCrop();
		$this->assertEquals(128, $img->width());
		$this->assertEquals(128, $img->height());

		$img = new \OC_Image(file_get_contents(OC::$SERVERROOT.'/tests/data/testimage.jpg'));
		$img->centerCrop();
		$this->assertEquals(1050, $img->width());
		$this->assertEquals(1050, $img->height());

		$img = new \OC_Image(base64_encode(file_get_contents(OC::$SERVERROOT.'/tests/data/testimage.gif')));
		$img->centerCrop(512);
		$this->assertEquals(512, $img->width());
		$this->assertEquals(512, $img->height());
	}

	public function testCrop() {
		$img = new \OC_Image(OC::$SERVERROOT.'/tests/data/testimage.png');
		$this->assertTrue($img->crop(0, 0, 50, 20));
		$this->assertEquals(50, $img->width());
		$this->assertEquals(20, $img->height());

		$img = new \OC_Image(file_get_contents(OC::$SERVERROOT.'/tests/data/testimage.jpg'));
		$this->assertTrue($img->crop(500, 700, 550, 300));
		$this->assertEquals(550, $img->width());
		$this->assertEquals(300, $img->height());

		$img = new \OC_Image(base64_encode(file_get_contents(OC::$SERVERROOT.'/tests/data/testimage.gif')));
		$this->assertTrue($img->crop(10, 10, 15, 15));
		$this->assertEquals(15, $img->width());
		$this->assertEquals(15, $img->height());
	}

	public function testFitIn() {
		$img = new \OC_Image(OC::$SERVERROOT.'/tests/data/testimage.png');
		$this->assertTrue($img->fitIn(200, 100));
		$this->assertEquals(100, $img->width());
		$this->assertEquals(100, $img->height());

		$img = new \OC_Image(file_get_contents(OC::$SERVERROOT.'/tests/data/testimage.jpg'));
		$this->assertTrue($img->fitIn(840, 840));
		$this->assertEquals(840, $img->width());
		$this->assertEquals(525, $img->height());

		$img = new \OC_Image(base64_encode(file_get_contents(OC::$SERVERROOT.'/tests/data/testimage.gif')));
		$this->assertTrue($img->fitIn(200, 250));
		$this->assertEquals(200, $img->width());
		$this->assertEquals(200, $img->height());
	}

	function convertDataProvider() {
		return array(
			array( 'image/gif'),
			array( 'image/jpeg'),
			array( 'image/png'),
		);
	}

	/**
	 * @dataProvider convertDataProvider
	 */
	public function testConvert($mimeType) {
		$img = new \OC_Image(OC::$SERVERROOT.'/tests/data/testimage.png');
		$tempFile = tempnam(sys_get_temp_dir(), 'img-test');

		$img->save($tempFile, $mimeType);
		$actualMimeType = \OC_Image::getMimeTypeForFile($tempFile);
		$this->assertEquals($mimeType, $actualMimeType);
	}
}