Skip to content

Commit 0712b72

Browse files
authored
Merge pull request #18 from JeffAlyanak/gravity-command
Added gravity to Command
2 parents defabc8 + 8e5d443 commit 0712b72

File tree

6 files changed

+202
-0
lines changed

6 files changed

+202
-0
lines changed

Command.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
namespace Orbitale\Component\ImageMagick;
1515

1616
use Orbitale\Component\ImageMagick\ReferenceClasses\Geometry;
17+
use Orbitale\Component\ImageMagick\ReferenceClasses\Gravity;
1718
use Symfony\Component\Process\ExecutableFinder;
1819
use Symfony\Component\Process\Process;
1920

@@ -430,6 +431,19 @@ public function extent($geometry): self
430431
return $this;
431432
}
432433

434+
/**
435+
* @param string $gravity
436+
*
437+
* @see https://www.imagemagick.org/script/command-line-options.php#gravity
438+
*/
439+
public function gravity($gravity): self
440+
{
441+
$this->command[] = '-gravity';
442+
$this->command[] = '"'.$this->ref->gravity($gravity).'"';
443+
444+
return $this;
445+
}
446+
433447
/**
434448
* @param string|Geometry $geometry
435449
*

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@ This is why a "few" ones are implemented now, to make sure validation is possibl
153153
* [`-fill`](http://www.imagemagick.org/script/command-line-options.php#fill)
154154
* [`-font`](http://www.imagemagick.org/script/command-line-options.php#font)
155155
* [`-gaussian-blur`](http://www.imagemagick.org/script/command-line-options.php#gaussian-blur)
156+
* [`-gravity`](http://www.imagemagick.org/script/command-line-options.php#gravity)
156157
* [`-interlace`](http://www.imagemagick.org/script/command-line-options.php#interlace)
157158
* [`-pointsize`](http://www.imagemagick.org/script/command-line-options.php#pointsize)
158159
* [`-quality`](http://www.imagemagick.org/script/command-line-options.php#quality)

ReferenceClasses/Gravity.php

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Orbitale\Component\ImageMagick\ReferenceClasses;
6+
7+
/**
8+
* Represents an ImageMagick gravity parameter.
9+
*
10+
* @see https://www.imagemagick.org/script/command-line-options.php#gravity
11+
*/
12+
class Gravity
13+
{
14+
private static $validGravity = [
15+
"NorthWest",
16+
"North",
17+
"NorthEast",
18+
"West",
19+
"Center",
20+
"East",
21+
"SouthWest",
22+
"South",
23+
"SouthEast"
24+
];
25+
26+
/**
27+
* @var string
28+
*/
29+
private $value;
30+
31+
public static function createFromParameters(string $gravity): string {
32+
return $gravity;
33+
}
34+
35+
public function __construct(string $gravity) {
36+
$this->value = $gravity;
37+
}
38+
39+
public function __toString()
40+
{
41+
return (string) $this->value;
42+
}
43+
44+
public function validate(): string
45+
{
46+
if (!in_array($this->value, self::$validGravity, true)) {
47+
throw new \InvalidArgumentException(\sprintf(
48+
"Invalid gravity option, \"%s\" given.\nAvailable: %s",
49+
$this->value, \implode(', ', self::$validGravity)
50+
));
51+
}
52+
53+
return $this->value;
54+
}
55+
}

References.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
namespace Orbitale\Component\ImageMagick;
1515

1616
use Orbitale\Component\ImageMagick\ReferenceClasses\Geometry;
17+
use Orbitale\Component\ImageMagick\ReferenceClasses\Gravity;
1718

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

