diff --git a/README.md b/README.md index f46907c..69c8c71 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,9 @@ # image-transform +[![Build Status](https://travis-ci.org/jobyone/image-transform.svg?branch=main)](https://travis-ci.org/jobyone/image-transform) +[![Coverage Status](https://coveralls.io/repos/github/jobyone/image-transform/badge.svg?branch=main)](https://coveralls.io/github/jobyone/image-transform?branch=main) + + A tightly-focused library for performing a very limited set of simple image transformations. This library's purpose is to eschew the standard kitchen sink approach to PHP image libraries in favor of high performance, wide driver support, and a dead simple API. ## Roadmap diff --git a/src/Sizers/Cover.php b/src/Sizers/Cover.php index 8eaa0ed..8cdb921 100644 --- a/src/Sizers/Cover.php +++ b/src/Sizers/Cover.php @@ -32,12 +32,22 @@ class Cover extends AbstractSizer ]; } + public function height(): int + { + return $this->height; + } + public function width(): int + { + return $this->width; + } + + public function resizeToWidth(): ?int { return $this->calculateSize()['width']; } - public function height(): int + public function resizeToHeight(): ?int { return $this->calculateSize()['height']; } diff --git a/src/Sizers/Crop.php b/src/Sizers/Crop.php deleted file mode 100644 index eaf38c6..0000000 --- a/src/Sizers/Crop.php +++ /dev/null @@ -1,44 +0,0 @@ -width = $width; - $this->height = $height; - } - - public function width(): int - { - return $this->width; - } - - public function height(): int - { - return $this->height; - } - - public function resizeToHeight(): ?int - { - return null; - } - - public function resizeToWidth(): ?int - { - return null; - } - - public function cropToWidth(): ?int - { - return $this->width; - } - - public function cropToHeight(): ?int - { - return $this->height; - } -} diff --git a/tests/units/Sizers/Cover.php b/tests/units/Sizers/Cover.php index cb54cf2..e9a8da9 100644 --- a/tests/units/Sizers/Cover.php +++ b/tests/units/Sizers/Cover.php @@ -13,13 +13,13 @@ class Cover extends atoum $this ->given($image = $this->image(__DIR__.'/../100x200.jpg',100,150)) ->integer($image->width())->isEqualTo(100) - ->integer($image->height())->isEqualTo(200) + ->integer($image->height())->isEqualTo(150) ->variable($image->sizer()->resizeToWidth())->isEqualTo(100) ->variable($image->sizer()->resizeToHeight())->isEqualTo(200) ->variable($image->sizer()->cropToWidth())->isEqualTo(100) ->variable($image->sizer()->cropToHeight())->isEqualTo(150) ->given($image->rotate()) - ->integer($image->width())->isEqualTo(300) + ->integer($image->width())->isEqualTo(100) ->integer($image->height())->isEqualTo(150) ->variable($image->sizer()->resizeToWidth())->isEqualTo(300) ->variable($image->sizer()->resizeToHeight())->isEqualTo(150) @@ -28,142 +28,142 @@ class Cover extends atoum ; } - // public function testPortraitWhenImageIsShorter() - // { - // $this - // ->given($image = $this->image(__DIR__.'/../100x200.jpg',50,300)) - // ->integer($image->width())->isEqualTo(50) - // ->integer($image->height())->isEqualTo(100) - // ->variable($image->sizer()->resizeToWidth())->isEqualTo(50) - // ->variable($image->sizer()->resizeToHeight())->isEqualTo(100) - // ->variable($image->sizer()->cropToWidth())->isNull() - // ->variable($image->sizer()->cropToHeight())->isNull() - // ->given($image->rotate()) - // ->integer($image->width())->isEqualTo(50) - // ->integer($image->height())->isEqualTo(25) - // ->variable($image->sizer()->resizeToWidth())->isEqualTo(50) - // ->variable($image->sizer()->resizeToHeight())->isEqualTo(25) - // ->variable($image->sizer()->cropToWidth())->isNull() - // ->variable($image->sizer()->cropToHeight())->isNull() - // ; - // } + public function testPortraitWhenImageIsShorter() + { + $this + ->given($image = $this->image(__DIR__.'/../100x200.jpg',50,300)) + ->integer($image->width())->isEqualTo(50) + ->integer($image->height())->isEqualTo(300) + ->variable($image->sizer()->resizeToWidth())->isEqualTo(150) + ->variable($image->sizer()->resizeToHeight())->isEqualTo(300) + ->variable($image->sizer()->cropToWidth())->isEqualTo(50) + ->variable($image->sizer()->cropToHeight())->isEqualTo(300) + ->given($image->rotate()) + ->integer($image->width())->isEqualTo(50) + ->integer($image->height())->isEqualTo(300) + ->variable($image->sizer()->resizeToWidth())->isEqualTo(600) + ->variable($image->sizer()->resizeToHeight())->isEqualTo(300) + ->variable($image->sizer()->cropToWidth())->isEqualTo(50) + ->variable($image->sizer()->cropToHeight())->isEqualTo(300) + ; + } - // public function testPortraitWithSquareImage() - // { - // $this - // ->given($image = $this->image(__DIR__.'/../200x200.jpg',75,300)) - // ->integer($image->width())->isEqualTo(75) - // ->integer($image->height())->isEqualTo(75) - // ->variable($image->sizer()->resizeToWidth())->isEqualTo(75) - // ->variable($image->sizer()->resizeToHeight())->isEqualTo(75) - // ->variable($image->sizer()->cropToWidth())->isNull() - // ->variable($image->sizer()->cropToHeight())->isNull() - // ; - // } + public function testPortraitWithSquareImage() + { + $this + ->given($image = $this->image(__DIR__.'/../200x200.jpg',75,300)) + ->integer($image->width())->isEqualTo(75) + ->integer($image->height())->isEqualTo(300) + ->variable($image->sizer()->resizeToWidth())->isEqualTo(300) + ->variable($image->sizer()->resizeToHeight())->isEqualTo(300) + ->variable($image->sizer()->cropToWidth())->isEqualTo(75) + ->variable($image->sizer()->cropToHeight())->isEqualTo(300) + ; + } - // public function testPortraitWithLandscapeImage() - // { - // $this - // ->given($image = $this->image(__DIR__.'/../200x100.jpg',100,200)) - // ->integer($image->width())->isEqualTo(100) - // ->integer($image->height())->isEqualTo(50) - // ->variable($image->sizer()->resizeToWidth())->isEqualTo(100) - // ->variable($image->sizer()->resizeToHeight())->isEqualTo(50) - // ->variable($image->sizer()->cropToWidth())->isNull() - // ->variable($image->sizer()->cropToHeight())->isNull() - // ; - // } + public function testPortraitWithLandscapeImage() + { + $this + ->given($image = $this->image(__DIR__.'/../200x100.jpg',100,200)) + ->integer($image->width())->isEqualTo(100) + ->integer($image->height())->isEqualTo(200) + ->variable($image->sizer()->resizeToWidth())->isEqualTo(400) + ->variable($image->sizer()->resizeToHeight())->isEqualTo(200) + ->variable($image->sizer()->cropToWidth())->isEqualTo(100) + ->variable($image->sizer()->cropToHeight())->isEqualTo(200) + ; + } - // public function testLandscapeWhenImageIsTaller() - // { - // $this - // ->given($image = $this->image(__DIR__.'/../200x100.jpg',150,100)) - // ->integer($image->width())->isEqualTo(150) - // ->integer($image->height())->isEqualTo(75) - // ->variable($image->sizer()->resizeToWidth())->isEqualTo(150) - // ->variable($image->sizer()->resizeToHeight())->isEqualTo(75) - // ->variable($image->sizer()->cropToWidth())->isNull() - // ->variable($image->sizer()->cropToHeight())->isNull() - // ; - // } + public function testLandscapeWhenImageIsTaller() + { + $this + ->given($image = $this->image(__DIR__.'/../200x100.jpg',150,100)) + ->integer($image->width())->isEqualTo(150) + ->integer($image->height())->isEqualTo(100) + ->variable($image->sizer()->resizeToWidth())->isEqualTo(200) + ->variable($image->sizer()->resizeToHeight())->isEqualTo(100) + ->variable($image->sizer()->cropToWidth())->isEqualTo(150) + ->variable($image->sizer()->cropToHeight())->isEqualTo(100) + ; + } - // public function testLandscapeWhenImageIsShorter() - // { - // $this - // ->given($image = $this->image(__DIR__.'/../200x100.jpg',300,75)) - // ->integer($image->width())->isEqualTo(150) - // ->integer($image->height())->isEqualTo(75) - // ->variable($image->sizer()->resizeToWidth())->isEqualTo(150) - // ->variable($image->sizer()->resizeToHeight())->isEqualTo(75) - // ->variable($image->sizer()->cropToWidth())->isNull() - // ->variable($image->sizer()->cropToHeight())->isNull() - // ; - // } + public function testLandscapeWhenImageIsShorter() + { + $this + ->given($image = $this->image(__DIR__.'/../200x100.jpg',300,75)) + ->integer($image->width())->isEqualTo(300) + ->integer($image->height())->isEqualTo(75) + ->variable($image->sizer()->resizeToWidth())->isEqualTo(300) + ->variable($image->sizer()->resizeToHeight())->isEqualTo(150) + ->variable($image->sizer()->cropToWidth())->isEqualTo(300) + ->variable($image->sizer()->cropToHeight())->isEqualTo(75) + ; + } - // public function testLandscapeWithSquareImage() - // { - // $this - // ->given($image = $this->image(__DIR__.'/../200x200.jpg',300,75)) - // ->integer($image->width())->isEqualTo(75) - // ->integer($image->height())->isEqualTo(75) - // ->variable($image->sizer()->resizeToWidth())->isEqualTo(75) - // ->variable($image->sizer()->resizeToHeight())->isEqualTo(75) - // ->variable($image->sizer()->cropToWidth())->isNull() - // ->variable($image->sizer()->cropToHeight())->isNull() - // ; - // } + public function testLandscapeWithSquareImage() + { + $this + ->given($image = $this->image(__DIR__.'/../200x200.jpg',300,75)) + ->integer($image->width())->isEqualTo(300) + ->integer($image->height())->isEqualTo(75) + ->variable($image->sizer()->resizeToWidth())->isEqualTo(300) + ->variable($image->sizer()->resizeToHeight())->isEqualTo(300) + ->variable($image->sizer()->cropToWidth())->isEqualTo(300) + ->variable($image->sizer()->cropToHeight())->isEqualTo(75) + ; + } - // public function testLandscapeWithPortraitImage() - // { - // $this - // ->given($image = $this->image(__DIR__.'/../100x200.jpg',200,100)) - // ->integer($image->width())->isEqualTo(50) - // ->integer($image->height())->isEqualTo(100) - // ->variable($image->sizer()->resizeToWidth())->isEqualTo(50) - // ->variable($image->sizer()->resizeToHeight())->isEqualTo(100) - // ->variable($image->sizer()->cropToWidth())->isNull() - // ->variable($image->sizer()->cropToHeight())->isNull() - // ; - // } + public function testLandscapeWithPortraitImage() + { + $this + ->given($image = $this->image(__DIR__.'/../100x200.jpg',200,100)) + ->integer($image->width())->isEqualTo(200) + ->integer($image->height())->isEqualTo(100) + ->variable($image->sizer()->resizeToWidth())->isEqualTo(200) + ->variable($image->sizer()->resizeToHeight())->isEqualTo(400) + ->variable($image->sizer()->cropToWidth())->isEqualTo(200) + ->variable($image->sizer()->cropToHeight())->isEqualTo(100) + ; + } - // public function testWithSmallSquareImage() - // { - // $this - // ->given($image = $this->image(__DIR__.'/../200x200.jpg',300,300)) - // ->integer($image->width())->isEqualTo(300) - // ->integer($image->height())->isEqualTo(300) - // ->variable($image->sizer()->resizeToWidth())->isEqualTo(300) - // ->variable($image->sizer()->resizeToHeight())->isEqualTo(300) - // ->variable($image->sizer()->cropToWidth())->isNull() - // ->variable($image->sizer()->cropToHeight())->isNull() - // ; - // } + public function testWithSmallSquareImage() + { + $this + ->given($image = $this->image(__DIR__.'/../200x200.jpg',300,300)) + ->integer($image->width())->isEqualTo(300) + ->integer($image->height())->isEqualTo(300) + ->variable($image->sizer()->resizeToWidth())->isEqualTo(300) + ->variable($image->sizer()->resizeToHeight())->isEqualTo(300) + ->variable($image->sizer()->cropToWidth())->isEqualTo(300) + ->variable($image->sizer()->cropToHeight())->isEqualTo(300) + ; + } - // public function testWithSmallLandscapeImage() - // { - // $this - // ->given($image = $this->image(__DIR__.'/../200x100.jpg',300,300)) - // ->integer($image->width())->isEqualTo(300) - // ->integer($image->height())->isEqualTo(150) - // ->variable($image->sizer()->resizeToWidth())->isEqualTo(300) - // ->variable($image->sizer()->resizeToHeight())->isEqualTo(150) - // ->variable($image->sizer()->cropToWidth())->isNull() - // ->variable($image->sizer()->cropToHeight())->isNull() - // ; - // } + public function testWithSmallLandscapeImage() + { + $this + ->given($image = $this->image(__DIR__.'/../200x100.jpg',300,300)) + ->integer($image->width())->isEqualTo(300) + ->integer($image->height())->isEqualTo(300) + ->variable($image->sizer()->resizeToWidth())->isEqualTo(600) + ->variable($image->sizer()->resizeToHeight())->isEqualTo(300) + ->variable($image->sizer()->cropToWidth())->isEqualTo(300) + ->variable($image->sizer()->cropToHeight())->isEqualTo(300) + ; + } - // public function testWithSmallPortraitImage() - // { - // $this - // ->given($image = $this->image(__DIR__.'/../100x200.jpg',300,300)) - // ->integer($image->width())->isEqualTo(150) - // ->integer($image->height())->isEqualTo(300) - // ->variable($image->sizer()->resizeToWidth())->isEqualTo(150) - // ->variable($image->sizer()->resizeToHeight())->isEqualTo(300) - // ->variable($image->sizer()->cropToWidth())->isNull() - // ->variable($image->sizer()->cropToHeight())->isNull() - // ; - // } + public function testWithSmallPortraitImage() + { + $this + ->given($image = $this->image(__DIR__.'/../100x200.jpg',300,300)) + ->integer($image->width())->isEqualTo(300) + ->integer($image->height())->isEqualTo(300) + ->variable($image->sizer()->resizeToWidth())->isEqualTo(300) + ->variable($image->sizer()->resizeToHeight())->isEqualTo(600) + ->variable($image->sizer()->cropToWidth())->isEqualTo(300) + ->variable($image->sizer()->cropToHeight())->isEqualTo(300) + ; + } protected function image(string $path, int $width, int $height) {