Skip to content

Commit f6fb03f

Browse files
Merge pull request #103 from peter279k/test_enhancement
Test enhancement
2 parents 23c2320 + d38555e commit f6fb03f

8 files changed

+231
-126
lines changed

.travis.yml

+13
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,20 @@
11
language: php
2+
23
php:
34
- 5.4
45
- 5.5
56
- 5.6
67
- 7.0
78
- 7.1
9+
- 7.2
10+
- nightly
11+
12+
before_script:
13+
- composer install
14+
15+
script:
16+
- mkdir -p build/logs
17+
- ./vendor/bin/phpunit --coverage-clover build/logs/clover.xml
18+
19+
after_script:
20+
- php vendor/bin/coveralls -v

README.md

+25-12
Original file line numberDiff line numberDiff line change
@@ -129,9 +129,9 @@ In the case of the example above, an image of 400px × 600px will be resize
129129

130130
Crop modes:
131131

132-
Few crop mode options are available in order for you to choose how you want to handle the eventual exceeding width or height after resizing down your image.
132+
Few crop mode options are available in order for you to choose how you want to handle the eventual exceeding width or height after resizing down your image.
133133
The default crop mode used is the `CROPCENTER`.
134-
As a result those pieces of code are equivalent:
134+
As a result those pieces of code are equivalent:
135135

136136
```php
137137
$image = new ImageResize('image.jpg');
@@ -269,7 +269,7 @@ By default, [image interlacing](http://php.net/manual/en/function.imageinterlace
269269
$image = new ImageResize('image.jpg');
270270
$image->scale(50);
271271
$image->interlace = 0;
272-
$image->save('image2.jpg')
272+
$image->save('image2.jpg');
273273
```
274274

275275
Chaining
@@ -305,26 +305,26 @@ try{
305305
$image = new ImageResize(null);
306306
echo "This line will not be printed";
307307
} catch (ImageResizeException $e) {
308-
echo "Something went wrong" . $e->getMessage();
308+
echo "Something went wrong" . $e->getMessage();
309309
}
310310
```
311-
311+
312312
Filters
313313
--------
314-
315-
You can apply special effects for new image like blur or add banner.
314+
315+
You can apply special effects for new image like blur or add banner.
316316

317317
```php
318318
$image = new ImageResize('image.jpg');
319319

