Skip to content

Commit defabc8

Browse files
authored
Merge pull request #16 from 70mmy/master
Added '-depth' '-flatten' and '-colorspace' options
2 parents 167af5e + af86aee commit defabc8

File tree

6 files changed

+253
-0
lines changed

6 files changed

+253
-0
lines changed

Command.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -562,6 +562,37 @@ public function autoOrient(): self
562562
return $this;
563563
}
564564

565+
/**
566+
* @see https://imagemagick.org/script/command-line-options.php#depth
567+
*/
568+
public function depth(int $depth): self
569+
{
570+
$this->command[] = '-depth ' . $depth;
571+
572+
return $this;
573+
}
574+
575+
/**
576+
* @see https://imagemagick.org/script/command-line-options.php#flatten
577+
*/
578+
public function flatten(): self
579+
{
580+
$this->command[] = '-flatten';
581+
582+
return $this;
583+
}
584+
585+
/**
586+
* @see https://imagemagick.org/script/command-line-options.php#colorspace
587+
*/
588+
public function colorspace(string $colorspace): self
589+
{
590+
$this->command[] = '-colorspace';
591+
$this->command[] = $this->ref->colorspace($colorspace);
592+
593+
return $this;
594+
}
595+
565596
/**
566597
* /!\ Append a raw command to ImageMagick.
567598
* Not safe! Use at your own risks!

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,9 @@ This is why a "few" ones are implemented now, to make sure validation is possibl
164164
* [`-thumbnail`](http://www.imagemagick.org/script/command-line-options.php#thumbnail)
165165
* [`-annotate`](http://www.imagemagick.org/script/command-line-options.php#annotate)
166166
* [`-draw`](http://www.imagemagick.org/script/command-line-options.php#draw)
167+
* [`-depth`](http://www.imagemagick.org/script/command-line-options.php#depth)
168+
* [`-flatten`](http://www.imagemagick.org/script/command-line-options.php#flatten)
169+
* [`-colorspace`](http://www.imagemagick.org/script/command-line-options.php#colorspace)
167170
* [`xc:`](http://www.imagemagick.org/Usage/canvas/)
168171

169172
Feel free to ask/create an issue if you need more!

References.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,14 @@ public function __construct()
5959
}
6060
}
6161

62+
/**
63+
* @return string[]
64+
*/
65+
public function getColorspaceValuesReference(): array
66+
{
67+
return $this->config['colorspace_values'];
68+
}
69+
6270
/**
6371
* @return string[]
6472
*/
@@ -120,6 +128,28 @@ public function color(string $color): string
120128
));
121129
}
122130

