Skip to content

Commit

Permalink
Merge pull request #18 from JeffAlyanak/gravity-command
Browse files Browse the repository at this point in the history
Added gravity to Command
  • Loading branch information
Pierstoval authored Dec 12, 2019
2 parents defabc8 + 8e5d443 commit 0712b72
Show file tree
Hide file tree
Showing 6 changed files with 202 additions and 0 deletions.
14 changes: 14 additions & 0 deletions Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
namespace Orbitale\Component\ImageMagick;

use Orbitale\Component\ImageMagick\ReferenceClasses\Geometry;
use Orbitale\Component\ImageMagick\ReferenceClasses\Gravity;
use Symfony\Component\Process\ExecutableFinder;
use Symfony\Component\Process\Process;

Expand Down Expand Up @@ -430,6 +431,19 @@ public function extent($geometry): self
return $this;
}

/**
* @param string $gravity
*
* @see https://www.imagemagick.org/script/command-line-options.php#gravity
*/
public function gravity($gravity): self
{
$this->command[] = '-gravity';
$this->command[] = '"'.$this->ref->gravity($gravity).'"';

return $this;
}

/**
* @param string|Geometry $geometry
*
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ This is why a "few" ones are implemented now, to make sure validation is possibl
* [`-fill`](http://www.imagemagick.org/script/command-line-options.php#fill)
* [`-font`](http://www.imagemagick.org/script/command-line-options.php#font)
* [`-gaussian-blur`](http://www.imagemagick.org/script/command-line-options.php#gaussian-blur)
* [`-gravity`](http://www.imagemagick.org/script/command-line-options.php#gravity)
* [`-interlace`](http://www.imagemagick.org/script/command-line-options.php#interlace)
* [`-pointsize`](http://www.imagemagick.org/script/command-line-options.php#pointsize)
* [`-quality`](http://www.imagemagick.org/script/command-line-options.php#quality)
Expand Down
55 changes: 55 additions & 0 deletions ReferenceClasses/Gravity.php
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;
}
}
13 changes: 13 additions & 0 deletions References.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
namespace Orbitale\Component\ImageMagick;

use Orbitale\Component\ImageMagick\ReferenceClasses\Geometry;
use Orbitale\Component\ImageMagick\ReferenceClasses\Gravity;

/**
* This class is here to add some validation processes when using options in the Command class.
Expand Down Expand Up @@ -95,6 +96,18 @@ public function geometry($geometry): string
return $geometry->validate();
}

/**
* @param string|Gravity $gravity
*/
public function gravity($gravity): string
{
if (!$gravity instanceof Gravity) {
$gravity = new Gravity(\trim($gravity));
}

return $gravity->validate();
}

/**
* Checks that a color is correct according to ImageMagick command line reference.
*
Expand Down
23 changes: 23 additions & 0 deletions Tests/CommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,29 @@ public function testColorspaceImage(): void
$this->testConvertIdentifyImage($imageOutput, 'JPEG', '180x170+0+0', '8-bit');
}

public function testGravityCommand(): void
{
$command = new Command(IMAGEMAGICK_DIR);

$imageToResize = $this->resourcesDir.'/moon_180.jpg';
$imageOutput = $this->resourcesDir.'/outputs/moon.jpg';
static::assertFileExists($imageToResize);

$response = $command
->convert($imageToResize)
->gravity('Center')
->extent('100x100')
->file($imageOutput, false)
->run()
;

static::assertFalse($response->hasFailed(), "Errors when testing:\n".$response->getProcess()->getOutput()."\t".$response->getProcess()->getErrorOutput());

static::assertFileExists($this->resourcesDir.'/outputs/moon.jpg');

$this->testConvertIdentifyImage($imageOutput, 'JPEG', '100x100+0+0', '8-bit');
}

/**
* @dataProvider provideImagesToIdentify
*/
Expand Down
96 changes: 96 additions & 0 deletions Tests/GravityTest.php
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 => [" "];
}
}

0 comments on commit 0712b72

Please sign in to comment.