Skip to content

Commit 8391456

Browse files
committed
Fix blur type issues, and add test for it
1 parent be47fef commit 8391456

File tree

3 files changed

+77
-5
lines changed

3 files changed

+77
-5
lines changed

BlurTest.php

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* This file is part of the OrbitaleImageMagickPHP package.
7+
*
8+
* (c) Alexandre Rock Ancelet <alex@orbitale.io>
9+
*
10+
* For the full copyright and license information, please view the LICENSE
11+
* file that was distributed with this source code.
12+
*/
13+
14+
namespace Orbitale\Component\ImageMagick\Tests\References;
15+
16+
use Orbitale\Component\ImageMagick\References;
17+
use PHPUnit\Framework\TestCase;
18+
19+
class BlurTest extends TestCase
20+
{
21+
/**
22+
* @dataProvider provideInvalidOptions
23+
*/
24+
public function testInvalidBlurOptions($invalidOption): void
25+
{
26+
$this->expectException(\InvalidArgumentException::class);
27+
$this->expectExceptionMessage('Gaussian blur must respect formats "{radius}" or "{radius}x{sigma}".'."\n"
28+
.'Please refer to ImageMagick command line documentation about the "-gaussian-blur" and "-blur" options:'."\n"
29+
.'http://www.imagemagick.org/script/command-line-options.php#blur'."\n"
30+
.'http://www.imagemagick.org/script/command-line-options.php#gaussian-blur');
31+
$this->getReferences()->blur($invalidOption);
32+
}
33+
34+
/**
35+
* @dataProvider provideValidOptions
36+
*/
37+
public function testValidBlurOptions($validOption, $expectedReturn): void
38+
{
39+
static::assertSame($expectedReturn, $this->getReferences()->blur($validOption));
40+
}
41+
42+
public function getReferences(): References
43+
{
44+
return new References();
45+
}
46+
47+
public function provideInvalidOptions(): \Generator
48+
{
49+
yield ['invalid'];
50+
yield ['axb'];
51+
yield [' a x b'];
52+
yield ['1x2x3'];
53+
yield ['1 x2'];
54+
yield ['1x 2'];
55+
}
56+
57+
public function provideValidOptions(): \Generator
58+
{
59+
yield [1, '1'];
60+
yield [-1, '-1'];
61+
yield ['1', '1'];
62+
yield ['-1', '-1'];
63+
yield ['1x2', '1x2'];
64+
yield [' 1x2 ', '1x2'];
65+
}
66+
}

src/Command.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -522,7 +522,7 @@ public function interlace(string $type): self
522522
/**
523523
* @see http://imagemagick.org/script/command-line-options.php#gaussian-blur
524524
*/
525-
public function gaussianBlur(string $blur): self
525+
public function gaussianBlur($blur): self
526526
{
527527
$this->command[] = '-gaussian-blur';
528528
$this->command[] = $this->ref->blur($blur);
@@ -533,7 +533,7 @@ public function gaussianBlur(string $blur): self
533533
/**
534534
* @see http://imagemagick.org/script/command-line-options.php#blur
535535
*/
536-
public function blur(string $blur): self
536+
public function blur($blur): self
537537
{
538538
$this->command[] = '-blur';
539539
$this->command[] = $this->ref->blur($blur);

src/References.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -185,12 +185,18 @@ public function rotation(string $rotation): string
185185
));
186186
}
187187

188-
public function blur(string $blur): float
188+
public function blur($blur): string
189189
{
190-
$blur = \trim($blur);
190+
if (\is_string($blur)) {
191+
$blur = \trim($blur);
192+
}
193+
194+
if (\is_numeric($blur)) {
195+
return (string) (float) $blur;
196+
}
191197

192198
if (\preg_match('~^\d+(?:\.\d+)?(?:x\d+(?:\.\d+)?)?$~', $blur)) {
193-
return (float) $blur;
199+
return (string) $blur;
194200
}
195201

196202
throw new \InvalidArgumentException(\sprintf(

0 commit comments

Comments
 (0)