From 58e2855af056c97dd177d05c6a85c05bb8db6199 Mon Sep 17 00:00:00 2001 From: Jeff Alyanak Date: Wed, 11 Dec 2019 04:46:08 -0500 Subject: [PATCH 01/10] Added gravity to Command Added gravity option to command, reference. Added a reference class for gravity which can validate the gravity parameter provided. It's possible this may need to be updated or altered for different versions of imagemagick, but currently conforms with the docs provided (https://www.imagemagick.org/script/command-line-options.php#gravity). --- Command.php | 14 +++++++++ README.md | 1 + ReferenceClasses/Gravity.php | 55 ++++++++++++++++++++++++++++++++++++ References.php | 13 +++++++++ 4 files changed, 83 insertions(+) create mode 100644 ReferenceClasses/Gravity.php diff --git a/Command.php b/Command.php index a2516e6..f5653d3 100644 --- a/Command.php +++ b/Command.php @@ -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; @@ -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 * diff --git a/README.md b/README.md index ec77a47..6ba53a9 100644 --- a/README.md +++ b/README.md @@ -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) diff --git a/ReferenceClasses/Gravity.php b/ReferenceClasses/Gravity.php new file mode 100644 index 0000000..581f865 --- /dev/null +++ b/ReferenceClasses/Gravity.php @@ -0,0 +1,55 @@ +value = $gravity; + } + + public function __toString() + { + return (string) $this->value; + } + + public function validate(): string + { + if ( !in_array($this->value, $this->validGravity) ) { + throw new \InvalidArgumentException(\sprintf( + "Invalid gravity option, \"%s\" given.\nAvailable: %s", + $gravity, \implode(', ', self::$validGravity) + )); + } + + return $this->value; + } +} diff --git a/References.php b/References.php index e59b06f..610b810 100644 --- a/References.php +++ b/References.php @@ -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. @@ -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. * From 051e83ea19ba98dea9a9a4482c14c84185cbd5ce Mon Sep 17 00:00:00 2001 From: Jeff Alyanak Date: Wed, 11 Dec 2019 06:53:19 -0500 Subject: [PATCH 02/10] Fixed exception strings Exception strings were not calling statics correctly. --- ReferenceClasses/Gravity.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ReferenceClasses/Gravity.php b/ReferenceClasses/Gravity.php index 581f865..f8d8abf 100644 --- a/ReferenceClasses/Gravity.php +++ b/ReferenceClasses/Gravity.php @@ -43,10 +43,10 @@ public function __toString() public function validate(): string { - if ( !in_array($this->value, $this->validGravity) ) { + if ( !in_array($this->value, self::$validGravity) ) { throw new \InvalidArgumentException(\sprintf( "Invalid gravity option, \"%s\" given.\nAvailable: %s", - $gravity, \implode(', ', self::$validGravity) + $this->value, \implode(', ', self::$validGravity) )); } From dd287f0c92f6998235c5801c5bafce6594344a42 Mon Sep 17 00:00:00 2001 From: Jeff Alyanak Date: Wed, 11 Dec 2019 06:54:34 -0500 Subject: [PATCH 03/10] Added test for Gravity ref class. --- Tests/GravityTest.php | 114 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 114 insertions(+) create mode 100644 Tests/GravityTest.php diff --git a/Tests/GravityTest.php b/Tests/GravityTest.php new file mode 100644 index 0000000..c0b4af1 --- /dev/null +++ b/Tests/GravityTest.php @@ -0,0 +1,114 @@ +validate(); + } catch (\Exception $e) { + static::fail($e->getMessage()); + + return; + } + + static::assertIsString($validatedGravity); + static::assertNotEmpty($validatedGravity); + + if ('' === $validatedGravity) { + static::markTestSkipped('No gravity to check. ['.$validatedGravity.'] ['.\implode(',', \func_get_args()).']'); + } + + $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 + { + $testGravity = new Gravity($gravity); + + $message = null; + + try { + $testGravity->validate(); + } catch (\InvalidArgumentException $e) { + $message = $e->getMessage(); + } + + static::assertNotNull($message, 'No exception for gravity "'.$gravity.'"'); + static::assertStringStartsWith('Invalid gravity option, "'.$gravity.'" given.', $message, "Wrong exception message:\n$message"); + } + + 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 => [" "]; + } +} From 7a6fe0fbf3dc35068587bd3c8e60c60c82288644 Mon Sep 17 00:00:00 2001 From: Jeff Alyanak Date: Wed, 11 Dec 2019 06:58:47 -0500 Subject: [PATCH 04/10] Added test of gravity command. --- Tests/CommandTest.php | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/Tests/CommandTest.php b/Tests/CommandTest.php index 35a69a0..0954acc 100644 --- a/Tests/CommandTest.php +++ b/Tests/CommandTest.php @@ -140,6 +140,31 @@ 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'); + + static::assertFalse($response->hasFailed()); + + $this->testConvertIdentifyImage($imageOutput, 'JPEG', '100x100+0+0', '8-bit'); + } + /** * @dataProvider provideImagesToIdentify */ From 6d32748a13d096eaedd698b27b27d54e0d53f00b Mon Sep 17 00:00:00 2001 From: Jeff Alyanak Date: Thu, 12 Dec 2019 05:32:54 -0500 Subject: [PATCH 05/10] Strict in_array check. --- ReferenceClasses/Gravity.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ReferenceClasses/Gravity.php b/ReferenceClasses/Gravity.php index f8d8abf..c5a7a16 100644 --- a/ReferenceClasses/Gravity.php +++ b/ReferenceClasses/Gravity.php @@ -43,7 +43,7 @@ public function __toString() public function validate(): string { - if ( !in_array($this->value, self::$validGravity) ) { + if (!in_array($this->value, self::$validGravity, true)) { throw new \InvalidArgumentException(\sprintf( "Invalid gravity option, \"%s\" given.\nAvailable: %s", $this->value, \implode(', ', self::$validGravity) From bf170e5087060a8eee3a833bd240e2eb81e7a2ca Mon Sep 17 00:00:00 2001 From: Jeff Alyanak Date: Thu, 12 Dec 2019 05:36:06 -0500 Subject: [PATCH 06/10] Replaced hard tabs with soft. --- Tests/CommandTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Tests/CommandTest.php b/Tests/CommandTest.php index 0954acc..349a312 100644 --- a/Tests/CommandTest.php +++ b/Tests/CommandTest.php @@ -151,7 +151,7 @@ public function testGravityCommand(): void $response = $command ->convert($imageToResize) ->gravity('Center') - ->extent('100x100') + ->extent('100x100') ->file($imageOutput, false) ->run() ; From 197908ef3661122910a2115b20ce5347665f57c8 Mon Sep 17 00:00:00 2001 From: Jeff Alyanak Date: Thu, 12 Dec 2019 05:38:02 -0500 Subject: [PATCH 07/10] Removed useless & Redundant Tests --- Tests/CommandTest.php | 2 -- Tests/GravityTest.php | 4 ---- 2 files changed, 6 deletions(-) diff --git a/Tests/CommandTest.php b/Tests/CommandTest.php index 349a312..814a7e0 100644 --- a/Tests/CommandTest.php +++ b/Tests/CommandTest.php @@ -160,8 +160,6 @@ public function testGravityCommand(): void static::assertFileExists($this->resourcesDir.'/outputs/moon.jpg'); - static::assertFalse($response->hasFailed()); - $this->testConvertIdentifyImage($imageOutput, 'JPEG', '100x100+0+0', '8-bit'); } diff --git a/Tests/GravityTest.php b/Tests/GravityTest.php index c0b4af1..5542f72 100644 --- a/Tests/GravityTest.php +++ b/Tests/GravityTest.php @@ -29,10 +29,6 @@ public function testGravity($gravity): void static::assertIsString($validatedGravity); static::assertNotEmpty($validatedGravity); - if ('' === $validatedGravity) { - static::markTestSkipped('No gravity to check. ['.$validatedGravity.'] ['.\implode(',', \func_get_args()).']'); - } - $command = new Command(IMAGEMAGICK_DIR); $outputFile = $this->resourcesDir.'/outputs/moon_180_test_gravity_'.\md5($gravity.'test_geo').'.jpg'; From 1a10dd2da3742880d863dd53b79a7cd0ef075063 Mon Sep 17 00:00:00 2001 From: Jeff Alyanak Date: Thu, 12 Dec 2019 05:46:24 -0500 Subject: [PATCH 08/10] Simplified exception tests for Gravity validatation. In future the testWrongGeometry function in the Tests/GeometryTest.php can also be cleaned up in a similar way. --- Tests/GravityTest.php | 15 ++++----------- 1 file changed, 4 insertions(+), 11 deletions(-) diff --git a/Tests/GravityTest.php b/Tests/GravityTest.php index 5542f72..8d4b7a9 100644 --- a/Tests/GravityTest.php +++ b/Tests/GravityTest.php @@ -76,18 +76,11 @@ public function provideValidGravities(): ?\Generator */ 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); - - $message = null; - - try { - $testGravity->validate(); - } catch (\InvalidArgumentException $e) { - $message = $e->getMessage(); - } - - static::assertNotNull($message, 'No exception for gravity "'.$gravity.'"'); - static::assertStringStartsWith('Invalid gravity option, "'.$gravity.'" given.', $message, "Wrong exception message:\n$message"); + $testGravity->validate(); } public function provideWrongGravities(): ?\Generator From 31a8eeb50ce5722a54639863cac8a71b695b9821 Mon Sep 17 00:00:00 2001 From: Jeff Alyanak Date: Thu, 12 Dec 2019 05:50:30 -0500 Subject: [PATCH 09/10] Removed redundant try/catch. Leaving this to phpunit to catch. --- Tests/GravityTest.php | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/Tests/GravityTest.php b/Tests/GravityTest.php index 8d4b7a9..6a59ff1 100644 --- a/Tests/GravityTest.php +++ b/Tests/GravityTest.php @@ -18,13 +18,7 @@ public function testGravity($gravity): void { $gravity = new Gravity($gravity); - try { - $validatedGravity = $gravity->validate(); - } catch (\Exception $e) { - static::fail($e->getMessage()); - - return; - } + $validatedGravity = $gravity->validate(); static::assertIsString($validatedGravity); static::assertNotEmpty($validatedGravity); From 8e5d4438034fc8608b2123f45692de31da1ba265 Mon Sep 17 00:00:00 2001 From: Jeff Alyanak Date: Thu, 12 Dec 2019 06:02:47 -0500 Subject: [PATCH 10/10] Removed redundant assertIsString Unless the return type of of Gravity class' validate function is changed, this will always be a string. --- Tests/GravityTest.php | 1 - 1 file changed, 1 deletion(-) diff --git a/Tests/GravityTest.php b/Tests/GravityTest.php index 6a59ff1..127f57f 100644 --- a/Tests/GravityTest.php +++ b/Tests/GravityTest.php @@ -20,7 +20,6 @@ public function testGravity($gravity): void $validatedGravity = $gravity->validate(); - static::assertIsString($validatedGravity); static::assertNotEmpty($validatedGravity); $command = new Command(IMAGEMAGICK_DIR);