1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Brendt\Image\Config; |
4
|
|
|
|
5
|
|
|
use Brendt\Image\Exception\InvalidConfigurationException; |
6
|
|
|
use Brendt\Image\ResponsiveFactory; |
7
|
|
|
use Brendt\Image\Scaler\AbstractScaler; |
8
|
|
|
use Brendt\Image\Scaler\FileSizeScaler; |
9
|
|
|
use Brendt\Image\Scaler\Scaler; |
10
|
|
|
use Brendt\Image\Scaler\WidthScaler; |
11
|
|
|
|
12
|
|
|
class DefaultConfigurator implements ResponsiveFactoryConfigurator |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* The default config |
16
|
|
|
* |
17
|
|
|
* @var array |
18
|
|
|
*/ |
19
|
|
|
protected $config = [ |
20
|
|
|
'driver' => 'gd', |
21
|
|
|
'publicPath' => './', |
22
|
|
|
'sourcePath' => './', |
23
|
|
|
'enableCache' => false, |
24
|
|
|
'stepModifier' => 0.5, |
25
|
|
|
'minFileSize' => 10000, |
26
|
|
|
'minWidth' => 150, |
27
|
|
|
'scaler' => 'filesize', |
28
|
|
|
]; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* ResponsiveFactoryConfigurator constructor. |
32
|
|
|
* |
33
|
|
|
* @param array $config |
34
|
|
|
* |
35
|
|
|
* @throws InvalidConfigurationException |
36
|
|
|
*/ |
37
|
|
|
public function __construct(array $config = []) { |
38
|
|
|
if (isset($config['driver']) && !in_array($config['driver'], ['gd', 'imagick'])) { |
39
|
|
|
throw new InvalidConfigurationException('Invalid driver. Possible drivers are `gd` and `imagick`'); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
$this->config = array_merge($this->config, $config); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @param ResponsiveFactory $factory |
47
|
|
|
* |
48
|
|
|
* @return void |
49
|
|
|
*/ |
50
|
|
|
public function configure(ResponsiveFactory $factory) { |
51
|
|
|
/** @var AbstractScaler $scaler */ |
52
|
|
|
switch ($this->config['scaler']) { |
53
|
|
|
case 'filesize': |
54
|
|
|
$scaler = new FileSizeScaler($this); |
55
|
|
|
break; |
56
|
|
|
case 'width': |
57
|
|
|
default: |
58
|
|
|
$scaler = new WidthScaler($this); |
59
|
|
|
break; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
$factory |
63
|
|
|
->setDriver($this->config['driver']) |
64
|
|
|
->setPublicPath($this->config['publicPath']) |
65
|
|
|
->setSourcePath($this->config['sourcePath']) |
66
|
|
|
->setEnableCache($this->config['enableCache']) |
67
|
|
|
->setScaler($scaler); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @param Scaler $scaler |
72
|
|
|
* |
73
|
|
|
* @return Scaler |
74
|
|
|
*/ |
75
|
|
|
public function configureScaler(Scaler $scaler) { |
76
|
|
|
$scaler |
77
|
|
|
->setSourcePath($this->config['sourcePath']) |
78
|
|
|
->setPublicPath($this->config['publicPath']) |
79
|
|
|
->setMinFileSize($this->config['minFileSize']) |
80
|
|
|
->setMinWidth($this->config['minWidth']) |
81
|
|
|
->setStepModifier($this->config['stepModifier']); |
82
|
|
|
|
83
|
|
|
return $scaler; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @return array |
88
|
|
|
*/ |
89
|
|
|
public function getConfig() { |
90
|
|
|
return $this->config; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @param $key |
95
|
|
|
* |
96
|
|
|
* @return mixed|null |
97
|
|
|
*/ |
98
|
|
|
public function get($key) { |
99
|
|
|
return isset($this->config[$key]) ? $this->config[$key] : null; |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|