Updated tests, Crop and Cover now pretty well covered I bet

This commit is contained in:
Joby Elliott 2020-10-01 13:05:16 -06:00
parent 8686f9ddd7
commit 3ce4c29671
4 changed files with 144 additions and 174 deletions

View file

@ -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

View file

@ -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'];
}

View file

@ -1,44 +0,0 @@
<?php
/* image-transform | https://github.com/jobyone/image-transform | MIT License */
namespace ByJoby\ImageTransform\Sizers;
class Crop extends AbstractSizer
{
protected $width, $height;
public function __construct(int $width, int $height)
{
$this->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;
}
}

View file

@ -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)
{