-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaverageColor.php
More file actions
128 lines (101 loc) · 3.31 KB
/
averageColor.php
File metadata and controls
128 lines (101 loc) · 3.31 KB
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
<?php
/**
* imageColor
*
* Shows three methods to find the 'average' image color.
*
* Each function expects a gd image object.
*
* imageColor::averageResize($image) resizing to 1px, and checking the color.
* imageColor::averageBorder($image) find the average color of all border pixels.
* imageColor::averageImage($image) find the average color of all pixels.
*
*/
class imageColor {
function scanLine($image, $height, $width, $axis, $line) {
$i = 0;
if ("x" == $axis) {
$limit = $width;
$y = $line;
$x = & $i;
if (-1 == $line) {
$y = 0;
$y2 = $width - 1;
$x2 = & $i;
}
} else {
$limit = $height;
$x = $line;
$y = & $i;
if (-1 == $line) {
$x = 0;
$x2 = $width - 1;
$y2 = & $i;
}
}
$colors = array();
if (-1 == $line) {
for ($i = 0; $i < $limit; $i++) {
self::addPixel($colors, $image, $x, $y);
self::addPixel($colors, $image, $x2, $y2);
}
} else {
for ($i = 0; $i < $limit; $i++) {
self::addPixel($colors, $image, $x, $y);
}
}
return $colors;
}
function addPixel(&$colors, $image, $x, $y) {
$rgb = imagecolorat($image, $x, $y);
$color = imagecolorsforindex($image, $rgb);
$colors['red'][] = $color['red'];
$colors['green'][] = $color['green'];
$colors['blue'][] = $color['blue'];
}
function totalColors($color, $colors) {
$color['red'] += array_sum($colors['red']);
$color['green'] += array_sum($colors['green']);
$color['blue'] += array_sum($colors['blue']);
return $colors;
}
function averageTotal($color, $count) {
$color['red'] = intval($color['red'] / $count);
$color['green'] = intval($color['green'] / $count);
$color['blue'] = intval($color['blue'] / $count);
return $color;
}
function averageResize($image) {
$width = imagesx($image);
$height = imagesy($image);
$pixel = imagecreatetruecolor(1, 1);
imagecopyresampled($pixel, $image, 0, 0, 0, 0, 1, 1, $width, $height);
$rgb = imagecolorat($pixel, 0, 0);
$color = imagecolorsforindex($pixel, $rgb);
return $color;
}
function averageBorder($image) {
$width = imagesx($image);
$height = imagesy($image);
$colors = self::scanLine($image, $height, $width, 'x', -1);
self::totalColors(&$color, $colors);
$colors = self::scanLine($image, $height, $width, 'y', -1);
self::totalColors(&$color, $colors);
$borderSize = ($height = $width) * 2;
self::averageTotal(&$color, $borderSize);
return $color;
}
function averageImage($image) {
$width = imagesx($image);
$height = imagesy($image);
$colors = array();
for ($line = 0; $line < $height; $line++) {
$colors = self::scanLine($image, $height, $width, 'x', $line);
self::totalColors(&$color, $colors);
}
$count = $width * $height;
self::averageTotal(&$color, $count);
return $color;
}
}
?>