|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
Plugin Name: Tonesque |
|
4
|
|
|
Plugin URI: http://automattic.com/ |
|
5
|
|
|
Description: Grab an average color representation from an image. |
|
6
|
|
|
Version: 1.0 |
|
7
|
|
|
Author: Automattic, Matias Ventura |
|
8
|
|
|
Author URI: http://automattic.com/ |
|
9
|
|
|
License: GNU General Public License v2 or later |
|
10
|
|
|
License URI: http://www.gnu.org/licenses/gpl-2.0.html |
|
11
|
|
|
*/ |
|
12
|
|
|
|
|
13
|
|
|
class Tonesque { |
|
14
|
|
|
|
|
15
|
|
|
private $image_url = ''; |
|
16
|
|
|
private $image_obj = NULL; |
|
17
|
|
|
private $color = ''; |
|
18
|
|
|
|
|
19
|
|
|
function __construct( $image_url ) { |
|
20
|
|
|
if ( ! class_exists( 'Jetpack_Color' ) ) |
|
21
|
|
|
jetpack_require_lib( 'class.color' ); |
|
22
|
|
|
|
|
23
|
|
|
$this->image_url = esc_url_raw( $image_url ); |
|
24
|
|
|
$this->image_url = trim( $this->image_url ); |
|
25
|
|
|
/** |
|
26
|
|
|
* Allows any image URL to be passed in for $this->image_url. |
|
27
|
|
|
* |
|
28
|
|
|
* @module theme-tools |
|
29
|
|
|
* |
|
30
|
|
|
* @since 2.5.0 |
|
31
|
|
|
* |
|
32
|
|
|
* @param string $image_url The URL to any image |
|
33
|
|
|
*/ |
|
34
|
|
|
$this->image_url = apply_filters( 'tonesque_image_url', $this->image_url ); |
|
35
|
|
|
|
|
36
|
|
|
$this->image_obj = self::imagecreatefromurl( $this->image_url ); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
public static function imagecreatefromurl( $image_url ) { |
|
40
|
|
|
return imagecreatefromstring( file_get_contents( $image_url ) ); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* |
|
45
|
|
|
* Construct object from image. |
|
46
|
|
|
* |
|
47
|
|
|
* @param optional $type (hex, rgb, hsl) |
|
|
|
|
|
|
48
|
|
|
* @return color as a string formatted as $type |
|
49
|
|
|
* |
|
50
|
|
|
*/ |
|
51
|
|
|
function color( $type = 'hex' ) { |
|
52
|
|
|
// Bail if there is no image to work with |
|
53
|
|
|
if ( ! $this->image_obj ) |
|
54
|
|
|
return false; |
|
55
|
|
|
|
|
56
|
|
|
// Finds dominant color |
|
57
|
|
|
$color = self::grab_color(); |
|
58
|
|
|
// Passes value to Color class |
|
59
|
|
|
$color = self::get_color( $color, $type ); |
|
60
|
|
|
return $color; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* |
|
65
|
|
|
* Grabs the color index for each of five sample points of the image |
|
66
|
|
|
* |
|
67
|
|
|
* @param $image |
|
68
|
|
|
* @param $type can be 'index' or 'hex' |
|
69
|
|
|
* @return array() with color indices |
|
|
|
|
|
|
70
|
|
|
* |
|
71
|
|
|
*/ |
|
72
|
|
|
function grab_points( $type = 'index' ) { |
|
73
|
|
|
$img = $this->image_obj; |
|
74
|
|
|
if ( ! $img ) |
|
75
|
|
|
return false; |
|
76
|
|
|
|
|
77
|
|
|
$height = imagesy( $img ); |
|
78
|
|
|
$width = imagesx( $img ); |
|
79
|
|
|
|
|
80
|
|
|
// Sample five points in the image |
|
81
|
|
|
// Based on rule of thirds and center |
|
82
|
|
|
$topy = round( $height / 3 ); |
|
83
|
|
|
$bottomy = round( ( $height / 3 ) * 2 ); |
|
84
|
|
|
$leftx = round( $width / 3 ); |
|
85
|
|
|
$rightx = round( ( $width / 3 ) * 2 ); |
|
86
|
|
|
$centery = round( $height / 2 ); |
|
87
|
|
|
$centerx = round( $width / 2 ); |
|
88
|
|
|
|
|
89
|
|
|
// Cast those colors into an array |
|
90
|
|
|
$points = array( |
|
91
|
|
|
imagecolorat( $img, $leftx, $topy ), |
|
92
|
|
|
imagecolorat( $img, $rightx, $topy ), |
|
93
|
|
|
imagecolorat( $img, $leftx, $bottomy ), |
|
94
|
|
|
imagecolorat( $img, $rightx, $bottomy ), |
|
95
|
|
|
imagecolorat( $img, $centerx, $centery ), |
|
96
|
|
|
); |
|
97
|
|
|
|
|
98
|
|
|
if ( 'hex' == $type ) { |
|
99
|
|
|
foreach ( $points as $i => $p ) { |
|
100
|
|
|
$c = imagecolorsforindex( $img, $p ); |
|
101
|
|
|
$points[ $i ] = self::get_color( array( |
|
102
|
|
|
'r' => $c['red'], |
|
103
|
|
|
'g' => $c['green'], |
|
104
|
|
|
'b' => $c['blue'], |
|
105
|
|
|
), 'hex' ); |
|
106
|
|
|
} |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
return $points; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* |
|
114
|
|
|
* Finds the average color of the image based on five sample points |
|
115
|
|
|
* |
|
116
|
|
|
* @param $image |
|
117
|
|
|
* @return array() with rgb color |
|
|
|
|
|
|
118
|
|
|
* |
|
119
|
|
|
*/ |
|
120
|
|
|
function grab_color() { |
|
121
|
|
|
$img = $this->image_obj; |
|
122
|
|
|
if ( ! $img ) |
|
123
|
|
|
return false; |
|
124
|
|
|
|
|
125
|
|
|
$rgb = self::grab_points(); |
|
126
|
|
|
|
|
127
|
|
|
// Process the color points |
|
128
|
|
|
// Find the average representation |
|
129
|
|
|
foreach ( $rgb as $color ) { |
|
|
|
|
|
|
130
|
|
|
$index = imagecolorsforindex( $img, $color ); |
|
131
|
|
|
$r[] = $index['red']; |
|
|
|
|
|
|
132
|
|
|
$g[] = $index['green']; |
|
|
|
|
|
|
133
|
|
|
$b[] = $index['blue']; |
|
|
|
|
|
|
134
|
|
|
|
|
135
|
|
|
$red = round( array_sum( $r ) / 5 ); |
|
|
|
|
|
|
136
|
|
|
$green = round( array_sum( $g ) / 5 ); |
|
|
|
|
|
|
137
|
|
|
$blue = round( array_sum( $b ) / 5 ); |
|
|
|
|
|
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
// The average color of the image as rgb array |
|
141
|
|
|
$color = array( |
|
142
|
|
|
'r' => $red, |
|
|
|
|
|
|
143
|
|
|
'g' => $green, |
|
|
|
|
|
|
144
|
|
|
'b' => $blue, |
|
|
|
|
|
|
145
|
|
|
); |
|
146
|
|
|
|
|
147
|
|
|
return $color; |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
/** |
|
151
|
|
|
* |
|
152
|
|
|
* Get a Color object using /lib class.color |
|
153
|
|
|
* Convert to appropriate type |
|
154
|
|
|
* |
|
155
|
|
|
* @return string |
|
156
|
|
|
* |
|
157
|
|
|
*/ |
|
158
|
|
|
function get_color( $color, $type ) { |
|
159
|
|
|
$c = new Jetpack_Color( $color, 'rgb' ); |
|
160
|
|
|
$this->color = $c; |
|
|
|
|
|
|
161
|
|
|
|
|
162
|
|
|
switch ( $type ) { |
|
163
|
|
|
case 'rgb' : |
|
164
|
|
|
$color = implode( $c->toRgbInt(), ',' ); |
|
|
|
|
|
|
165
|
|
|
break; |
|
166
|
|
|
case 'hex' : |
|
167
|
|
|
$color = $c->toHex(); |
|
|
|
|
|
|
168
|
|
|
break; |
|
169
|
|
|
case 'hsv' : |
|
170
|
|
|
$color = implode( $c->toHsvInt(), ',' ); |
|
|
|
|
|
|
171
|
|
|
break; |
|
172
|
|
|
default: |
|
173
|
|
|
return $color = $c->toHex(); |
|
|
|
|
|
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
return $color; |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
/** |
|
180
|
|
|
* |
|
181
|
|
|
* Checks contrast against main color |
|
182
|
|
|
* Gives either black or white for using with opacity |
|
183
|
|
|
* |
|
184
|
|
|
* @return string |
|
185
|
|
|
* |
|
186
|
|
|
*/ |
|
187
|
|
|
function contrast() { |
|
188
|
|
|
if ( ! $this->color ) |
|
189
|
|
|
return false; |
|
190
|
|
|
|
|
191
|
|
|
$c = $this->color->getMaxContrastColor(); |
|
|
|
|
|
|
192
|
|
|
return implode( $c->toRgbInt(), ',' ); |
|
193
|
|
|
} |
|
194
|
|
|
|
|
195
|
|
|
}; |
|
196
|
|
|
|
This check looks for
@paramannotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive.
Most often this is a case of a parameter that can be null in addition to its declared types.