Skip to content

Commit 77a6332

Browse files
committed
Merge pull request #8 from cainmi/master
Unit tests, refactored cropping
2 parents 05863bc + 425ceb2 commit 77a6332

File tree

5 files changed

+316
-13
lines changed

5 files changed

+316
-13
lines changed

.travis.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
language: php
2+
php:
3+
- 5.3
4+
- 5.4
5+
- 5.5
6+
- hhvm

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ If using [Composer](https://getcomposer.org/), in your `composer.json` file add:
1313
```json
1414
{
1515
"require": {
16-
"eventviva/php-image-resize": "1.1.*"
16+
"eventviva/php-image-resize": "1.2.*"
1717
}
1818
}
1919
```

phpunit.xml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<phpunit colors="true">
2+
<testsuites>
3+
<testsuite name="php-image-resize tests">
4+
<directory>test</directory>
5+
</testsuite>
6+
</testsuites>
7+
<logging>
8+
<log type="coverage-text" target="php://stdout"/>
9+
</logging>
10+
</phpunit>

src/ImageResize.php

Lines changed: 34 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@ class ImageResize
88
public $quality_jpg = 75;
99
public $quality_png = 0;
1010

11+
public $source_type;
12+
1113
protected $source_image;
12-
protected $source_type;
1314

1415
protected $original_w;
1516
protected $original_h;
@@ -156,22 +157,22 @@ public function output($image_type = null, $quality = null)
156157
$this->save(null, $image_type, $quality);
157158
}
158159

159-
public function resizeToHeight($height)
160+
public function resizeToHeight($height, $allow_enlarge = false)
160161
{
161162
$ratio = $height / $this->getSourceHeight();
162163
$width = $this->getSourceWidth() * $ratio;
163164

164-
$this->resize($width, $height);
165+
$this->resize($width, $height, $allow_enlarge);
165166

166167
return $this;
167168
}
168169

