|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Toothless\TinyImage\Extractor; |
|
4
|
|
|
|
|
5
|
|
|
use Imagick; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Class ImagickDominantColorExtractor |
|
9
|
|
|
*/ |
|
10
|
|
|
class ImagickDominantColorExtractor implements DominantColorExtractorInterface |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* Default width for image sampling |
|
14
|
|
|
* |
|
15
|
|
|
* @var int |
|
16
|
|
|
* |
|
17
|
|
|
* @see http://php.net/manual/en/imagick.resizeimage.php |
|
18
|
|
|
*/ |
|
19
|
|
|
const DEFAULT_SAMPLE_WIDTH = 256; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Default height for image sampling |
|
23
|
|
|
* |
|
24
|
|
|
* @var int |
|
25
|
|
|
* |
|
26
|
|
|
* @see http://php.net/manual/en/imagick.resizeimage.php |
|
27
|
|
|
*/ |
|
28
|
|
|
const DEFAULT_SAMPLE_HEIGHT = 256; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Default blur ratio for image sampling |
|
32
|
|
|
* |
|
33
|
|
|
* @var int |
|
34
|
|
|
* |
|
35
|
|
|
* @see http://php.net/manual/en/imagick.resizeimage.php |
|
36
|
|
|
*/ |
|
37
|
|
|
const DEFAULT_SAMPLE_BLUR_RATIO = 1; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @var RGBColorExtractor |
|
41
|
|
|
*/ |
|
42
|
|
|
private $extractor; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @var Imagick |
|
46
|
|
|
*/ |
|
47
|
|
|
private $imagick; |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @var int |
|
51
|
|
|
*/ |
|
52
|
|
|
private $sampleWidth; |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @var int |
|
56
|
|
|
*/ |
|
57
|
|
|
private $sampleHeight; |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @var int |
|
61
|
|
|
*/ |
|
62
|
|
|
private $sampleBlurRatio; |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* ImagickDominantColorExtractor constructor. |
|
66
|
|
|
* |
|
67
|
|
|
* @param RGBColorExtractor $extractor |
|
68
|
|
|
* @param Imagick $imagick Imagick instance |
|
69
|
|
|
* @param int $sampleWidth Image sample width |
|
70
|
|
|
* @param int $sampleHeight Image sample height |
|
71
|
|
|
* @param int $sampleBlurRatio Image blur ratio |
|
72
|
|
|
*/ |
|
73
|
4 |
|
public function __construct( |
|
74
|
|
|
RGBColorExtractor $extractor, |
|
75
|
|
|
Imagick $imagick, |
|
76
|
|
|
$sampleWidth = self::DEFAULT_SAMPLE_WIDTH, |
|
77
|
|
|
$sampleHeight = self::DEFAULT_SAMPLE_HEIGHT, |
|
78
|
|
|
$sampleBlurRatio = self::DEFAULT_SAMPLE_BLUR_RATIO |
|
79
|
|
|
) |
|
80
|
|
|
{ |
|
81
|
4 |
|
$this->extractor = $extractor; |
|
82
|
4 |
|
$this->imagick = $imagick; |
|
83
|
4 |
|
$this->sampleWidth = $sampleWidth; |
|
84
|
4 |
|
$this->sampleHeight = $sampleHeight; |
|
85
|
4 |
|
$this->sampleBlurRatio = $sampleBlurRatio; |
|
86
|
4 |
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* {@inheritdoc} |
|
90
|
|
|
*/ |
|
91
|
2 |
|
public function extract($content) |
|
92
|
|
|
{ |
|
93
|
2 |
|
$this->imagick->readImageBlob($content); |
|
94
|
2 |
|
$this->imagick->resizeImage( |
|
95
|
2 |
|
$this->sampleWidth, |
|
96
|
2 |
|
$this->sampleHeight, |
|
97
|
2 |
|
Imagick::FILTER_GAUSSIAN, |
|
98
|
2 |
|
$this->sampleBlurRatio |
|
99
|
1 |
|
); |
|
100
|
2 |
|
$this->imagick->quantizeImage(1, Imagick::COLORSPACE_RGB, 0, false, false); |
|
101
|
2 |
|
$this->imagick->setFormat('RGB'); |
|
102
|
|
|
|
|
103
|
|
|
// Delegate color extraction |
|
104
|
2 |
|
return $this->extractor->extract($this->imagick->getImageBlob()); |
|
105
|
|
|
} |
|
106
|
|
|
} |
|
107
|
|
|
|