99+
/**
100+
* @param string|Gravity $gravity
101+
*/
102+
public function gravity($gravity): string
103+
{
104+
if (!$gravity instanceof Gravity) {
105+
$gravity = new Gravity(\trim($gravity));
106+
}
107+
108+
return $gravity->validate();
109+
}
110+
98111
/**
99112
* Checks that a color is correct according to ImageMagick command line reference.
100113
*

Tests/CommandTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,29 @@ public function testColorspaceImage(): void
140140
$this->testConvertIdentifyImage($imageOutput, 'JPEG', '180x170+0+0', '8-bit');
141141
}
142142

143+
public function testGravityCommand(): void
144+
{
145+
$command = new Command(IMAGEMAGICK_DIR);
146+
147+
$imageToResize = $this->resourcesDir.'/moon_180.jpg';
148+
$imageOutput = $this->resourcesDir.'/outputs/moon.jpg';
149+
static::assertFileExists($imageToResize);
150+
151+
$response = $command
152+
->convert($imageToResize)
153+
->gravity('Center')
154+
->extent('100x100')
155+
->file($imageOutput, false)
156+
->run()
157+
;
158+
159+
static::assertFalse($response->hasFailed(), "Errors when testing:\n".$response->getProcess()->getOutput()."\t".$response->getProcess()->getErrorOutput());
160+
161+
static::assertFileExists($this->resourcesDir.'/outputs/moon.jpg');
162+
163+
$this->testConvertIdentifyImage($imageOutput, 'JPEG', '100x100+0+0', '8-bit');
164+
}
165+
143166
/**
144167
* @dataProvider provideImagesToIdentify
145168
*/

Tests/GravityTest.php

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Orbitale\Component\ImageMagick\Tests;
6+
7+
use Orbitale\Component\ImageMagick\Command;
8+
use Orbitale\Component\ImageMagick\ReferenceClasses\Gravity;
9+
10+
class GravityTest extends AbstractTestCase
11+
{
12+
/**
13+
* @param string $gravity
14+
*
15+
* @dataProvider provideValidGravities
16+
*/
17+
public function testGravity($gravity): void
18+
{
19+
$gravity = new Gravity($gravity);
20+
21+
$validatedGravity = $gravity->validate();
22+
23+
static::assertNotEmpty($validatedGravity);
24+
25+
$command = new Command(IMAGEMAGICK_DIR);
26+
27+
$outputFile = $this->resourcesDir.'/outputs/moon_180_test_gravity_'.\md5($gravity.'test_geo').'.jpg';
28+
29+
if (\file_exists($outputFile)) {
30+
\unlink($outputFile);
31+
}
32+
33+
$command
34+
->convert($this->resourcesDir.'/moon_180.jpg')
35+
->gravity($gravity)
36+
->resize('100x100')
37+
->file($outputFile, false)
38+
;
39+
40+
$response = $command->run();
41+
42+
static::assertTrue($response->isSuccessful(), \sprintf(
43+
"Gravity fixture \"%s\" returned an ImageMagick error.\nFull command: %s\nErrors:\n%s\n%s",
44+
$gravity->validate(),
45+
$command->getCommand(),
46+
$response->getOutput(),
47+
$response->getError()
48+
));
49+
static::assertFileExists($outputFile);
50+
}
51+
52+
public function provideValidGravities(): ?\Generator
53+
{
54+
yield 0 => ["NorthWest"];
55+
yield 1 => ["North"];
56+
yield 2 => ["NorthEast"];
57+
yield 3 => ["West"];
58+
yield 4 => ["Center"];
59+
yield 5 => ["East"];
60+
yield 6 => ["SouthWest"];
61+
yield 7 => ["South"];
62+
yield 8 => ["SouthEast"];
63+
}
64+
65+
/**
66+
* @param string $gravity
67+
*
68+
* @dataProvider provideWrongGravities
69+
*/
70+
public function testWrongGravities($gravity): void
71+
{
72+
$this->expectException(\InvalidArgumentException::class);
73+
$this->expectExceptionMessage("Invalid gravity option, \"" .$gravity. "\" given.\nAvailable: NorthWest, North, NorthEast, West, Center, East, SouthWest, South, SouthEast" );
74+
75+
$testGravity = new Gravity($gravity);
76+
$testGravity->validate();
77+
}
78+
79+
public function provideWrongGravities(): ?\Generator
80+
{
81+
yield 0 => ["Northwest"];
82+
yield 1 => ["northwest"];
83+
yield 2 => ["north"];
84+
yield 3 => ["northEast"];
85+
yield 4 => ["Northeast"];
86+
yield 5 => ["west"];
87+
yield 6 => ["center"];
88+
yield 7 => ["east"];
89+
yield 8 => ["southwest"];
90+
yield 9 => ["south"];
91+
yield 10 => ["southeast"];
92+
yield 11 => ["Middle"];
93+
yield 12 => [""];
94+
yield 13 => [" "];
95+
}
96+
}

0 commit comments

Comments
 (0)