From be2849b52002ee5d8f752bf5349ec7604a28d175 Mon Sep 17 00:00:00 2001 From: peter279k Date: Thu, 1 Feb 2018 14:19:44 +0800 Subject: [PATCH 1/6] add more tests --- .editorconfig | 12 +++ .travis.yml | 34 ++++++-- composer.json | 17 +++- lib/ImageResize.php | 101 +++++++++++------------- lib/ImageResizeException.php | 10 +++ phpunit.xml | 17 ++-- test/ImageResizeExceptionTest.php | 44 +++++++++++ test/{Test.php => ImageResizeTest.php} | 105 ++++++++++++++----------- 8 files changed, 222 insertions(+), 118 deletions(-) create mode 100644 .editorconfig create mode 100644 lib/ImageResizeException.php create mode 100644 test/ImageResizeExceptionTest.php rename test/{Test.php => ImageResizeTest.php} (86%) diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..3de3873 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,12 @@ +# This file is for unifying the coding style for different editors and IDEs +# editorconfig.org + +root = true + +[*] +end_of_line = lf +charset = utf-8 +trim_trailing_whitespace = true +insert_final_newline = false +indent_style = space +indent_size = 4 diff --git a/.travis.yml b/.travis.yml index 16ef547..496c72e 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,7 +1,29 @@ language: php -php: - - 5.4 - - 5.5 - - 5.6 - - 7.0 - - 7.1 +matrix: + include: + - php: 5.3 + dist: precise + - php: 5.4 + dist: trusty + - php: 5.5 + dist: trusty + - php: 5.6 + dist: trusty + - php: 7.0 + dist: trusty + - php: 7.1 + dist: trusty + - php: 7.2 + dist: trusty + - php: nightly + dist: trusty + +before_script: + - composer install + +script: + - mkdir -p build/logs + - ./vendor/bin/phpunit --coverage-clover build/logs/clover.xml + +after_script: + - php vendor/bin/coveralls -v diff --git a/composer.json b/composer.json index 1094c56..67f59e6 100644 --- a/composer.json +++ b/composer.json @@ -13,15 +13,26 @@ ], "require": { "php": ">=5.3.0", - "ext-gd": "*" + "ext-gd": "*", + "ext-fileinfo": "*" }, "suggest": { "ext-exif": "Auto-rotate jpeg files" }, "autoload": { - "classmap": ["lib"] + "psr-4": { + "Gumlet\\": "lib/" + } + }, + "autoload-dev": { + "psr-4": { + "Gumlet\\": "test/" + } }, "require-dev": { - "apigen/apigen": "^4.1" + "phpunit/phpunit": "^4.8", + "php-coveralls/php-coveralls": "dev-master", + "ext-exif": "*", + "ext-gd": "*" } } diff --git a/lib/ImageResize.php b/lib/ImageResize.php index 976f330..26fa390 100644 --- a/lib/ImageResize.php +++ b/lib/ImageResize.php @@ -14,6 +14,9 @@ class ImageResize const CROPLEFT = 4; const CROPRIGHT = 5; const CROPTOPCENTER = 6; + const IMG_FLIP_HORIZONTAL = 0; + const IMG_FLIP_VERTICAL = 1; + const IMG_FLIP_BOTH = 2; public $quality_jpg = 85; public $quality_webp = 85; @@ -79,11 +82,12 @@ public function addFilter(callable $filter) * Apply filters. * * @param $image resource an image resource identifier + * @param $filterType filter type and default value is IMG_FILTER_NEGATE */ - protected function applyFilter($image) + protected function applyFilter($image, $filterType = IMG_FILTER_NEGATE) { foreach ($this->filters as $function) { - $function($image); + $function($image, $filterType); } } @@ -149,7 +153,6 @@ public function __construct($filename) default: throw new ImageResizeException('Unsupported image type'); - break; } if (!$this->source_image) { @@ -571,7 +574,7 @@ public function crop($width, $height, $allow_enlarge = false, $position = self:: */ public function freecrop($width, $height, $x = false, $y = false) { - if ($x === false or $y === false) { + if ($x === false || $y === false) { return $this->crop($width, $height); } $this->source_x = $x; @@ -658,59 +661,51 @@ protected function getCropPosition($expectedSize, $position = self::CROPCENTER) } return $size; } -} - -// imageflip definition for PHP < 5.5 -if (!function_exists('imageflip')) { - define('IMG_FLIP_HORIZONTAL', 0); - define('IMG_FLIP_VERTICAL', 1); - define('IMG_FLIP_BOTH', 2); - function imageflip($image, $mode) + /** + * Flips an image using a given mode if PHP version is lower than 5.5 + * + * @param resource $image + * @param integer $mode + * @return integer|null + */ + public function imageFlip($image, $mode) { - switch ($mode) { - case IMG_FLIP_HORIZONTAL: { - $max_x = imagesx($image) - 1; - $half_x = $max_x / 2; - $sy = imagesy($image); - $temp_image = imageistruecolor($image)? imagecreatetruecolor(1, $sy): imagecreate(1, $sy); - for ($x = 0; $x < $half_x; ++$x) { - imagecopy($temp_image, $image, 0, 0, $x, 0, 1, $sy); - imagecopy($image, $image, $x, 0, $max_x - $x, 0, 1, $sy); - imagecopy($image, $temp_image, $max_x - $x, 0, 0, 0, 1, $sy); + switch($mode) { + case self::IMG_FLIP_HORIZONTAL: { + $max_x = imagesx($image) - 1; + $half_x = $max_x / 2; + $sy = imagesy($image); + $temp_image = imageistruecolor($image)? imagecreatetruecolor(1, $sy): imagecreate(1, $sy); + for ($x = 0; $x < $half_x; ++$x) { + imagecopy($temp_image, $image, 0, 0, $x, 0, 1, $sy); + imagecopy($image, $image, $x, 0, $max_x - $x, 0, 1, $sy); + imagecopy($image, $temp_image, $max_x - $x, 0, 0, 0, 1, $sy); + } + break; } - break; - } -case IMG_FLIP_VERTICAL: { - $sx = imagesx($image); - $max_y = imagesy($image) - 1; - $half_y = $max_y / 2; - $temp_image = imageistruecolor($image)? imagecreatetruecolor($sx, 1): imagecreate($sx, 1); - for ($y = 0; $y < $half_y; ++$y) { - imagecopy($temp_image, $image, 0, 0, 0, $y, $sx, 1); - imagecopy($image, $image, 0, $y, 0, $max_y - $y, $sx, 1); - imagecopy($image, $temp_image, 0, $max_y - $y, 0, 0, $sx, 1); - } - break; -} -case IMG_FLIP_BOTH: { - $sx = imagesx($image); - $sy = imagesy($image); - $temp_image = imagerotate($image, 180, 0); - imagecopy($image, $temp_image, 0, 0, 0, 0, $sx, $sy); - break; -} -default: { - return; -} + case self::IMG_FLIP_VERTICAL: { + $sx = imagesx($image); + $max_y = imagesy($image) - 1; + $half_y = $max_y / 2; + $temp_image = imageistruecolor($image)? imagecreatetruecolor($sx, 1): imagecreate($sx, 1); + for ($y = 0; $y < $half_y; ++$y) { + imagecopy($temp_image, $image, 0, 0, 0, $y, $sx, 1); + imagecopy($image, $image, 0, $y, 0, $max_y - $y, $sx, 1); + imagecopy($image, $temp_image, 0, $max_y - $y, 0, 0, $sx, 1); + } + break; + } + case self::IMG_FLIP_BOTH: { + $sx = imagesx($image); + $sy = imagesy($image); + $temp_image = imagerotate($image, 180, 0); + imagecopy($image, $temp_image, 0, 0, 0, 0, $sx, $sy); + break; + } + default: + return null; } imagedestroy($temp_image); } } - -/** - * PHP Exception used in the ImageResize class - */ -class ImageResizeException extends \Exception -{ -} diff --git a/lib/ImageResizeException.php b/lib/ImageResizeException.php new file mode 100644 index 0000000..ef0da8d --- /dev/null +++ b/lib/ImageResizeException.php @@ -0,0 +1,10 @@ + + + - - test + + test - - lib - + + lib + - - - diff --git a/test/ImageResizeExceptionTest.php b/test/ImageResizeExceptionTest.php new file mode 100644 index 0000000..7b63a42 --- /dev/null +++ b/test/ImageResizeExceptionTest.php @@ -0,0 +1,44 @@ +assertEquals("", $e->getMessage()); + $this->assertInstanceOf('\Gumlet\ImageResizeException', $e); + } + + public function testExceptionMessage() + { + $e = new ImageResizeException("General error"); + + $this->assertEquals("General error", $e->getMessage()); + $this->assertInstanceOf('\Gumlet\ImageResizeException', $e); + } + + public function testExceptionExtending() + { + $e = new ImageResizeException("General error"); + + $this->assertInstanceOf('\Exception', $e); + } + + public function testExceptionThrown() + { + try{ + throw new ImageResizeException("General error"); + } catch (\Exception $e) { + $this->assertEquals("General error", $e->getMessage()); + $this->assertInstanceOf('\Gumlet\ImageResizeException', $e); + return; + } + $this->fail(); + } +} +// It's pretty easy to get your attention these days, isn't it? :D diff --git a/test/Test.php b/test/ImageResizeTest.php similarity index 86% rename from test/Test.php rename to test/ImageResizeTest.php index 5e4d413..63b0ad5 100644 --- a/test/Test.php +++ b/test/ImageResizeTest.php @@ -1,15 +1,10 @@ = 0 && !class_exists('PHPUnit_Framework_TestCase')) { - class_alias('PHPUnit\Framework\TestCase', 'PHPUnit_Framework_TestCase'); -} - -class ImageResizeTest extends PHPUnit_Framework_TestCase +class ImageResizeTest extends TestCase { private $image_types = array( @@ -43,7 +38,7 @@ public function testLoadJpg() $this->assertEquals(IMAGETYPE_JPEG, $resize->source_type); $this->assertInstanceOf('\Gumlet\ImageResize', $resize); } - + public function testLoadIgnoreXmpExifJpg() { $image = __DIR__.'/ressources/test_xmp.jpg'; @@ -70,6 +65,25 @@ public function testLoadString() $this->assertInstanceOf('\Gumlet\ImageResize', $resize); } + public function testAddFilter() + { + $image = $this->createImage(1, 1, 'png'); + $resize = new ImageResize($image); + $filename = $this->getTempFile(); + + $this->assertInstanceOf('\Gumlet\ImageResize', $resize->addFilter('imagefilter')); + } + + public function testApplyFilter() + { + $image = $this->createImage(1, 1, 'png'); + $resize = new ImageResize($image); + $resize->addFilter('imagefilter'); + $filename = $this->getTempFile(); + + $this->assertInstanceOf('\Gumlet\ImageResize', $resize->save($filename)); + } + /** * Bad load tests */ @@ -275,6 +289,16 @@ public function testFreeCrop() $this->assertEquals(50, $resize->getDestWidth()); $this->assertEquals(50, $resize->getDestHeight()); + + $resize->freecrop(50, 50); + + $this->assertEquals(50, $resize->getDestWidth()); + $this->assertEquals(50, $resize->getDestHeight()); + + $resize->freecrop(300, 300, 1, 1); + + $this->assertEquals(300, $resize->getDestWidth()); + $this->assertEquals(300, $resize->getDestHeight()); } public function testCropPosition() @@ -289,6 +313,22 @@ public function testCropPosition() $source_x->setAccessible(true); $this->assertEquals(100, $source_x->getValue($resize)); + + $resize->crop(50, 50, false, $resize::CROPCENTRE); + + $reflection_class = new ReflectionClass('\Gumlet\ImageResize'); + $source_x = $reflection_class->getProperty('source_x'); + $source_x->setAccessible(true); + + $this->assertEquals(50, $source_x->getValue($resize)); + + $resize->crop(50, 50, false, $resize::CROPTOPCENTER); + + $reflection_class = new ReflectionClass('\Gumlet\ImageResize'); + $source_x = $reflection_class->getProperty('source_x'); + $source_x->setAccessible(true); + + $this->assertEquals(25, $source_x->getValue($resize)); } public function testCropLargerNotAllowed() @@ -302,6 +342,16 @@ public function testCropLargerNotAllowed() $this->assertEquals(100, $resize->getDestHeight()); } + /** + * Image flip tests + */ + + public function testImageFlip() + { + $image = $this->createImage(200, 100, 'png'); + $resize = new ImageResize($image); + $resize->imageFlip(); + } /** * Save tests @@ -470,42 +520,3 @@ private function getTempFile() } } - -class ImageResizeExceptionTest extends PHPUnit_Framework_TestCase -{ - public function testExceptionEmpty() - { - $e = new ImageResizeException(); - - $this->assertEquals("", $e->getMessage()); - $this->assertInstanceOf('\Gumlet\ImageResizeException', $e); - } - - public function testExceptionMessage() - { - $e = new ImageResizeException("General error"); - - $this->assertEquals("General error", $e->getMessage()); - $this->assertInstanceOf('\Gumlet\ImageResizeException', $e); - } - - public function testExceptionExtending() - { - $e = new ImageResizeException("General error"); - - $this->assertInstanceOf('\Exception', $e); - } - - public function testExceptionThrown() - { - try{ - throw new ImageResizeException("General error"); - } catch (\Exception $e) { - $this->assertEquals("General error", $e->getMessage()); - $this->assertInstanceOf('\Gumlet\ImageResizeException', $e); - return; - } - $this->fail(); - } -} -// It's pretty easy to get your attention these days, isn't it? :D From 79085bf00b7c2a99e0c76d70b3a0fb8dd3597681 Mon Sep 17 00:00:00 2001 From: peter279k Date: Thu, 1 Feb 2018 14:52:44 +0800 Subject: [PATCH 2/6] complete the most of ImageResize tests --- lib/ImageResize.php | 2 +- test/ImageResizeTest.php | 11 ++++++++--- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/lib/ImageResize.php b/lib/ImageResize.php index 26fa390..02564ec 100644 --- a/lib/ImageResize.php +++ b/lib/ImageResize.php @@ -667,7 +667,7 @@ protected function getCropPosition($expectedSize, $position = self::CROPCENTER) * * @param resource $image * @param integer $mode - * @return integer|null + * @return null */ public function imageFlip($image, $mode) { diff --git a/test/ImageResizeTest.php b/test/ImageResizeTest.php index 63b0ad5..e797356 100644 --- a/test/ImageResizeTest.php +++ b/test/ImageResizeTest.php @@ -348,9 +348,14 @@ public function testCropLargerNotAllowed() public function testImageFlip() { - $image = $this->createImage(200, 100, 'png'); - $resize = new ImageResize($image); - $resize->imageFlip(); + $imageFileName = $this->createImage(200, 100, 'png'); + $resize = new ImageResize($imageFileName); + $image = imagecreatetruecolor(200, 100); + + $this::assertNull($resize->imageFlip($image, 0)); + $this::assertNull($resize->imageFlip($image, 1)); + $this::assertNull($resize->imageFlip($image, 2)); + $this->assertNull($resize->imageFlip($image, 3)); } /** From bafe1f6b664406ea98570acbf43056029ad71f70 Mon Sep 17 00:00:00 2001 From: peter279k Date: Thu, 1 Feb 2018 14:59:59 +0800 Subject: [PATCH 3/6] set the php-coveralls satisfied version --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 67f59e6..454584e 100644 --- a/composer.json +++ b/composer.json @@ -31,7 +31,7 @@ }, "require-dev": { "phpunit/phpunit": "^4.8", - "php-coveralls/php-coveralls": "dev-master", + "php-coveralls/php-coveralls": "dev-master || ^1.0", "ext-exif": "*", "ext-gd": "*" } From bf1f6f9abc73c3fe34aea9e53108e2e8c3ac2e3b Mon Sep 17 00:00:00 2001 From: peter279k Date: Thu, 1 Feb 2018 15:14:21 +0800 Subject: [PATCH 4/6] drop PHP 5.3 and set required PHP version is 5.4+. --- .travis.yml | 27 +++++++++------------------ composer.json | 2 +- 2 files changed, 10 insertions(+), 19 deletions(-) diff --git a/.travis.yml b/.travis.yml index 496c72e..de805f2 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,22 +1,13 @@ language: php -matrix: - include: - - php: 5.3 - dist: precise - - php: 5.4 - dist: trusty - - php: 5.5 - dist: trusty - - php: 5.6 - dist: trusty - - php: 7.0 - dist: trusty - - php: 7.1 - dist: trusty - - php: 7.2 - dist: trusty - - php: nightly - dist: trusty + +php: + - 5.4 + - 5.5 + - 5.6 + - 7.0 + - 7.1 + - 7.2 + - nightly before_script: - composer install diff --git a/composer.json b/composer.json index 454584e..6ef9747 100644 --- a/composer.json +++ b/composer.json @@ -12,7 +12,7 @@ } ], "require": { - "php": ">=5.3.0", + "php": ">=5.4.0", "ext-gd": "*", "ext-fileinfo": "*" }, From 785aeeacd9090ef590f6d5386cee49fa1f1f60c8 Mon Sep 17 00:00:00 2001 From: peter279k Date: Wed, 7 Feb 2018 10:15:18 +0800 Subject: [PATCH 5/6] remove .editorconfig --- .editorconfig | 12 ------------ 1 file changed, 12 deletions(-) delete mode 100644 .editorconfig diff --git a/.editorconfig b/.editorconfig deleted file mode 100644 index 3de3873..0000000 --- a/.editorconfig +++ /dev/null @@ -1,12 +0,0 @@ -# This file is for unifying the coding style for different editors and IDEs -# editorconfig.org - -root = true - -[*] -end_of_line = lf -charset = utf-8 -trim_trailing_whitespace = true -insert_final_newline = false -indent_style = space -indent_size = 4 From d38555ec6ffc27c8c417bf83275951089b662b3e Mon Sep 17 00:00:00 2001 From: peter279k Date: Wed, 7 Feb 2018 10:38:59 +0800 Subject: [PATCH 6/6] check imageflip is existed and add imageFlip usage --- README.md | 37 +++++++++++++++++++++++++------------ lib/ImageResize.php | 6 +++++- 2 files changed, 30 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index b5da1a3..b22a896 100644 --- a/README.md +++ b/README.md @@ -129,9 +129,9 @@ In the case of the example above, an image of 400px × 600px will be resize Crop modes: -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. +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. The default crop mode used is the `CROPCENTER`. -As a result those pieces of code are equivalent: +As a result those pieces of code are equivalent: ```php $image = new ImageResize('image.jpg'); @@ -269,7 +269,7 @@ By default, [image interlacing](http://php.net/manual/en/function.imageinterlace $image = new ImageResize('image.jpg'); $image->scale(50); $image->interlace = 0; -$image->save('image2.jpg') +$image->save('image2.jpg'); ``` Chaining @@ -305,26 +305,26 @@ try{ $image = new ImageResize(null); echo "This line will not be printed"; } catch (ImageResizeException $e) { - echo "Something went wrong" . $e->getMessage(); + echo "Something went wrong" . $e->getMessage(); } ``` - + Filters -------- - -You can apply special effects for new image like blur or add banner. + +You can apply special effects for new image like blur or add banner. ```php $image = new ImageResize('image.jpg'); // Add blure -$image->addFIlter(function ($imageDesc) { +$image->addFilter(function ($imageDesc) { imagefilter($imageDesc, IMG_FILTER_GAUSSIAN_BLUR); }); // Add banner on bottom left corner $image18Plus = 'banner.png' -$image->addFIlter(function ($imageDesc) use ($image18Plus) { +$image->addFilter(function ($imageDesc) use ($image18Plus) { $logo = imagecreatefrompng($image18Plus); $logo_width = imagesx($logo); $logo_height = imagesy($logo); @@ -336,10 +336,23 @@ $image->addFIlter(function ($imageDesc) use ($image18Plus) { }); ``` - + +Flip +-------- + +Flips an image using a given mode and this method is only for PHP version 5.4. + +```php +$flip = new ImageResize('image.png'); +$image = imagecreatetruecolor(200, 100); + +$flip->imageFlip($image, 0); + +``` + Both functions will be used in the order in which they were added. - - + + API Doc ------- diff --git a/lib/ImageResize.php b/lib/ImageResize.php index 02564ec..634a34f 100644 --- a/lib/ImageResize.php +++ b/lib/ImageResize.php @@ -188,7 +188,11 @@ public function imageCreateJpegfromExif($filename) } if ($orientation === 5 || $orientation === 4 || $orientation === 7) { - imageflip($img, IMG_FLIP_HORIZONTAL); + if(function_exists('imageflip')) { + imageflip($img, IMG_FLIP_HORIZONTAL); + } else { + $this->imageFlip($img, IMG_FLIP_HORIZONTAL); + } } return $img;