1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SixtyNine\Cloud\Command; |
4
|
|
|
|
5
|
|
|
use Imagine\Image\ImageInterface; |
6
|
|
|
use SixtyNine\Cloud\Builder\PalettesBuilder; |
7
|
|
|
use SixtyNine\Cloud\Color\ColorGeneratorInterface; |
8
|
|
|
use SixtyNine\Cloud\Color\RandomColorGenerator; |
9
|
|
|
use SixtyNine\Cloud\Color\RotateColorGenerator; |
10
|
|
|
use SixtyNine\Cloud\Factory\FontsFactory; |
11
|
|
|
use SixtyNine\Cloud\Factory\PlacerFactory; |
12
|
|
|
use SixtyNine\Cloud\FontSize\BoostFontSizeGenerator; |
13
|
|
|
use SixtyNine\Cloud\FontSize\DimFontSizeGenerator; |
14
|
|
|
use SixtyNine\Cloud\FontSize\FontSizeGeneratorInterface; |
15
|
|
|
use SixtyNine\Cloud\FontSize\LinearFontSizeGenerator; |
16
|
|
|
use SixtyNine\Cloud\Placer\PlacerInterface; |
17
|
|
|
|
18
|
|
|
class CommandsHelper |
19
|
|
|
{ |
20
|
|
|
protected $allowedFontSizeBoosts = array('linear', 'dim', 'boost'); |
21
|
|
|
protected $allowedPaletteTypes = array('cycle', 'random'); |
22
|
|
|
protected $allowedOutputFormats = array('gif', 'jpeg', 'png'); |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @param string $name |
26
|
|
|
* @return PlacerInterface |
27
|
|
|
* @throws \InvalidArgumentException |
28
|
|
|
*/ |
29
|
|
|
public function getPlacer($name) |
30
|
|
|
{ |
31
|
|
|
$availablePlacers = PlacerFactory::getInstance()->getPlacersNames(); |
32
|
|
|
|
33
|
|
|
if ($name) { |
34
|
|
|
if (!in_array($name, $availablePlacers)) { |
35
|
|
|
throw new \InvalidArgumentException('Word placer not found: ' . $name); |
36
|
|
|
} |
37
|
|
|
return $name; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
if (!count($availablePlacers)) { |
41
|
|
|
throw new \InvalidArgumentException('No word placers available'); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
return $availablePlacers[0]; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @param FontsFactory $factory |
49
|
|
|
* @param string $font |
50
|
|
|
* @return string |
51
|
|
|
* @throws \InvalidArgumentException |
52
|
|
|
*/ |
53
|
|
|
public function getFont(FontsFactory $factory, $font) |
54
|
|
|
{ |
55
|
|
|
if ($font) { |
56
|
|
|
return $font; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
if (0 === count($factory->getFonts())) { |
60
|
|
|
throw new \InvalidArgumentException('No font file found'); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
return $factory->getFonts()[0]; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @param string $type |
68
|
|
|
* @return FontSizeGeneratorInterface |
69
|
|
|
* @throws \InvalidArgumentException |
70
|
|
|
*/ |
71
|
|
|
public function getFontSizeGenerator($type = 'linear') |
72
|
|
|
{ |
73
|
|
|
if (!in_array($type, $this->allowedFontSizeBoosts)) { |
74
|
|
|
throw new \InvalidArgumentException('Invalid font size boost: ' . $type); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
$generatorClass = LinearFontSizeGenerator::class; |
78
|
|
|
if ($type === 'dim') { |
79
|
|
|
$generatorClass = DimFontSizeGenerator::class; |
80
|
|
|
} |
81
|
|
|
if ($type === 'boost') { |
82
|
|
|
$generatorClass = BoostFontSizeGenerator::class; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
return new $generatorClass(); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @param string $paletteName |
90
|
|
|
* @param string $paletteType |
91
|
|
|
* @param string $palettesFile |
92
|
|
|
* @return bool|ColorGeneratorInterface |
93
|
|
|
* @throws \InvalidArgumentException |
94
|
|
|
*/ |
95
|
|
|
public function getColorGenerator($paletteName, $paletteType, $palettesFile = null) |
96
|
|
|
{ |
97
|
|
|
if ($paletteName && $paletteType) { |
98
|
|
|
|
99
|
|
|
if (!in_array($paletteType, $this->allowedPaletteTypes)) { |
100
|
|
|
throw new \InvalidArgumentException('Palette type must be either "cycle" or "random"'); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
$file = $palettesFile |
104
|
|
|
? $palettesFile |
105
|
|
|
: __DIR__ . '/../Resources/palettes.yml' |
106
|
|
|
; |
107
|
|
|
$paletteBuilder = PalettesBuilder::create()->importPalettes($file); |
108
|
|
|
|
109
|
|
|
$palette = $paletteBuilder->getNamedPalette($paletteName); |
110
|
|
|
$generatorClass = ($paletteType === 'cycle') |
111
|
|
|
? RotateColorGenerator::class |
112
|
|
|
: RandomColorGenerator::class |
113
|
|
|
; |
114
|
|
|
return new $generatorClass($palette); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
return false; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @param ImageInterface $image |
122
|
|
|
* @param string $outputFormat |
123
|
|
|
* @param string $outputFile |
124
|
|
|
* @throws \InvalidArgumentException |
125
|
|
|
*/ |
126
|
|
|
public function output(ImageInterface $image, $outputFormat, $outputFile = null) |
127
|
|
|
{ |
128
|
|
|
if (!in_array($outputFormat, $this->allowedOutputFormats)) { |
129
|
|
|
throw new \InvalidArgumentException('Invalid output format: ' . $outputFormat); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
if ($outputFile) { |
|
|
|
|
133
|
|
|
$image->save($outputFile, array('format' => $outputFormat)); |
134
|
|
|
return; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
echo $image->get($outputFormat); |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
|
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: