1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
This file is part of WideImage. |
4
|
|
|
|
5
|
|
|
WideImage is free software; you can redistribute it and/or modify |
6
|
|
|
it under the terms of the GNU Lesser General Public License as published by |
7
|
|
|
the Free Software Foundation; either version 2.1 of the License, or |
8
|
|
|
(at your option) any later version. |
9
|
|
|
|
10
|
|
|
WideImage is distributed in the hope that it will be useful, |
11
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of |
12
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13
|
|
|
GNU Lesser General Public License for more details. |
14
|
|
|
|
15
|
|
|
You should have received a copy of the GNU Lesser General Public License |
16
|
|
|
along with WideImage; if not, write to the Free Software |
17
|
|
|
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
18
|
|
|
|
19
|
|
|
* @package Tests |
20
|
|
|
**/ |
21
|
|
|
|
22
|
|
|
namespace Test\WideImage; |
23
|
|
|
|
24
|
|
|
include_once __DIR__ . '/Mapper/FOO.php'; |
25
|
|
|
include_once __DIR__ . '/Mapper/FOO2.php'; |
26
|
|
|
|
27
|
|
|
use WideImage\WideImage; |
28
|
|
|
use WideImage\PaletteImage; |
29
|
|
|
use WideImage\TrueColorImage; |
30
|
|
|
use WideImage\Mapper\FOO; |
31
|
|
|
use WideImage\Mapper\FOO2; |
32
|
|
|
use Test\WideImage_TestCase; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @package Tests |
36
|
|
|
*/ |
37
|
|
|
class WideImageTest extends WideImage_TestCase |
38
|
|
|
{ |
39
|
|
|
protected $_FILES; |
40
|
|
|
|
41
|
|
|
public function setup() |
42
|
|
|
{ |
43
|
|
|
$this->_FILES = $_FILES; |
44
|
|
|
$_FILES = array(); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public function teardown() |
48
|
|
|
{ |
49
|
|
|
$_FILES = $this->_FILES; |
50
|
|
|
|
51
|
|
|
if (PHP_OS == 'WINNT') { |
52
|
|
|
chdir(IMG_PATH . "temp"); |
53
|
|
|
|
54
|
|
|
foreach (new \DirectoryIterator(IMG_PATH . "temp") as $file) { |
55
|
|
|
if (!$file->isDot()) { |
56
|
|
|
if ($file->isDir()) { |
57
|
|
|
exec("rd /S /Q {$file->getFilename()}\n"); |
58
|
|
|
} else { |
59
|
|
|
unlink($file->getFilename()); |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
} else { |
64
|
|
|
exec("rm -rf " . IMG_PATH . 'temp/*'); |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public function testLoadFromFile() |
69
|
|
|
{ |
70
|
|
|
$img = WideImage::load(IMG_PATH . '100x100-red-transparent.gif'); |
71
|
|
|
$this->assertTrue($img instanceof PaletteImage); |
72
|
|
|
$this->assertValidImage($img); |
73
|
|
|
$this->assertFalse($img->isTrueColor()); |
74
|
|
|
$this->assertEquals(100, $img->getWidth()); |
75
|
|
|
$this->assertEquals(100, $img->getHeight()); |
76
|
|
|
|
77
|
|
|
$img = WideImage::load(IMG_PATH . '100x100-rainbow.png'); |
78
|
|
|
$this->assertTrue($img instanceof TrueColorImage); |
79
|
|
|
$this->assertValidImage($img); |
80
|
|
|
$this->assertTrue($img->isTrueColor()); |
81
|
|
|
$this->assertEquals(100, $img->getWidth()); |
82
|
|
|
$this->assertEquals(100, $img->getHeight()); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
public function testLoadFromString() |
86
|
|
|
{ |
87
|
|
|
$img = WideImage::load(file_get_contents(IMG_PATH . '100x100-rainbow.png')); |
88
|
|
|
$this->assertTrue($img instanceof TrueColorImage); |
89
|
|
|
$this->assertValidImage($img); |
90
|
|
|
$this->assertTrue($img->isTrueColor()); |
91
|
|
|
$this->assertEquals(100, $img->getWidth()); |
92
|
|
|
$this->assertEquals(100, $img->getHeight()); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
public function testLoadFromHandle() |
96
|
|
|
{ |
97
|
|
|
$handle = imagecreatefrompng(IMG_PATH . '100x100-rainbow.png'); |
98
|
|
|
$img = WideImage::loadFromHandle($handle); |
|
|
|
|
99
|
|
|
$this->assertValidImage($img); |
100
|
|
|
$this->assertTrue($img->isTrueColor()); |
101
|
|
|
$this->assertSame($handle, $img->getHandle()); |
102
|
|
|
$this->assertEquals(100, $img->getWidth()); |
103
|
|
|
$this->assertEquals(100, $img->getHeight()); |
104
|
|
|
unset($img); |
105
|
|
|
$this->assertFalse(WideImage::isValidImageHandle($handle)); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
public function testLoadFromUpload() |
109
|
|
|
{ |
110
|
|
|
copy(IMG_PATH . '100x100-rainbow.png', IMG_PATH . 'temp' . DIRECTORY_SEPARATOR . 'upltmpimg'); |
111
|
|
|
$_FILES = array( |
112
|
|
|
'testupl' => array( |
113
|
|
|
'name' => '100x100-rainbow.png', |
114
|
|
|
'type' => 'image/png', |
115
|
|
|
'size' => strlen(file_get_contents(IMG_PATH . '100x100-rainbow.png')), |
116
|
|
|
'tmp_name' => IMG_PATH . 'temp' . DIRECTORY_SEPARATOR . 'upltmpimg', |
117
|
|
|
'error' => false, |
118
|
|
|
) |
119
|
|
|
); |
120
|
|
|
|
121
|
|
|
$img = WideImage::loadFromUpload('testupl'); |
122
|
|
|
$this->assertValidImage($img); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
public function testLoadFromMultipleUploads() |
126
|
|
|
{ |
127
|
|
|
copy(IMG_PATH . '100x100-rainbow.png', IMG_PATH . 'temp' . DIRECTORY_SEPARATOR . 'upltmpimg1'); |
128
|
|
|
copy(IMG_PATH . 'splat.tga', IMG_PATH . 'temp' . DIRECTORY_SEPARATOR . 'upltmpimg2'); |
129
|
|
|
$_FILES = array( |
130
|
|
|
'testupl' => array( |
131
|
|
|
'name' => array('100x100-rainbow.png', 'splat.tga'), |
132
|
|
|
'type' => array('image/png', 'image/tga'), |
133
|
|
|
'size' => array( |
134
|
|
|
strlen(file_get_contents(IMG_PATH . '100x100-rainbow.png')), |
135
|
|
|
strlen(file_get_contents(IMG_PATH . 'splat.tga')) |
136
|
|
|
), |
137
|
|
|
'tmp_name' => array( |
138
|
|
|
IMG_PATH . 'temp' . DIRECTORY_SEPARATOR . 'upltmpimg1', |
139
|
|
|
IMG_PATH . 'temp' . DIRECTORY_SEPARATOR . 'upltmpimg2' |
140
|
|
|
), |
141
|
|
|
'error' => array(false, false), |
142
|
|
|
) |
143
|
|
|
); |
144
|
|
|
|
145
|
|
|
$images = WideImage::loadFromUpload('testupl'); |
146
|
|
|
$this->assertInternalType("array", $images); |
147
|
|
|
$this->assertValidImage($images[0]); |
148
|
|
|
$this->assertValidImage($images[1]); |
149
|
|
|
|
150
|
|
|
$img = WideImage::loadFromUpload('testupl', 1); |
151
|
|
|
$this->assertValidImage($img); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
public function testLoadMagicalFromHandle() |
155
|
|
|
{ |
156
|
|
|
$img = WideImage::load(imagecreatefrompng(IMG_PATH . '100x100-rainbow.png')); |
157
|
|
|
$this->assertValidImage($img); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
|
161
|
|
|
public function testLoadMagicalFromBinaryString() |
162
|
|
|
{ |
163
|
|
|
$img = WideImage::load(file_get_contents(IMG_PATH . '100x100-rainbow.png')); |
164
|
|
|
$this->assertValidImage($img); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
public function testLoadMagicalFromFile() |
168
|
|
|
{ |
169
|
|
|
$img = WideImage::load(IMG_PATH . '100x100-rainbow.png'); |
170
|
|
|
$this->assertValidImage($img); |
171
|
|
|
copy(IMG_PATH . '100x100-rainbow.png', IMG_PATH . 'temp' . DIRECTORY_SEPARATOR . 'upltmpimg'); |
172
|
|
|
$_FILES = array( |
173
|
|
|
'testupl' => array( |
174
|
|
|
'name' => 'fgnl.bmp', |
175
|
|
|
'type' => 'image/bmp', |
176
|
|
|
'size' => strlen(file_get_contents(IMG_PATH . 'fgnl.bmp')), |
177
|
|
|
'tmp_name' => IMG_PATH . 'temp' . DIRECTORY_SEPARATOR . 'upltmpimg', |
178
|
|
|
'error' => false, |
179
|
|
|
) |
180
|
|
|
); |
181
|
|
|
$img = WideImage::load('testupl'); |
182
|
|
|
$this->assertValidImage($img); |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
public function testLoadFromStringWithCustomMapper() |
186
|
|
|
{ |
187
|
|
|
$img = WideImage::loadFromString(file_get_contents(IMG_PATH . 'splat.tga')); |
188
|
|
|
$this->assertValidImage($img); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
public function testLoadFromFileWithInvalidExtension() |
192
|
|
|
{ |
193
|
|
|
$img = WideImage::load(IMG_PATH . 'actually-a-png.jpg'); |
194
|
|
|
$this->assertValidImage($img); |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
public function testLoadFromFileWithInvalidExtensionWithCustomMapper() |
198
|
|
|
{ |
199
|
|
|
if (PHP_OS == 'WINNT') |
200
|
|
|
$this->markTestSkipped("For some reason, this test kills PHP my 32-bit Vista + PHP 5.3.1."); |
201
|
|
|
|
202
|
|
|
$img = WideImage::loadFromFile(IMG_PATH . 'fgnl-bmp.jpg'); |
203
|
|
|
$this->assertValidImage($img); |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* @expectedException WideImage\Exception\InvalidImageSourceException |
208
|
|
|
*/ |
209
|
|
|
public function testLoadFromStringEmpty() |
210
|
|
|
{ |
211
|
|
|
WideImage::loadFromString(''); |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
public function testLoadBMPMagicalFromUpload() |
215
|
|
|
{ |
216
|
|
|
copy(IMG_PATH . 'fgnl.bmp', IMG_PATH . 'temp' . DIRECTORY_SEPARATOR . 'upltmpimg'); |
217
|
|
|
$_FILES = array( |
218
|
|
|
'testupl' => array( |
219
|
|
|
'name' => 'fgnl.bmp', |
220
|
|
|
'type' => 'image/bmp', |
221
|
|
|
'size' => strlen(file_get_contents(IMG_PATH . 'fgnl.bmp')), |
222
|
|
|
'tmp_name' => IMG_PATH . 'temp' . DIRECTORY_SEPARATOR . 'upltmpimg', |
223
|
|
|
'error' => false, |
224
|
|
|
) |
225
|
|
|
); |
226
|
|
|
$img = WideImage::load('testupl'); |
227
|
|
|
$this->assertValidImage($img); |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
public function testMapperLoad() |
231
|
|
|
{ |
232
|
|
|
FOO::$handle = imagecreate(10, 10); |
233
|
|
|
$filename = IMG_PATH . 'image.foo'; |
234
|
|
|
WideImage::registerCustomMapper(__NAMESPACE__ . '\\FOO', 'image/foo', 'foo'); |
235
|
|
|
$img = WideImage::load($filename); |
|
|
|
|
236
|
|
|
$this->assertEquals(FOO::$calls['load'], array($filename)); |
237
|
|
|
imagedestroy(FOO::$handle); |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
public function testLoadFromFileFallbackToLoadFromString() |
241
|
|
|
{ |
242
|
|
|
FOO::$handle = imagecreate(10, 10); |
243
|
|
|
$filename = IMG_PATH . 'image-actually-foo.foo2'; |
244
|
|
|
WideImage::registerCustomMapper('FOO', 'image/foo', 'foo'); |
245
|
|
|
WideImage::registerCustomMapper('FOO2', 'image/foo2', 'foo2'); |
246
|
|
|
$img = WideImage::load($filename); |
|
|
|
|
247
|
|
|
$this->assertEquals(FOO2::$calls['load'], array($filename)); |
248
|
|
|
$this->assertEquals(FOO::$calls['loadFromString'], array(file_get_contents($filename))); |
249
|
|
|
imagedestroy(FOO::$handle); |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
public function testMapperSaveToFile() |
253
|
|
|
{ |
254
|
|
|
$img = WideImage::load(IMG_PATH . 'fgnl.jpg'); |
255
|
|
|
$img->saveToFile('test.foo', '123', 789); |
256
|
|
|
$this->assertEquals(FOO::$calls['save'], array($img->getHandle(), 'test.foo', '123', 789)); |
257
|
|
|
} |
258
|
|
|
|
259
|
|
|
public function testMapperAsString() |
260
|
|
|
{ |
261
|
|
|
$img = WideImage::load(IMG_PATH . 'fgnl.jpg'); |
262
|
|
|
$str = $img->asString('foo', '123', 789); |
263
|
|
|
$this->assertEquals(FOO::$calls['save'], array($img->getHandle(), null, '123', 789)); |
264
|
|
|
$this->assertEquals('out', $str); |
265
|
|
|
} |
266
|
|
|
|
267
|
|
|
/** |
268
|
|
|
* @expectedException WideImage\Exception\InvalidImageSourceException |
269
|
|
|
*/ |
270
|
|
|
public function testInvalidImageFile() |
271
|
|
|
{ |
272
|
|
|
WideImage::loadFromFile(IMG_PATH . 'fakeimage.png'); |
273
|
|
|
} |
274
|
|
|
|
275
|
|
|
/** |
276
|
|
|
* @expectedException WideImage\Exception\InvalidImageSourceException |
277
|
|
|
*/ |
278
|
|
|
public function testEmptyString() |
279
|
|
|
{ |
280
|
|
|
WideImage::load(''); |
281
|
|
|
} |
282
|
|
|
|
283
|
|
|
/** |
284
|
|
|
* @expectedException WideImage\Exception\InvalidImageSourceException |
285
|
|
|
*/ |
286
|
|
|
public function testInvalidImageStringData() |
287
|
|
|
{ |
288
|
|
|
WideImage::loadFromString('asdf'); |
289
|
|
|
} |
290
|
|
|
|
291
|
|
|
/** |
292
|
|
|
* @expectedException WideImage\Exception\InvalidImageSourceException |
293
|
|
|
*/ |
294
|
|
|
public function testInvalidImageHandle() |
295
|
|
|
{ |
296
|
|
|
WideImage::loadFromHandle(0); |
|
|
|
|
297
|
|
|
} |
298
|
|
|
|
299
|
|
|
/** |
300
|
|
|
* @expectedException WideImage\Exception\InvalidImageSourceException |
301
|
|
|
*/ |
302
|
|
|
public function testInvalidImageUploadField() |
303
|
|
|
{ |
304
|
|
|
WideImage::loadFromUpload('xyz'); |
305
|
|
|
} |
306
|
|
|
} |
307
|
|
|
|