Skip to content

Commit

Permalink
Fix blur type issues, and add test for it
Browse files Browse the repository at this point in the history
  • Loading branch information
Pierstoval committed Jan 13, 2021
1 parent be47fef commit 8391456
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 5 deletions.
66 changes: 66 additions & 0 deletions BlurTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

declare(strict_types=1);

/*
* This file is part of the OrbitaleImageMagickPHP package.
*
* (c) Alexandre Rock Ancelet <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Orbitale\Component\ImageMagick\Tests\References;

use Orbitale\Component\ImageMagick\References;
use PHPUnit\Framework\TestCase;

class BlurTest extends TestCase
{
/**
* @dataProvider provideInvalidOptions
*/
public function testInvalidBlurOptions($invalidOption): void
{
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('Gaussian blur must respect formats "{radius}" or "{radius}x{sigma}".'."\n"
.'Please refer to ImageMagick command line documentation about the "-gaussian-blur" and "-blur" options:'."\n"
.'http://www.imagemagick.org/script/command-line-options.php#blur'."\n"
.'http://www.imagemagick.org/script/command-line-options.php#gaussian-blur');
$this->getReferences()->blur($invalidOption);
}

/**
* @dataProvider provideValidOptions
*/
public function testValidBlurOptions($validOption, $expectedReturn): void
{
static::assertSame($expectedReturn, $this->getReferences()->blur($validOption));
}

public function getReferences(): References
{
return new References();
}

public function provideInvalidOptions(): \Generator
{
yield ['invalid'];
yield ['axb'];
yield [' a x b'];
yield ['1x2x3'];
yield ['1 x2'];
yield ['1x 2'];
}

public function provideValidOptions(): \Generator
{
yield [1, '1'];
yield [-1, '-1'];
yield ['1', '1'];
yield ['-1', '-1'];
yield ['1x2', '1x2'];
yield [' 1x2 ', '1x2'];
}
}
4 changes: 2 additions & 2 deletions src/Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -522,7 +522,7 @@ public function interlace(string $type): self
/**
* @see http://imagemagick.org/script/command-line-options.php#gaussian-blur
*/
public function gaussianBlur(string $blur): self
public function gaussianBlur($blur): self
{
$this->command[] = '-gaussian-blur';
$this->command[] = $this->ref->blur($blur);
Expand All @@ -533,7 +533,7 @@ public function gaussianBlur(string $blur): self
/**
* @see http://imagemagick.org/script/command-line-options.php#blur
*/
public function blur(string $blur): self
public function blur($blur): self
{
$this->command[] = '-blur';
$this->command[] = $this->ref->blur($blur);
Expand Down
12 changes: 9 additions & 3 deletions src/References.php
Original file line number Diff line number Diff line change
Expand Up @@ -185,12 +185,18 @@ public function rotation(string $rotation): string
));
}

public function blur(string $blur): float
public function blur($blur): string
{
$blur = \trim($blur);
if (\is_string($blur)) {
$blur = \trim($blur);
}

if (\is_numeric($blur)) {
return (string) (float) $blur;
}

if (\preg_match('~^\d+(?:\.\d+)?(?:x\d+(?:\.\d+)?)?$~', $blur)) {
return (float) $blur;
return (string) $blur;
}

throw new \InvalidArgumentException(\sprintf(
Expand Down

0 comments on commit 8391456

Please sign in to comment.