-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathColorImageProcessor.php
199 lines (138 loc) · 6.18 KB
/
ColorImageProcessor.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
<?php
include_once "./util/Point.php";
include_once "./util/density.php";
include_once "./ColorCollection.php";
class ColorImageProcessor {
/**
* @var string
*/
protected $imagePath;
/**
* @var Imagick
*/
protected $image;
protected function __construct(string $imagePath) {
$this->imagePath = $imagePath;
}
public static function fromURI(string $uri): ColorImageProcessor {
if (substr($uri, 0, 4) === "http") return ColorImageProcessor::fromRemoteFile($uri);
else return ColorImageProcessor::fromLocalFilePath($uri);
}
public static function fromLocalFilePath(string $imagePath): ColorImageProcessor {
$result = new ColorImageProcessor($imagePath);
if (file_exists($imagePath)) $result->imagePath = $imagePath;
else throw new Error("Could not find the image file located at: '$imagePath'...");
$result->image = new \Imagick(realpath($result->imagePath));
return $result;
}
public static function fromRemoteFile(string $imagePath): ColorImageProcessor {
$tempFile = tmpfile();
$tempFileMetaData = stream_get_meta_data($tempFile);
$tempFileURI = $tempFileMetaData["uri"];
$remoteImage = file_get_contents($imagePath);
file_put_contents($tempFileURI, $remoteImage);
return ColorImageProcessor::fromLocalFilePath($tempFileURI);
}
public function getImageSize(): Point {
return new Point($this->image->getImageWidth(), $this->image->getImageHeight());
}
/**
* Returns a ColorCollection object containing all of the strictly distinct colors found in the input image at the
* given absolute density.
*
* @param float $absoluteDensity The 'absolute density' at which pixels will be extracted from the image. An
* absolute density of 'n' means that a pixel will be extracted from the intersection of every n columns and n rows.
* @return ColorCollection A ColorCollection object containing all of the strictly distinct colors found in the
* input image at the specified density.
*/
public function getDistinctColorsByAbsoluteDensity(float $absoluteDensity = 1): ColorCollection {
$imageSize = $this->getImageSize();
$result = new ColorCollection();
for ($xCoordinate = 0; $xCoordinate < $imageSize->x; $xCoordinate += $absoluteDensity) {
$realXCoordinate = round($xCoordinate);
for ($yCoordinate = 0; $yCoordinate < $imageSize->y; $yCoordinate += $absoluteDensity) {
$realYCoordinate = round($yCoordinate);
$result->addColors(
Color::fromRGB(
$this->image->getImagePixelColor($realXCoordinate, $realYCoordinate)->getColorAsString()
)
);
}
}
return $result;
}
/**
* Returns a ColorCollection object containing all of the strictly distinct colors found in the input image at the
* given relative density.
*
* @param float $relativeDensity The 'relative density' at which pixels will be extracted from the image. For
* example, a relative density of '0.65' means that 65% of the source image's pixels will be extracted as colors.
* @return ColorCollection A ColorCollection object containing all of the strictly distinct colors found in the
* input image at the specified density.
*/
public function getDistinctColorsByRelativeDensity(float $relativeDensity): ColorCollection {
return $this->getDistinctColorsByAbsoluteDensity(
relativeDensityToAbsoluteDensity($relativeDensity)
);
}
/**
* Returns a ColorCollection object containing all of the strictly distinct colors found in the input image, given
* the maximum sample size.
*
* Internally, the maximum sample size is converted to a relative density, which is then converted to an absolute
* density. Due to the fact that this conversion process is not entirely lossless, the final number of sampled
* pixels will not necessarily match the maximum sample size.
*
* @param int $maxSampleSize The maximum number of pixels that should be sampled from the image.
* @return ColorCollection A ColorCollection object containing all of the strictly distinct colors found in the
* input image at the specified density.
*/
public function getDistinctColorsByMaximumSampleSize(int $maxSampleSize): ColorCollection {
$imageSize = $this->getImageSize();
$totalPixelCount = $imageSize->x * $imageSize->y;
return $this->getDistinctColorsByRelativeDensity($maxSampleSize / $totalPixelCount);
}
public function getPrimaryImageColors(): ColorCollection {
// Get up to 50,000 color samples from the image.
$result = $this->getDistinctColorsByMaximumSampleSize(50000);
// Attempt to remove colors that originated from the background.
$result = $result->getFilteredColorCollection(
ColorCollectionFilterFunctions::createEquivalencyFilter(
$this->guessBackgroundColor(),
ColorEquivalencyFunctions::createCIE94DeltaEEquivalencyFunction(10)
),
true
);
// Sort the results so that the upcoming merge operation
$result->sortByIncidence();
// Use a weighted merge to merge colors that are found to have a CIE94 delta-E of 30 or less.
$result = $result->getMergedColorCollection(
ColorEquivalencyFunctions::createCIE94DeltaEEquivalencyFunction(30),
ColorMergerFunctions::createWeightedAverageMergerFunction()
);
// Limit the results to only colors that comprise at least %5 of the colorspace.
$result = $result->getFilteredColorCollection(
ColorCollectionFilterFunctions::createRelativeOccurrenceFilter(0.05)
);
// Sort the result set by incidence in descending order.
$result->sortByIncidence();
return $result;
}
public function guessBackgroundColor(): Color {
$colorCollection = new ColorCollection();
$imageWidth = $this->image->getImageWidth();
$imageHeight = $this->image->getImageHeight();
$pixels = [
$this->image->getImagePixelColor(0, 0),
$this->image->getImagePixelColor($imageWidth, 0),
$this->image->getImagePixelColor(0, $imageHeight),
$this->image->getImagePixelColor($imageWidth, $imageHeight)
];
$colorCollection->addColors(...array_map(function(ImagickPixel $pixel): Color {
return Color::fromRGB($pixel->getColorAsString());
}, $pixels));
return $colorCollection->mergeToColor(
ColorMergerFunctions::createWeightedAverageMergerFunction()
);
}
}