-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAverageColorExtractor.php
136 lines (117 loc) · 3.73 KB
/
AverageColorExtractor.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
<?php
namespace Schnitzler\AverageColorExtractor;
use TYPO3\CMS\Core\Log\Logger;
use TYPO3\CMS\Core\Log\LogManager;
use TYPO3\CMS\Core\Resource\AbstractFile;
use TYPO3\CMS\Core\Resource\File;
use TYPO3\CMS\Core\Resource\Index\ExtractorInterface;
use ColorThief\ColorThief;
use TYPO3\CMS\Core\Utility\GeneralUtility;
/**
* Class AverageColorExtractor
* @package Schnitzler\AverageColorExtractor
*/
class AverageColorExtractor implements ExtractorInterface
{
/**
* @var Logger
*/
protected $logger;
public function __construct()
{
$this->logger = GeneralUtility::makeInstance(LogManager::class)->getLogger(__CLASS__);
}
/**
* Returns an array of supported file types;
* An empty array indicates all filetypes
*
* @return array
*/
public function getFileTypeRestrictions()
{
return [AbstractFile::FILETYPE_IMAGE];
}
/**
* Get all supported DriverClasses
*
* Since some extractors may only work for local files, and other extractors
* are especially made for grabbing data from remote.
*
* Returns array of string with driver names of Drivers which are supported,
* If the driver did not register a name, it's the classname.
* empty array indicates no restrictions
*
* @return array
*/
public function getDriverRestrictions()
{
return ['Local'];
}
/**
* Returns the data priority of the extraction Service.
* Defines the precedence of Data if several extractors
* extracted the same property.
*
* Should be between 1 and 100, 100 is more important than 1
*
* @return integer
*/
public function getPriority()
{
return 10;
}
/**
* Returns the execution priority of the extraction Service
* Should be between 1 and 100, 100 means runs as first service, 1 runs at last service
*
* @return integer
*/
public function getExecutionPriority()
{
return 10;
}
/**
* Checks if the given file can be processed by this Extractor
*
* @param File $file
* @return boolean
*/
public function canProcess(File $file)
{
return true;
}
/**
* The actual processing TASK
*
* Should return an array with database properties for sys_file_metadata to write
*
* @param File $file
* @param array $previousExtractedData optional, contains the array of already extracted data
* @return array
*/
public function extractMetaData(File $file, array $previousExtractedData = [])
{
$metadata = [];
try {
if (!class_exists('ColorThief\\ColorThief')) {
throw new \RuntimeException('Class ColorThief\\ColorThief does not exist', 1470749087524);
}
$path = $file->getForLocalProcessing();
$averageColor = ColorThief::getColor($path);
if (!is_array($averageColor)) {
throw new \RuntimeException('$averageColor is not an array', 1470749109020);
}
if (count($averageColor) !== 3) {
throw new \RuntimeException('$averageColor is an array, but has less than 3 items', 1470749136303);
}
$r = dechex((int)$averageColor[0]);
$g = dechex((int)$averageColor[1]);
$b = dechex((int)$averageColor[2]);
$metadata['average_color'] = '#' . $r . $g . $b;
$this->logger->debug(sprintf('Extracted average color "%s"', $metadata['average_color'] ), ['file' => $file->getUid()]);
} catch (\Exception $e) {
$this->logger->error($e->getCode() . ': ' . $e->getMessage(), ['file' => $file->getUid()]);
}
return $metadata;
}
}