-
-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #18 from JeffAlyanak/gravity-command
Added gravity to Command
- Loading branch information
Showing
6 changed files
with
202 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Orbitale\Component\ImageMagick\ReferenceClasses; | ||
|
||
/** | ||
* Represents an ImageMagick gravity parameter. | ||
* | ||
* @see https://www.imagemagick.org/script/command-line-options.php#gravity | ||
*/ | ||
class Gravity | ||
{ | ||
private static $validGravity = [ | ||
"NorthWest", | ||
"North", | ||
"NorthEast", | ||
"West", | ||
"Center", | ||
"East", | ||
"SouthWest", | ||
"South", | ||
"SouthEast" | ||
]; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
private $value; | ||
|
||
public static function createFromParameters(string $gravity): string { | ||
return $gravity; | ||
} | ||
|
||
public function __construct(string $gravity) { | ||
$this->value = $gravity; | ||
} | ||
|
||
public function __toString() | ||
{ | ||
return (string) $this->value; | ||
} | ||
|
||
public function validate(): string | ||
{ | ||
if (!in_array($this->value, self::$validGravity, true)) { | ||
throw new \InvalidArgumentException(\sprintf( | ||
"Invalid gravity option, \"%s\" given.\nAvailable: %s", | ||
$this->value, \implode(', ', self::$validGravity) | ||
)); | ||
} | ||
|
||
return $this->value; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Orbitale\Component\ImageMagick\Tests; | ||
|
||
use Orbitale\Component\ImageMagick\Command; | ||
use Orbitale\Component\ImageMagick\ReferenceClasses\Gravity; | ||
|
||
class GravityTest extends AbstractTestCase | ||
{ | ||
/** | ||
* @param string $gravity | ||
* | ||
* @dataProvider provideValidGravities | ||
*/ | ||
public function testGravity($gravity): void | ||
{ | ||
$gravity = new Gravity($gravity); | ||
|
||
$validatedGravity = $gravity->validate(); | ||
|
||
static::assertNotEmpty($validatedGravity); | ||
|
||
$command = new Command(IMAGEMAGICK_DIR); | ||
|
||
$outputFile = $this->resourcesDir.'/outputs/moon_180_test_gravity_'.\md5($gravity.'test_geo').'.jpg'; | ||
|
||
if (\file_exists($outputFile)) { | ||
\unlink($outputFile); | ||
} | ||
|
||
$command | ||
->convert($this->resourcesDir.'/moon_180.jpg') | ||
->gravity($gravity) | ||
->resize('100x100') | ||
->file($outputFile, false) | ||
; | ||
|
||
$response = $command->run(); | ||
|
||
static::assertTrue($response->isSuccessful(), \sprintf( | ||
"Gravity fixture \"%s\" returned an ImageMagick error.\nFull command: %s\nErrors:\n%s\n%s", | ||
$gravity->validate(), | ||
$command->getCommand(), | ||
$response->getOutput(), | ||
$response->getError() | ||
)); | ||
static::assertFileExists($outputFile); | ||
} | ||
|
||
public function provideValidGravities(): ?\Generator | ||
{ | ||
yield 0 => ["NorthWest"]; | ||
yield 1 => ["North"]; | ||
yield 2 => ["NorthEast"]; | ||
yield 3 => ["West"]; | ||
yield 4 => ["Center"]; | ||
yield 5 => ["East"]; | ||
yield 6 => ["SouthWest"]; | ||
yield 7 => ["South"]; | ||
yield 8 => ["SouthEast"]; | ||
} | ||
|
||
/** | ||
* @param string $gravity | ||
* | ||
* @dataProvider provideWrongGravities | ||
*/ | ||
public function testWrongGravities($gravity): void | ||
{ | ||
$this->expectException(\InvalidArgumentException::class); | ||
$this->expectExceptionMessage("Invalid gravity option, \"" .$gravity. "\" given.\nAvailable: NorthWest, North, NorthEast, West, Center, East, SouthWest, South, SouthEast" ); | ||
|
||
$testGravity = new Gravity($gravity); | ||
$testGravity->validate(); | ||
} | ||
|
||
public function provideWrongGravities(): ?\Generator | ||
{ | ||
yield 0 => ["Northwest"]; | ||
yield 1 => ["northwest"]; | ||
yield 2 => ["north"]; | ||
yield 3 => ["northEast"]; | ||
yield 4 => ["Northeast"]; | ||
yield 5 => ["west"]; | ||
yield 6 => ["center"]; | ||
yield 7 => ["east"]; | ||
yield 8 => ["southwest"]; | ||
yield 9 => ["south"]; | ||
yield 10 => ["southeast"]; | ||
yield 11 => ["Middle"]; | ||
yield 12 => [""]; | ||
yield 13 => [" "]; | ||
} | ||
} |