131+
/**
132+
* Checks that colorspace value is valid in the references.
133+
*/
134+
public function colorspace(string $colorspace): string
135+
{
136+
$colorspace = \trim($colorspace);
137+
138+
$references = $this->getColorspaceValuesReference();
139+
140+
if (\in_array($colorspace, $references, true)) {
141+
return $colorspace;
142+
}
143+
144+
throw new \InvalidArgumentException(\sprintf(
145+
'The specified colorspace value (%s) is invalid.'."\n".
146+
'The available values are:'."\n%s\n".
147+
'Please refer to ImageMagick command line documentation:'."\n%s",
148+
$colorspace, \implode(', ', $references),
149+
'http://www.imagemagick.org/script/command-line-options.php#colorspace'
150+
));
151+
}
152+
123153
/**
124154
* Checks that a rotation option is correct according to ImageMagick command line reference.
125155
*

Resources/references.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,39 @@
2121
'gif',
2222
'png',
2323
],
24+
'colorspace_values' => [
25+
'CMY',
26+
'CMYK',
27+
'Gray',
28+
'HCL',
29+
'HCLp',
30+
'HSB',
31+
'HSI',
32+
'HSL',
33+
'HSV',
34+
'HWB',
35+
'Lab',
36+
'LCHab',
37+
'LCHuv',
38+
'LMS',
39+
'Log',
40+
'Luv',
41+
'OHTA',
42+
'Rec601YCbCr',
43+
'Rec709YCbCr',
44+
'RGB',
45+
'scRGB',
46+
'sRGB',
47+
'Transparent',
48+
'xyY',
49+
'XYZ',
50+
'YCbCr',
51+
'YCC',
52+
'YDbDr',
53+
'YIQ',
54+
'YPbPr',
55+
'YUV'
56+
],
2457
'colors' => [
2558
'snow',
2659
'snow1',

Tests/CommandTest.php

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,78 @@ public function testResizeImage(): void
6868
$this->testConvertIdentifyImage($imageOutput, 'JPEG', '100x94+0+0', '8-bit');
6969
}
7070

71+
public function testDepthImage(): void
72+
{
73+
$command = new Command(IMAGEMAGICK_DIR);
74+
75+
$imageToResize = $this->resourcesDir.'/moon_180.jpg';
76+
$imageOutput = $this->resourcesDir.'/outputs/moon.jpg';
77+
static::assertFileExists($imageToResize);
78+
79+
$response = $command
80+
->convert($imageToResize)
81+
->depth(1)
82+
->file($imageOutput, false)
83+
->run()
84+
;
85+
86+
static::assertFalse($response->hasFailed(), "Errors when testing:\n".$response->getProcess()->getOutput()."\t".$response->getProcess()->getErrorOutput());
87+
88+
static::assertFileExists($this->resourcesDir.'/outputs/moon.jpg');
89+
90+
static::assertFalse($response->hasFailed());
91+
92+
$this->testConvertIdentifyImage($imageOutput, 'JPEG', '180x170+0+0', '8-bit');
93+
}
94+
95+
public function testFlattenImage(): void
96+
{
97+
$command = new Command(IMAGEMAGICK_DIR);
98+
99+
$imageToResize = $this->resourcesDir.'/moon_180.jpg';
100+
$imageOutput = $this->resourcesDir.'/outputs/moon.jpg';
101+
static::assertFileExists($imageToResize);
102+
103+
$response = $command
104+
->convert($imageToResize)
105+
->flatten()
106+
->file($imageOutput, false)
107+
->run()
108+
;
109+
110+
static::assertFalse($response->hasFailed(), "Errors when testing:\n".$response->getProcess()->getOutput()."\t".$response->getProcess()->getErrorOutput());
111+
112+
static::assertFileExists($this->resourcesDir.'/outputs/moon.jpg');
113+
114+
static::assertFalse($response->hasFailed());
115+
116+
$this->testConvertIdentifyImage($imageOutput, 'JPEG', '180x170+0+0', '8-bit');
117+
}
118+
119+
public function testColorspaceImage(): void
120+
{
121+
$command = new Command(IMAGEMAGICK_DIR);
122+
123+
$imageToResize = $this->resourcesDir.'/moon_180.jpg';
124+
$imageOutput = $this->resourcesDir.'/outputs/moon.jpg';
125+
static::assertFileExists($imageToResize);
126+
127+
$response = $command
128+
->convert($imageToResize)
129+
->colorspace('Gray')
130+
->file($imageOutput, false)
131+
->run()
132+
;
133+
134+
static::assertFalse($response->hasFailed(), "Errors when testing:\n".$response->getProcess()->getOutput()."\t".$response->getProcess()->getErrorOutput());
135+
136+
static::assertFileExists($this->resourcesDir.'/outputs/moon.jpg');
137+
138+
static::assertFalse($response->hasFailed());
139+
140+
$this->testConvertIdentifyImage($imageOutput, 'JPEG', '180x170+0+0', '8-bit');
141+
}
142+
71143
/**
72144
* @dataProvider provideImagesToIdentify
73145
*/
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
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 ColorspaceValuesTest extends TestCase
20+
{
21+
/**
22+
* @var References
23+
*/
24+
private $ref;
25+
26+
public function __construct($name = null, array $data = [], $dataName = '')
27+
{
28+
parent::__construct($name, $data, $dataName);
29+
$this->ref = new References();
30+
}
31+
32+
/**
33+
* @param string $colorspaceValue
34+
* @param string $expected
35+
*
36+
* @dataProvider provideValidColorspaceValues
37+
*/
38+
public function testValidColorspaceValue($colorspaceValue, $expected): void
39+
{
40+
$validatedType = $this->ref->colorspace($colorspaceValue);
41+
42+
static::assertSame($expected, $validatedType);
43+
}
44+
45+
public function provideValidColorspaceValues(): ?\Generator
46+
{
47+
yield ['CMY', 'CMY'];
48+
yield ['CMYK', 'CMYK'];
49+
yield ['Gray', 'Gray'];
50+
yield ['HCL', 'HCL'];
51+
yield ['HCLp', 'HCLp'];
52+
yield ['HSB', 'HSB'];
53+
yield ['HSI', 'HSI'];
54+
}
55+
56+
/**
57+
* @dataProvider provideInvalidColorspaceValues
58+
*/
59+
public function testInvalidColorspaceValues($colorspaceValue): void
60+
{
61+
$colorspaceValue = (string) $colorspaceValue;
62+
63+
$msg = '';
64+
try {
65+
$this->ref->colorspace($colorspaceValue);
66+
} catch (\InvalidArgumentException $e) {
67+
$msg = $e->getMessage();
68+
}
69+
static::assertContains(
70+
\sprintf('The specified colorspace value (%s) is invalid', \trim($colorspaceValue)),
71+
$msg
72+
);
73+
}
74+
75+
public function provideInvalidColorspaceValues(): ?\Generator
76+
{
77+
yield [1];
78+
yield ['2'];
79+
yield ['wow'];
80+
yield ['WoW'];
81+
yield [''];
82+
yield [' '];
83+
}
84+
}

0 commit comments

Comments
 (0)