169-
public function resizeToWidth($width)
170+
public function resizeToWidth($width, $allow_enlarge = false)
170171
{
171172
$ratio = $width / $this->getSourceWidth();
172173
$height = $this->getSourceHeight() * $ratio;
173174

174-
$this->resize($width, $height);
175+
$this->resize($width, $height, $allow_enlarge);
175176

176177
return $this;
177178
}
@@ -189,6 +190,10 @@ public function scale($scale)
189190
public function resize($width, $height, $allow_enlarge = false)
190191
{
191192
if (!$allow_enlarge) {
193+
// if the user hasn't explicitly allowed enlarging,
194+
// but either of the dimensions are larger then the original,
195+
// then just use original dimensions - this logic may need rethinking
196+
192197
if ($width > $this->getSourceWidth() || $height > $this->getSourceHeight()) {
193198
$width = $this->getSourceWidth();
194199
$height = $this->getSourceHeight();
@@ -210,23 +215,40 @@ public function resize($width, $height, $allow_enlarge = false)
210215
public function crop($width, $height, $allow_enlarge = false)
211216
{
212217
if (!$allow_enlarge) {
213-
if ($width > $this->getSourceWidth() || $height > $this->getSourceHeight()) {
218+
// this logic is slightly different to resize(),
219+
// it will only reset dimensions to the original
220+
// if that particular dimenstion is larger
221+
222+
if ($width > $this->getSourceWidth()) {
214223
$width = $this->getSourceWidth();
224+
}
225+
226+
if ($height > $this->getSourceHeight()) {
215227
$height = $this->getSourceHeight();
216228
}
217229
}
218230

219-
$this->resize($width, $height, $allow_enlarge);
220-
221231
$ratio_source = $this->getSourceWidth() / $this->getSourceHeight();
222232
$ratio_dest = $width / $height;
223233

224234
if ($ratio_dest < $ratio_source) {
225-
$this->source_w /= $ratio_source;
226-
$this->source_x = ($this->getSourceWidth() - $this->source_w) / 2;
235+
$this->resizeToHeight($height, $allow_enlarge);
236+
237+
$excess_width = ($this->getDestWidth() - $width) / $this->getDestWidth() * $this->getSourceWidth();
238+
239+
$this->source_w = $this->getSourceWidth() - $excess_width;
240+
$this->source_x = $excess_width / 2;
241+
242+
$this->dest_w = $width;
227243
} else {
228-
$this->source_h /= $ratio_dest;
229-
$this->source_y = ($this->getSourceHeight() - $this->source_h) / 2;
244+
$this->resizeToWidth($width, $allow_enlarge);
245+
246+
$excess_height = ($this->getDestHeight() - $height) / $this->getDestHeight() * $this->getSourceHeight();
247+
248+
$this->source_h = $this->getSourceHeight() - $excess_height;
249+
$this->source_y = $excess_height / 2;
250+
251+
$this->dest_h = $height;
230252
}
231253

232254
return $this;

test/Test.php

Lines changed: 265 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,265 @@
1+
<?php
2+
3+
include 'src/ImageResize.php';
4+
5+
use \Eventviva\ImageResize;
6+
7+
class ImageResizeTest extends PHPUnit_Framework_TestCase
8+
{
9+
10+
private $image_types = array(
11+
'gif',
12+
'jpeg',
13+
'png'
14+
);
15+
16+
private $unsupported_image = 'Qk08AAAAAAAAADYAAAAoAAAAAQAAAAEAAAABABAAAAAAAAYAAAASCwAAEgsAAAAAAAAAAAAA/38AAAAA';
17+
18+
public function testLoadGif() {
19+
$image = $this->createImage(1, 1, 'gif');
20+
$resize = new ImageResize($image);
21+
22+
$this->assertEquals(IMAGETYPE_GIF, $resize->source_type);
23+
}
24+
25+
public function testLoadJpg() {
26+
$image = $this->createImage(1, 1, 'jpeg');
27+
$resize = new ImageResize($image);
28+
29+
$this->assertEquals(IMAGETYPE_JPEG, $resize->source_type);
30+
}
31+
32+
public function testLoadPng() {
33+
$image = $this->createImage(1, 1, 'png');
34+
$resize = new ImageResize($image);
35+
36+
$this->assertEquals(IMAGETYPE_PNG, $resize->source_type);
37+
}
38+
39+
/**
40+
* @expectedException Exception
41+
* @expectedExceptionMessage getimagesize(): Filename cannot be empty
42+
*/
43+
public function testLoadNoFile() {
44+
new ImageResize(null);
45+
}
46+
47+
/**
48+
* @expectedException Exception
49+
* @expectedExceptionMessage Could not read
50+
*/
51+
public function testLoadUnsupportedFile() {
52+
new ImageResize(__FILE__);
53+
}
54+
55+
/**
56+
* @expectedException Exception
57+
* @expectedExceptionMessage Unsupported image type
58+
*/
59+
public function testLoadUnsupportedImage() {
60+
$filename = $this->getTempFile();
61+
62+
$image = fopen($filename, 'w');
63+
fwrite($image, base64_decode($this->unsupported_image));
64+
fclose($image);
65+
66+
new ImageResize($filename);
67+
}
68+
69+
public function testResizeToHeight() {
70+
$image = $this->createImage(200, 100, 'png');
71+
$resize = new ImageResize($image);
72+
73+
$resize->resizeToHeight(50);
74+
75+
$this->assertEquals(100, $resize->getDestWidth());
76+
$this->assertEquals(50, $resize->getDestHeight());
77+
}
78+
79+
public function testResizeToWidth() {
80+
$image = $this->createImage(200, 100, 'png');
81+
$resize = new ImageResize($image);
82+
83+
$resize->resizeToWidth(100);
84+
85+
$this->assertEquals(100, $resize->getDestWidth());
86+
$this->assertEquals(50, $resize->getDestHeight());
87+
}
88+
89+
public function testScale() {
90+
$image = $this->createImage(200, 100, 'png');
91+
$resize = new ImageResize($image);
92+
93+
$resize->scale(50);
94+
95+
$this->assertEquals(100, $resize->getDestWidth());
96+
$this->assertEquals(50, $resize->getDestHeight());
97+
}
98+
99+
public function testResize() {
100+
$image = $this->createImage(200, 100, 'png');
101+
$resize = new ImageResize($image);
102+
103+
$resize->resize(50, 50);
104+
105+
$this->assertEquals(50, $resize->getDestWidth());
106+
$this->assertEquals(50, $resize->getDestHeight());
107+
}
108+
109+
public function testResizeLargerNotAllowed() {
110+
$image = $this->createImage(200, 100, 'png');
111+
$resize = new ImageResize($image);
112+
113+
$resize->resize(400, 200);
114+
115+
$this->assertEquals(200, $resize->getDestWidth());
116+
$this->assertEquals(100, $resize->getDestHeight());
117+
}
118+
119+
public function testCrop() {
120+
$image = $this->createImage(200, 100, 'png');
121+
$resize = new ImageResize($image);
122+
123+
$resize->crop(50, 50);
124+
125+
$this->assertEquals(50, $resize->getDestWidth());
126+
$this->assertEquals(50, $resize->getDestHeight());
127+
}
128+
129+
public function testCropLargerNotAllowed() {
130+
$image = $this->createImage(200, 100, 'png');
131+
$resize = new ImageResize($image);
132+
133+
$resize->crop(500, 500);
134+
135+
$this->assertEquals(200, $resize->getDestWidth());
136+
$this->assertEquals(100, $resize->getDestHeight());
137+
}
138+
139+
public function testSaveGif() {
140+
$image = $this->createImage(200, 100, 'gif');
141+
142+
$resize = new ImageResize($image);
143+
144+
$filename = $this->getTempFile();
145+
146+
$resize->save($filename);
147+
148+
$this->assertEquals(IMAGETYPE_GIF, exif_imagetype($filename));
149+
}
150+
151+
public function testSaveJpg() {
152+
$image = $this->createImage(200, 100, 'jpeg');
153+
154+
$resize = new ImageResize($image);
155+
156+
$filename = $this->getTempFile();
157+
158+
$resize->save($filename);
159+
160+
$this->assertEquals(IMAGETYPE_JPEG, exif_imagetype($filename));
161+
}
162+
163+
public function testSavePng() {
164+
$image = $this->createImage(200, 100, 'png');
165+
166+
$resize = new ImageResize($image);
167+
168+
$filename = $this->getTempFile();
169+
170+
$resize->save($filename);
171+
172+
$this->assertEquals(IMAGETYPE_PNG, exif_imagetype($filename));
173+
}
174+
175+
public function testSaveChmod() {
176+
$image = $this->createImage(200, 100, 'png');
177+
178+
$resize = new ImageResize($image);
179+
180+
$filename = $this->getTempFile();
181+
182+
$resize->save($filename, null, null, 0600);
183+
184+
$this->assertEquals(600, substr(decoct(fileperms($filename)), 3));
185+
}
186+
187+
public function testOutputGif() {
188+
$image = $this->createImage(200, 100, 'gif');
189+
190+
$resize = new ImageResize($image);
191+
192+
ob_start();
193+
194+
// supressing header errors
195+
@$resize->output();
196+
197+
$image_contents = ob_get_clean();
198+
199+
$info = finfo_open();
200+
201+
$type = finfo_buffer($info, $image_contents, FILEINFO_MIME_TYPE);
202+
203+
$this->assertEquals('image/gif', $type);
204+
}
205+
206+
public function testOutputJpg() {
207+
$image = $this->createImage(200, 100, 'jpeg');
208+
209+
$resize = new ImageResize($image);
210+
211+
ob_start();
212+
213+
// supressing header errors
214+
@$resize->output();
215+
216+
$image_contents = ob_get_clean();
217+
218+
$info = finfo_open();
219+
220+
$type = finfo_buffer($info, $image_contents, FILEINFO_MIME_TYPE);
221+
222+
$this->assertEquals('image/jpeg', $type);
223+
}
224+
225+
public function testOutputPng() {
226+
$image = $this->createImage(200, 100, 'png');
227+
228+
$resize = new ImageResize($image);
229+
230+
ob_start();
231+
232+
// supressing header errors
233+
@$resize->output();
234+
235+
$image_contents = ob_get_clean();
236+
237+
$info = finfo_open();
238+
239+
$type = finfo_buffer($info, $image_contents, FILEINFO_MIME_TYPE);
240+
241+
$this->assertEquals('image/png', $type);
242+
}
243+
244+
private function createImage($width, $height, $type) {
245+
if (!in_array($type, $this->image_types)) {
246+
throw new \Exception('Unsupported image type');
247+
}
248+
249+
$image = imagecreatetruecolor($width, $height);
250+
251+
$filename = $this->getTempFile();
252+
253+
$output_function = 'image' . $type;
254+
$output_function($image, $filename);
255+
256+
return $filename;
257+
}
258+
259+
private function getTempFile() {
260+
return tempnam(sys_get_temp_dir(), 'resize_test_image');
261+
}
262+
263+
}
264+
265+
?>

0 commit comments

Comments
 (0)