-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTopColor.php
250 lines (201 loc) · 8 KB
/
TopColor.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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
<?php
/***
* Class TopColor
* - Deals with Image specific customization
*/
class TopColor{
/***
* [Step I] Method to resize image
*
* @param $imgResource
* @return bool
*/
public function resizeImage($imgResource){
// Default Variable
$imageName = 'img_0.png';
$shrinkPercent = 0.65;
// Get new dimensions
list($oldWidth, $oldHeight) = getimagesize($imgResource);
$newWidth = $oldWidth * $shrinkPercent;
$newHeight = $oldHeight * $shrinkPercent;
// Resample
$newImage = imagecreatetruecolor($newWidth, $newHeight);
$orgImage = imagecreatefromstring(file_get_contents($imgResource));
imagecopyresampled($newImage, $orgImage, 0, 0, 0, 0, $newWidth, $newHeight, $oldWidth, $oldHeight);
imagepng($newImage, $imageName);
return $imageName;
}
/***
* [Step II] Method to apply filters on image
*
* @param $resizedImage
* @return bool
*/
public function applyFilter($resizedImage){
// Default Variable
$imageName = 'img_1.png';
$brightnessLevel = -210;
$imageResource = imagecreatefromstring(file_get_contents($resizedImage));
imagefilter($imageResource, IMG_FILTER_EDGEDETECT);
imagefilter($imageResource, IMG_FILTER_BRIGHTNESS, $brightnessLevel);
imagefilter($imageResource, IMG_FILTER_GRAYSCALE);
imagefilter($imageResource, IMG_FILTER_MEAN_REMOVAL);
imagepng($imageResource, $imageName);
imagedestroy($imageResource);
return $imageName;
}
/***
* [Step III] Method to replace White with Green Color
*
* (Note: This step is an additional step, this could be skipped and Step IV will be dependant on Step II)
* @param $filterImage
* @return string
*/
public function setColor($filterImage){
// Default Variable
$imageName = 'img_2.png';
$imageResource = imagecreatefrompng($filterImage);
imagetruecolortopalette($imageResource, false, 255);
$index = imagecolorclosest ($imageResource, 255, 255, 255); // Get White Color
imagecolorset($imageResource, $index, 0, 255, 0); // Set Green Color
imagepng($imageResource, $imageName);
imagedestroy($imageResource);
return $imageName;
}
/***
* [Step IV] Method to Crop Image
*
* @param $coloredImage
* @param $resizedImage
* @return string
*/
public function cropImage($coloredImage, $resizedImage){
// Default Variable
$fileName = 'img_3.png';
$imageResource = imagecreatefrompng($coloredImage);
$size = getimagesize($coloredImage);
$height = $size[1];
$width = $size[0];
$half = $width/2;
$xCord_R = $yCord_R = $xCord_L = $yCord_L = [];
//******************************
// Getting Right Co-ordinates
//******************************
for($y = 0; $y < $height; $y++){
for($x = $half; $x < $width; $x++){
$thisColor = imagecolorat($imageResource, $x, $y); //9475598
$rgb = imagecolorsforindex($imageResource, $thisColor); //RGBA array
$red = round(round(($rgb['red'] / 0x33)) * 0x33); // 153
$green = round(round(($rgb['green'] / 0x33)) * 0x33); // 153
$blue = round(round(($rgb['blue'] / 0x33)) * 0x33); // 0
$thisRGB = sprintf('%02X%02X%02X', $red, $green, $blue); // FF9900
if($thisRGB == '00FF00'){
$xCord_R[] = $x;
$yCord_R[] = $y;
}
}
}
rsort($xCord_R);
sort($yCord_R);
if (empty($xCord_R)) {
$endPoint_R = $half;
} else {
$endPoint_R = $xCord_R[0]; // Point where green pixel last occurred.
}
if (empty($yCord_R)) {
$topPoint_R = 1;
$lastPoint_R = 1;
} else {
$lastPixel_R = count($yCord_R) - 1;
$topPoint_R = $yCord_R[0]; // Point where green pixel starts.
$lastPoint_R = $yCord_R[$lastPixel_R]; // No more green pixel after this.
}
//******************************
// Getting Left Co-ordinates
//******************************
for($y = 0; $y < $height; $y++){
for($x = 0; $x < $half; $x++){
$thisColor = imagecolorat($imageResource, $x, $y); //9475598
$rgb = imagecolorsforindex($imageResource, $thisColor); //RGBA array
$red = round(round(($rgb['red'] / 0x33)) * 0x33); // 153
$green = round(round(($rgb['green'] / 0x33)) * 0x33); // 153
$blue = round(round(($rgb['blue'] / 0x33)) * 0x33); // 0
$thisRGB = sprintf('%02X%02X%02X', $red, $green, $blue); // FF9900
if($thisRGB == '00FF00'){
$xCord_L[] = $x;
$yCord_L[] = $y;
}
}
}
sort($yCord_L);
sort($xCord_L);
if (empty($xCord_L)) {
$startPoint_L = $half;
} else {
$startPoint_L = $xCord_L[0]; // Point where green pixel last occurred.
}
if (empty($yCord_L)) {
$topPoint_L = 1;
$lastPoint_L = 1;
} else {
$lastPixel_L = count($yCord_L) - 1;
$topPoint_L = $yCord_L[0]; // Point where green pixel starts.
$lastPoint_L = $yCord_L[$lastPixel_L]; // No more green pixel after this.
}
$gap = $endPoint_R - $startPoint_L;
$length = (($lastPoint_R - $topPoint_R) + ($lastPoint_L - $topPoint_L)) / 2;
$topPoint = ($topPoint_R >= $topPoint_L)? $topPoint_R : $topPoint_L;
$cropImg = imagecreatefromstring(file_get_contents($resizedImage));
$to_crop_array = array('x' =>$startPoint_L , 'y' => $topPoint, 'width' => $gap, 'height'=> $length);
$croppedImage = imagecrop($cropImg, $to_crop_array);
imagepng($croppedImage, $fileName);
imagedestroy($croppedImage);
return $fileName;
}
/***
* [Step V] Method to fetch top 5 dominating color hexcodes
*
* @param $imageFile
* @param $numColors
* @param $granularity
* @param $isWhiteFlag
* @return array|bool
*/
function extractColor($imageFile, $numColors, $granularity, $isWhiteFlag=false){
$granularity = max(1, abs((int)$granularity)); // No. of pixel to skip
$colors = []; // Stores hex and occurrence
$size = getimagesize($imageFile);
if($size === false){
user_error("Unable to get image size data");
return false;
}
// To load any file type you can use this:
$img = imagecreatefromstring(file_get_contents($imageFile));
if(!$img) {
user_error("Unable to open image file");
return false;
}
for($x = 0; $x < $size[0]; $x += $granularity)
{
for($y = 0; $y < $size[1]; $y += $granularity)
{
$thisColor = imagecolorat($img, $x, $y); //9475598
$rgb = imagecolorsforindex($img, $thisColor); //RGBA array
$red = round(round(($rgb['red'] / 0x33)) * 0x33); // 153
$green = round(round(($rgb['green'] / 0x33)) * 0x33); // 153
$blue = round(round(($rgb['blue'] / 0x33)) * 0x33); // 0
$thisRGB = sprintf('%02X%02X%02X', $red, $green, $blue); // FF9900
if(array_key_exists($thisRGB, $colors)) {
$colors[$thisRGB]++; // If hex exist then occurrence increase
} else {
$colors[$thisRGB] = 1; // If hex not found, then occurrence is init to 1
}
}
}
arsort($colors); // Sort Array of color:key & occurrence:value in asc order
if($isWhiteFlag){
unset($colors['FFFFFF']);
}
return array_slice(array_keys($colors), 0, $numColors);
}
}