|
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\Operation; |
|
23
|
|
|
|
|
24
|
|
|
use WideImage\WideImage; |
|
25
|
|
|
use WideImage\Image; |
|
26
|
|
|
use WideImage\TrueColorImage; |
|
27
|
|
|
use WideImage\Operation\Resize; |
|
28
|
|
|
use Test\WideImage_TestCase; |
|
29
|
|
|
|
|
30
|
|
|
// WideImage_OperationFactory::get('Resize'); |
|
31
|
|
|
|
|
32
|
|
|
class ResizeTestable extends Resize |
|
33
|
|
|
{ |
|
34
|
|
|
public function prepareDimensions($img, $width, $height, $fit) |
|
35
|
|
|
{ |
|
36
|
|
|
return parent::prepareDimensions($img, $width, $height, $fit); |
|
37
|
|
|
} |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @package Tests |
|
42
|
|
|
*/ |
|
43
|
|
|
class ResizeTest extends WideImage_TestCase |
|
44
|
|
|
{ |
|
45
|
|
|
public function testProxyMethod() |
|
46
|
|
|
{ |
|
47
|
|
|
$op = $this->getMock('WideImage\\Operation\\Resize', array('execute')); |
|
48
|
|
|
$img = $this->getMock('WideImage\\PaletteImage', array('getOperation'), array(imagecreate(10, 10))); |
|
49
|
|
|
|
|
50
|
|
|
$img->expects($this->exactly(2))-> |
|
51
|
|
|
method('getOperation')->with('Resize')-> |
|
52
|
|
|
will($this->returnValue($op)); |
|
53
|
|
|
|
|
54
|
|
|
$op->expects($this->at(0))-> |
|
55
|
|
|
method('execute')->with($img, 'WIDTH', 'HEIGHT', 'FIT', 'SCALE'); |
|
56
|
|
|
|
|
57
|
|
|
$op->expects($this->at(1))-> |
|
58
|
|
|
method('execute')->with($img, null, null, 'inside', 'any'); |
|
59
|
|
|
|
|
60
|
|
|
$img->resize('WIDTH', 'HEIGHT', 'FIT', 'SCALE'); |
|
61
|
|
|
$img->resize(); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
public function testResultTypeIsSameAsInput() |
|
65
|
|
|
{ |
|
66
|
|
|
$this->assertInstanceOf("WideImage\\PaletteImage", WideImage::createPaletteImage(20, 20)->resize(10, 10)); |
|
67
|
|
|
$this->assertInstanceOf("WideImage\\TrueColorImage", WideImage::createTrueColorImage(20, 20)->resize(10, 10)); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
public function testResizeWithoutParametersDoesNothing() |
|
71
|
|
|
{ |
|
72
|
|
|
$img = WideImage::createTrueColorImage(70, 20); |
|
73
|
|
|
$res = $img->resize(); |
|
74
|
|
|
$this->assertDimensions($res, $img->getWidth(), $img->getHeight()); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
public function testPreservesTransparency() |
|
78
|
|
|
{ |
|
79
|
|
|
$img = $this->load('100x100-color-hole.gif'); |
|
80
|
|
|
$this->assertTrue($img->isTransparent()); |
|
81
|
|
|
$res = $img->resize(50, 50); |
|
82
|
|
|
$this->assertTrue($res->isTransparent()); |
|
83
|
|
|
$this->assertTransparentColorMatch($img, $res); |
|
84
|
|
|
$this->assertEquals($res->getColorAt(25, 25), $res->getTransparentColor()); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
public function testFitFill() |
|
88
|
|
|
{ |
|
89
|
|
|
$this->assertDimensions(WideImage::createPaletteImage(100, 50)->resize(100, 50, 'fill'), 100, 50); |
|
90
|
|
|
$this->assertDimensions(WideImage::createPaletteImage(100, 50)->resize(120, 30, 'fill'), 120, 30); |
|
91
|
|
|
$this->assertDimensions(WideImage::createPaletteImage(100, 50)->resize(20, 130, 'fill'), 20, 130); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
public function testFitOutside() |
|
95
|
|
|
{ |
|
96
|
|
|
$this->assertDimensions(WideImage::createPaletteImage(100, 50)->resize(100, 50, 'outside'), 100, 50); |
|
97
|
|
|
$this->assertDimensions(WideImage::createPaletteImage(100, 50)->resize(120, 30, 'outside'), 120, 60); |
|
98
|
|
|
$this->assertDimensions(WideImage::createPaletteImage(100, 50)->resize(20, 30, 'outside'), 60, 30); |
|
99
|
|
|
$this->assertDimensions(WideImage::createPaletteImage(100, 50)->resize(20, 100, 'outside'), 200, 100); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
public function testFitInside() |
|
103
|
|
|
{ |
|
104
|
|
|
$this->assertDimensions(WideImage::createPaletteImage(100, 50)->resize(100, 50, 'inside'), 100, 50); |
|
105
|
|
|
$this->assertDimensions(WideImage::createPaletteImage(100, 50)->resize(120, 30, 'inside'), 60, 30); |
|
106
|
|
|
$this->assertDimensions(WideImage::createPaletteImage(100, 50)->resize(20, 30, 'inside'), 20, 10); |
|
107
|
|
|
$this->assertDimensions(WideImage::createPaletteImage(100, 50)->resize(20, 100, 'inside'), 20, 10); |
|
108
|
|
|
$this->assertDimensions(WideImage::createPaletteImage(950, 266)->resize(256, null, 'inside'), 256, 72); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
public function testScaleDown() |
|
112
|
|
|
{ |
|
113
|
|
|
$this->assertDimensions(WideImage::createPaletteImage(100, 50)->resize(100, 50, 'fill', 'down'), 100, 50); |
|
114
|
|
|
$this->assertDimensions(WideImage::createPaletteImage(100, 50)->resize(120, 60, 'fill', 'down'), 100, 50); |
|
115
|
|
|
$this->assertDimensions(WideImage::createPaletteImage(100, 50)->resize(120, 40, 'fill', 'down'), 120, 40); |
|
116
|
|
|
$this->assertDimensions(WideImage::createPaletteImage(100, 50)->resize(90, 60, 'fill', 'down'), 90, 60); |
|
117
|
|
|
$this->assertDimensions(WideImage::createPaletteImage(100, 50)->resize(90, 30, 'fill', 'down'), 90, 30); |
|
118
|
|
|
|
|
119
|
|
|
$this->assertDimensions(WideImage::createPaletteImage(100, 50)->resize(100, 50, 'inside', 'down'), 100, 50); |
|
120
|
|
|
$this->assertDimensions(WideImage::createPaletteImage(100, 50)->resize(120, 60, 'inside', 'down'), 100, 50); |
|
121
|
|
|
$this->assertDimensions(WideImage::createPaletteImage(100, 50)->resize(120, 40, 'inside', 'down'), 80, 40); |
|
122
|
|
|
$this->assertDimensions(WideImage::createPaletteImage(100, 50)->resize(90, 60, 'inside', 'down'), 90, 45); |
|
123
|
|
|
$this->assertDimensions(WideImage::createPaletteImage(100, 50)->resize(90, 30, 'inside', 'down'), 60, 30); |
|
124
|
|
|
|
|
125
|
|
|
$this->assertDimensions(WideImage::createPaletteImage(100, 50)->resize(100, 50, 'outside', 'down'), 100, 50); |
|
126
|
|
|
$this->assertDimensions(WideImage::createPaletteImage(100, 50)->resize(120, 60, 'outside', 'down'), 100, 50); |
|
127
|
|
|
$this->assertDimensions(WideImage::createPaletteImage(100, 50)->resize(120, 40, 'outside', 'down'), 100, 50); |
|
128
|
|
|
$this->assertDimensions(WideImage::createPaletteImage(100, 50)->resize(90, 60, 'outside', 'down'), 100, 50); |
|
129
|
|
|
$this->assertDimensions(WideImage::createPaletteImage(100, 50)->resize(90, 30, 'outside', 'down'), 90, 45); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
public function testScaleUp() |
|
133
|
|
|
{ |
|
134
|
|
|
$this->assertDimensions(WideImage::createPaletteImage(100, 50)->resize(100, 50, 'fill', 'up'), 100, 50); |
|
135
|
|
|
$this->assertDimensions(WideImage::createPaletteImage(100, 50)->resize(120, 60, 'fill', 'up'), 120, 60); |
|
136
|
|
|
$this->assertDimensions(WideImage::createPaletteImage(100, 50)->resize(120, 40, 'fill', 'up'), 120, 40); |
|
137
|
|
|
$this->assertDimensions(WideImage::createPaletteImage(100, 50)->resize(90, 60, 'fill', 'up'), 90, 60); |
|
138
|
|
|
$this->assertDimensions(WideImage::createPaletteImage(100, 50)->resize(90, 30, 'fill', 'up'), 100, 50); |
|
139
|
|
|
|
|
140
|
|
|
$this->assertDimensions(WideImage::createPaletteImage(100, 50)->resize(100, 50, 'inside', 'up'), 100, 50); |
|
141
|
|
|
$this->assertDimensions(WideImage::createPaletteImage(100, 50)->resize(120, 60, 'inside', 'up'), 120, 60); |
|
142
|
|
|
$this->assertDimensions(WideImage::createPaletteImage(100, 50)->resize(120, 40, 'inside', 'up'), 100, 50); |
|
143
|
|
|
$this->assertDimensions(WideImage::createPaletteImage(100, 50)->resize(90, 60, 'inside', 'up'), 100, 50); |
|
144
|
|
|
$this->assertDimensions(WideImage::createPaletteImage(100, 50)->resize(90, 30, 'inside', 'up'), 100, 50); |
|
145
|
|
|
|
|
146
|
|
|
$this->assertDimensions(WideImage::createPaletteImage(100, 50)->resize(100, 50, 'outside', 'up'), 100, 50); |
|
147
|
|
|
$this->assertDimensions(WideImage::createPaletteImage(100, 50)->resize(120, 60, 'outside', 'up'), 120, 60); |
|
148
|
|
|
$this->assertDimensions(WideImage::createPaletteImage(100, 50)->resize(120, 40, 'outside', 'up'), 120, 60); |
|
149
|
|
|
$this->assertDimensions(WideImage::createPaletteImage(100, 50)->resize(90, 60, 'outside', 'up'), 120, 60); |
|
150
|
|
|
$this->assertDimensions(WideImage::createPaletteImage(100, 50)->resize(90, 30, 'outside', 'up'), 100, 50); |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
public function testResizeFill() |
|
154
|
|
|
{ |
|
155
|
|
|
$img = WideImage::load(IMG_PATH . '100x100-color-hole.gif'); |
|
156
|
|
|
$resized = $img->resize(50, 20, 'fill'); |
|
157
|
|
|
$this->assertTrue($resized instanceof Image); |
|
158
|
|
|
$this->assertTrue($resized->isTransparent()); |
|
159
|
|
|
$this->assertEquals(50, $resized->getWidth()); |
|
160
|
|
|
$this->assertEquals(20, $resized->getHeight()); |
|
161
|
|
|
$this->assertRGBEqual($resized->getRGBAt(5, 5), 255, 255, 0); |
|
162
|
|
|
$this->assertRGBEqual($resized->getRGBAt(45, 5), 0, 0, 255); |
|
163
|
|
|
$this->assertRGBEqual($resized->getRGBAt(45, 15), 0, 255, 0); |
|
164
|
|
|
$this->assertRGBEqual($resized->getRGBAt(5, 15), 255, 0, 0); |
|
165
|
|
|
|
|
166
|
|
|
$this->assertRGBEqual($resized->getRGBAt(25, 10), 255, 255, 255); |
|
167
|
|
|
$this->assertRGBEqual($img->getTransparentColorRGB(), 255, 255, 255); |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
public function testNullDimensionsAreCalculatedForFill() |
|
171
|
|
|
{ |
|
172
|
|
|
$img = TrueColorImage::create(100, 50); |
|
173
|
|
|
$resized = $img->resize(30, null, 'fill'); |
|
174
|
|
|
$this->assertEquals(30, $resized->getWidth()); |
|
175
|
|
|
$this->assertEquals(15, $resized->getHeight()); |
|
176
|
|
|
|
|
177
|
|
|
$img = TrueColorImage::create(100, 50); |
|
178
|
|
|
$resized = $img->resize(null, 30, 'fill'); |
|
179
|
|
|
$this->assertEquals(60, $resized->getWidth()); |
|
180
|
|
|
$this->assertEquals(30, $resized->getHeight()); |
|
181
|
|
|
|
|
182
|
|
|
$img = TrueColorImage::create(100, 50); |
|
183
|
|
|
$resized = $img->resize(30, 30, 'fill'); |
|
184
|
|
|
$this->assertEquals(30, $resized->getWidth()); |
|
185
|
|
|
$this->assertEquals(30, $resized->getHeight()); |
|
186
|
|
|
|
|
187
|
|
|
$img = TrueColorImage::create(100, 50); |
|
188
|
|
|
$resized = $img->resize(30, 40, 'fill'); |
|
189
|
|
|
$this->assertEquals(30, $resized->getWidth()); |
|
190
|
|
|
$this->assertEquals(40, $resized->getHeight()); |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
public function testResizeInside() |
|
194
|
|
|
{ |
|
195
|
|
|
$img = WideImage::load(IMG_PATH . '100x100-color-hole.gif'); |
|
196
|
|
|
$resized = $img->resize(50, 20, 'inside'); |
|
197
|
|
|
$this->assertTrue($resized instanceof Image); |
|
198
|
|
|
$this->assertTrue($resized->isTransparent()); |
|
199
|
|
|
$this->assertEquals(20, $resized->getWidth()); |
|
200
|
|
|
$this->assertEquals(20, $resized->getHeight()); |
|
201
|
|
|
/* |
|
202
|
|
|
$this->assertRGBEqual($resized->getRGBAt(5, 5), 255, 255, 0); |
|
203
|
|
|
$this->assertRGBEqual($resized->getRGBAt(45, 5), 0, 0, 255); |
|
204
|
|
|
$this->assertRGBEqual($resized->getRGBAt(45, 15), 0, 255, 0); |
|
205
|
|
|
$this->assertRGBEqual($resized->getRGBAt(5, 15), 255, 0, 0); |
|
206
|
|
|
$this->assertRGBEqual($resized->getRGBAt(25, 10), 255, 255, 255); |
|
207
|
|
|
$this->assertRGBEqual($img->getTransparentColorRGB(), 255, 255, 255); |
|
208
|
|
|
*/ |
|
209
|
|
|
} |
|
210
|
|
|
|
|
211
|
|
|
public function testResizeDown() |
|
212
|
|
|
{ |
|
213
|
|
|
$img = TrueColorImage::create(100, 100); |
|
214
|
|
|
$resized = $img->resizeDown(30); |
|
215
|
|
|
$this->assertEquals(30, $resized->getWidth()); |
|
216
|
|
|
$this->assertEquals(30, $resized->getHeight()); |
|
217
|
|
|
|
|
218
|
|
|
$img = TrueColorImage::create(200, 100); |
|
219
|
|
|
$resized = $img->resizeDown(100); |
|
220
|
|
|
$this->assertEquals(100, $resized->getWidth()); |
|
221
|
|
|
$this->assertEquals(50, $resized->getHeight()); |
|
222
|
|
|
|
|
223
|
|
|
$img = TrueColorImage::create(200, 100); |
|
224
|
|
|
$resized = $img->resizeDown(null, 30); |
|
225
|
|
|
$this->assertEquals(60, $resized->getWidth()); |
|
226
|
|
|
$this->assertEquals(30, $resized->getHeight()); |
|
227
|
|
|
|
|
228
|
|
|
$img = TrueColorImage::create(200, 100); |
|
229
|
|
|
$resized = $img->resizeDown(201); |
|
230
|
|
|
$this->assertEquals($img->getWidth(), $resized->getWidth()); |
|
231
|
|
|
$this->assertEquals($img->getHeight(), $resized->getHeight()); |
|
232
|
|
|
|
|
233
|
|
|
$img = TrueColorImage::create(200, 100); |
|
234
|
|
|
$resized = $img->resizeDown(null, 300); |
|
235
|
|
|
$this->assertEquals($img->getWidth(), $resized->getWidth()); |
|
236
|
|
|
$this->assertEquals($img->getHeight(), $resized->getHeight()); |
|
237
|
|
|
} |
|
238
|
|
|
|
|
239
|
|
|
public function testResizeUp() |
|
240
|
|
|
{ |
|
241
|
|
|
$img = TrueColorImage::create(100, 100); |
|
242
|
|
|
$resized = $img->resizeUp(300); |
|
243
|
|
|
$this->assertEquals(300, $resized->getWidth()); |
|
244
|
|
|
$this->assertEquals(300, $resized->getHeight()); |
|
245
|
|
|
|
|
246
|
|
|
$img = TrueColorImage::create(200, 100); |
|
247
|
|
|
$resized = $img->resizeUp(300); |
|
248
|
|
|
$this->assertEquals(300, $resized->getWidth()); |
|
249
|
|
|
$this->assertEquals(150, $resized->getHeight()); |
|
250
|
|
|
|
|
251
|
|
|
$img = TrueColorImage::create(20, 10); |
|
252
|
|
|
$resized = $img->resizeUp(null, 30); |
|
253
|
|
|
$this->assertEquals(60, $resized->getWidth()); |
|
254
|
|
|
$this->assertEquals(30, $resized->getHeight()); |
|
255
|
|
|
|
|
256
|
|
|
$img = TrueColorImage::create(200, 100); |
|
257
|
|
|
$resized = $img->resizeUp(199); |
|
258
|
|
|
$this->assertEquals($img->getWidth(), $resized->getWidth()); |
|
259
|
|
|
$this->assertEquals($img->getHeight(), $resized->getHeight()); |
|
260
|
|
|
|
|
261
|
|
|
$img = TrueColorImage::create(200, 100); |
|
262
|
|
|
$resized = $img->resizeUp(null, 10); |
|
263
|
|
|
$this->assertEquals($img->getWidth(), $resized->getWidth()); |
|
264
|
|
|
$this->assertEquals($img->getHeight(), $resized->getHeight()); |
|
265
|
|
|
} |
|
266
|
|
|
|
|
267
|
|
|
/** |
|
268
|
|
|
* @group bugs |
|
269
|
|
|
*/ |
|
270
|
|
|
public function testResizeBug214() |
|
271
|
|
|
{ |
|
272
|
|
|
$img = TrueColorImage::create(1600, 1200); |
|
273
|
|
|
$op = new ResizeTestable(); |
|
274
|
|
|
$dim = $op->prepareDimensions($img, 214, null, 'outside'); |
|
275
|
|
|
$this->assertEquals(214, $dim['width']); |
|
276
|
|
|
$this->assertEquals(161, $dim['height']); |
|
277
|
|
|
} |
|
278
|
|
|
|
|
279
|
|
|
/** |
|
280
|
|
|
* https://sourceforge.net/tracker/?func=detail&aid=3312764&group_id=190526&atid=933712 |
|
281
|
|
|
* @group bugs |
|
282
|
|
|
*/ |
|
283
|
|
|
public function testResizeBug950to256() |
|
284
|
|
|
{ |
|
285
|
|
|
$img = TrueColorImage::create(950, 266); |
|
286
|
|
|
$op = new ResizeTestable(); |
|
287
|
|
|
$dim = $op->prepareDimensions($img, 256, null, 'inside'); |
|
288
|
|
|
$this->assertEquals(256, $dim['width']); |
|
289
|
|
|
$this->assertEquals(72, $dim['height']); |
|
290
|
|
|
} |
|
291
|
|
|
} |
|
292
|
|
|
|