320320
// Add blure
321-
$image->addFIlter(function ($imageDesc) {
321+
$image->addFilter(function ($imageDesc) {
322322
imagefilter($imageDesc, IMG_FILTER_GAUSSIAN_BLUR);
323323
});
324324

325325
// Add banner on bottom left corner
326326
$image18Plus = 'banner.png'
327-
$image->addFIlter(function ($imageDesc) use ($image18Plus) {
327+
$image->addFilter(function ($imageDesc) use ($image18Plus) {
328328
$logo = imagecreatefrompng($image18Plus);
329329
$logo_width = imagesx($logo);
330330
$logo_height = imagesy($logo);
@@ -336,10 +336,23 @@ $image->addFIlter(function ($imageDesc) use ($image18Plus) {
336336
});
337337

338338
```
339-
339+
340+
Flip
341+
--------
342+
343+
Flips an image using a given mode and this method is only for PHP version 5.4.
344+
345+
```php
346+
$flip = new ImageResize('image.png');
347+
$image = imagecreatetruecolor(200, 100);
348+
349+
$flip->imageFlip($image, 0);
350+
351+
```
352+
340353
Both functions will be used in the order in which they were added.
341-
342-
354+
355+
343356
API Doc
344357
-------
345358

composer.json

+15-4
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,27 @@
1212
}
1313
],
1414
"require": {
15-
"php": ">=5.3.0",
16-
"ext-gd": "*"
15+
"php": ">=5.4.0",
16+
"ext-gd": "*",
17+
"ext-fileinfo": "*"
1718
},
1819
"suggest": {
1920
"ext-exif": "Auto-rotate jpeg files"
2021
},
2122
"autoload": {
22-
"classmap": ["lib"]
23+
"psr-4": {
24+
"Gumlet\\": "lib/"
25+
}
26+
},
27+
"autoload-dev": {
28+
"psr-4": {
29+
"Gumlet\\": "test/"
30+
}
2331
},
2432
"require-dev": {
25-
"apigen/apigen": "^4.1"
33+
"phpunit/phpunit": "^4.8",
34+
"php-coveralls/php-coveralls": "dev-master || ^1.0",
35+
"ext-exif": "*",
36+
"ext-gd": "*"
2637
}
2738
}

lib/ImageResize.php

+53-54
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ class ImageResize
1414
const CROPLEFT = 4;
1515
const CROPRIGHT = 5;
1616
const CROPTOPCENTER = 6;
17+
const IMG_FLIP_HORIZONTAL = 0;
18+
const IMG_FLIP_VERTICAL = 1;
19+
const IMG_FLIP_BOTH = 2;
1720

1821
public $quality_jpg = 85;
1922
public $quality_webp = 85;
@@ -79,11 +82,12 @@ public function addFilter(callable $filter)
7982
* Apply filters.
8083
*
8184
* @param $image resource an image resource identifier
85+
* @param $filterType filter type and default value is IMG_FILTER_NEGATE
8286
*/
83-
protected function applyFilter($image)
87+
protected function applyFilter($image, $filterType = IMG_FILTER_NEGATE)
8488
{
8589
foreach ($this->filters as $function) {
86-
$function($image);
90+
$function($image, $filterType);
8791
}
8892
}
8993

@@ -149,7 +153,6 @@ public function __construct($filename)
149153

150154
default:
151155
throw new ImageResizeException('Unsupported image type');
152-
break;
153156
}
154157

155158
if (!$this->source_image) {
@@ -185,7 +188,11 @@ public function imageCreateJpegfromExif($filename)
185188
}
186189

187190
if ($orientation === 5 || $orientation === 4 || $orientation === 7) {
188-
imageflip($img, IMG_FLIP_HORIZONTAL);
191+
if(function_exists('imageflip')) {
192+
imageflip($img, IMG_FLIP_HORIZONTAL);
193+
} else {
194+
$this->imageFlip($img, IMG_FLIP_HORIZONTAL);
195+
}
189196
}
190197

191198
return $img;
@@ -571,7 +578,7 @@ public function crop($width, $height, $allow_enlarge = false, $position = self::
571578
*/
572579
public function freecrop($width, $height, $x = false, $y = false)
573580
{
574-
if ($x === false or $y === false) {
581+
if ($x === false || $y === false) {
575582
return $this->crop($width, $height);
576583
}
577584
$this->source_x = $x;
@@ -658,59 +665,51 @@ protected function getCropPosition($expectedSize, $position = self::CROPCENTER)
658665
}
659666
return $size;
660667
}
661-
}
662-
663-
// imageflip definition for PHP < 5.5
664-
if (!function_exists('imageflip')) {
665-
define('IMG_FLIP_HORIZONTAL', 0);
666-
define('IMG_FLIP_VERTICAL', 1);
667-
define('IMG_FLIP_BOTH', 2);
668668

669-
function imageflip($image, $mode)
669+
/**
670+
* Flips an image using a given mode if PHP version is lower than 5.5
671+
*
672+
* @param resource $image
673+
* @param integer $mode
674+
* @return null
675+
*/
676+
public function imageFlip($image, $mode)
670677
{
671-
switch ($mode) {
672-
case IMG_FLIP_HORIZONTAL: {
673-
$max_x = imagesx($image) - 1;
674-
$half_x = $max_x / 2;
675-
$sy = imagesy($image);
676-
$temp_image = imageistruecolor($image)? imagecreatetruecolor(1, $sy): imagecreate(1, $sy);
677-
for ($x = 0; $x < $half_x; ++$x) {
678-
imagecopy($temp_image, $image, 0, 0, $x, 0, 1, $sy);
679-
imagecopy($image, $image, $x, 0, $max_x - $x, 0, 1, $sy);
680-
imagecopy($image, $temp_image, $max_x - $x, 0, 0, 0, 1, $sy);
678+
switch($mode) {
679+
case self::IMG_FLIP_HORIZONTAL: {
680+
$max_x = imagesx($image) - 1;
681+
$half_x = $max_x / 2;
682+
$sy = imagesy($image);
683+
$temp_image = imageistruecolor($image)? imagecreatetruecolor(1, $sy): imagecreate(1, $sy);
684+
for ($x = 0; $x < $half_x; ++$x) {
685+
imagecopy($temp_image, $image, 0, 0, $x, 0, 1, $sy);
686+
imagecopy($image, $image, $x, 0, $max_x - $x, 0, 1, $sy);
687+
imagecopy($image, $temp_image, $max_x - $x, 0, 0, 0, 1, $sy);
688+
}
689+
break;
681690
}
682-
break;
683-
}
684-
case IMG_FLIP_VERTICAL: {
685-
$sx = imagesx($image);
686-
$max_y = imagesy($image) - 1;
687-
$half_y = $max_y / 2;
688-
$temp_image = imageistruecolor($image)? imagecreatetruecolor($sx, 1): imagecreate($sx, 1);
689-
for ($y = 0; $y < $half_y; ++$y) {
690-
imagecopy($temp_image, $image, 0, 0, 0, $y, $sx, 1);
691-
imagecopy($image, $image, 0, $y, 0, $max_y - $y, $sx, 1);
692-
imagecopy($image, $temp_image, 0, $max_y - $y, 0, 0, $sx, 1);
693-
}
694-
break;
695-
}
696-
case IMG_FLIP_BOTH: {
697-
$sx = imagesx($image);
698-
$sy = imagesy($image);
699-
$temp_image = imagerotate($image, 180, 0);
700-
imagecopy($image, $temp_image, 0, 0, 0, 0, $sx, $sy);
701-
break;
702-
}
703-
default: {
704-
return;
705-
}
691+
case self::IMG_FLIP_VERTICAL: {
692+
$sx = imagesx($image);
693+
$max_y = imagesy($image) - 1;
694+
$half_y = $max_y / 2;
695+
$temp_image = imageistruecolor($image)? imagecreatetruecolor($sx, 1): imagecreate($sx, 1);
696+
for ($y = 0; $y < $half_y; ++$y) {
697+
imagecopy($temp_image, $image, 0, 0, 0, $y, $sx, 1);
698+
imagecopy($image, $image, 0, $y, 0, $max_y - $y, $sx, 1);
699+
imagecopy($image, $temp_image, 0, $max_y - $y, 0, 0, $sx, 1);
700+
}
701+
break;
702+
}
703+
case self::IMG_FLIP_BOTH: {
704+
$sx = imagesx($image);
705+
$sy = imagesy($image);
706+
$temp_image = imagerotate($image, 180, 0);
707+
imagecopy($image, $temp_image, 0, 0, 0, 0, $sx, $sy);
708+
break;
709+
}
710+
default:
711+
return null;
706712
}
707713
imagedestroy($temp_image);
708714
}
709715
}
710-
711-
/**
712-
* PHP Exception used in the ImageResize class
713-
*/
714-
class ImageResizeException extends \Exception
715-
{
716-
}

lib/ImageResizeException.php

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace Gumlet;
4+
5+
/**
6+
* PHP Exception used in the ImageResize class
7+
*/
8+
class ImageResizeException extends \Exception
9+
{
10+
}

phpunit.xml

+8-9
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
1-
<phpunit colors="true">
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit bootstrap="vendor/autoload.php" colors="true" backupGlobals="false"
3+
backupStaticAttributes="false" syntaxCheck="false">
24
<testsuites>
3-
<testsuite name="php-image-resize tests">
4-
<directory>test</directory>
5+
<testsuite name="Tests">
6+
<directory suffix="Test.php">test</directory>
57
</testsuite>
68
</testsuites>
79
<filter>
8-
<whitelist processUncoveredFilesFromWhitelist="true">
9-
<directory suffix=".php">lib</directory>
10-
</whitelist>
10+
<whitelist processUncoveredFilesFromWhitelist="false">
11+
<directory suffix=".php">lib</directory>
12+
</whitelist>
1113
</filter>
12-
<logging>
13-
<log type="coverage-text" target="php://stdout"/>
14-
</logging>
1514
</phpunit>

test/ImageResizeExceptionTest.php

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
use \Gumlet\ImageResize;
4+
use \Gumlet\ImageResizeException;
5+
use \PHPUnit\Framework\TestCase;
6+
7+
class ImageResizeExceptionTest extends TestCase
8+
{
9+
public function testExceptionEmpty()
10+
{
11+
$e = new ImageResizeException();
12+
13+
$this->assertEquals("", $e->getMessage());
14+
$this->assertInstanceOf('\Gumlet\ImageResizeException', $e);
15+
}
16+
17+
public function testExceptionMessage()
18+
{
19+
$e = new ImageResizeException("General error");
20+
21+
$this->assertEquals("General error", $e->getMessage());
22+
$this->assertInstanceOf('\Gumlet\ImageResizeException', $e);
23+
}
24+
25+
public function testExceptionExtending()
26+
{
27+
$e = new ImageResizeException("General error");
28+
29+
$this->assertInstanceOf('\Exception', $e);
30+
}
31+
32+
public function testExceptionThrown()
33+
{
34+
try{
35+
throw new ImageResizeException("General error");
36+
} catch (\Exception $e) {
37+
$this->assertEquals("General error", $e->getMessage());
38+
$this->assertInstanceOf('\Gumlet\ImageResizeException', $e);
39+
return;
40+
}
41+
$this->fail();
42+
}
43+
}
44+
// It's pretty easy to get your attention these days, isn't it? :D

0 commit comments

Comments
 (0)