Completed
Push — fix/normalize-www-in-site-url-... ( e67e76 )
by
unknown
13:13 queued 02:59
created

_inc/lib/tonesque.php (4 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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
	 	// Grab the extension
41
		$file = strtolower( pathinfo( $image_url, PATHINFO_EXTENSION ) );
42
		$file = explode( '?', $file );
43
		$file = $file[ 0 ];
44
45
		switch ( $file ) {
46
			case 'gif' :
47
				$image_obj = imagecreatefromgif( $image_url );
48
				break;
49
			case 'png' :
50
				$image_obj = imagecreatefrompng( $image_url );
51
				break;
52
			case 'jpg' :
53
			case 'jpeg' :
54
				$image_obj = imagecreatefromjpeg( $image_url );
55
				break;
56
			default:
57
				return false;
58
		}
59
60
		return $image_obj;
61
	}
62
63
	/**
64
	 *
65
	 * Construct object from image.
66
	 *
67
	 * @param optional $type (hex, rgb, hsl)
68
	 * @return color as a string formatted as $type
69
	 *
70
 	 */
71
	function color( $type = 'hex' ) {
72
		// Bail if there is no image to work with
73
	 	if ( ! $this->image_obj )
74
			return false;
75
76
		// Finds dominant color
77
		$color = self::grab_color();
78
		// Passes value to Color class
79
		$color = self::get_color( $color, $type );
80
		return $color;
81
	}
82
83
	/**
84
	 *
85
	 * Grabs the color index for each of five sample points of the image
86
	 *
87
	 * @param $image
88
	 * @param $type can be 'index' or 'hex'
89
	 * @return array() with color indices
90
	 *
91
 	 */
92
	function grab_points( $type = 'index' ) {
93
		$img = $this->image_obj;
94
		if ( ! $img )
95
			return false;
96
97
		$height = imagesy( $img );
98
		$width  = imagesx( $img );
99
100
		// Sample five points in the image
101
		// Based on rule of thirds and center
102
		$topy    = round( $height / 3 );
103
		$bottomy = round( ( $height / 3 ) * 2 );
104
		$leftx   = round( $width / 3 );
105
		$rightx  = round( ( $width / 3 ) * 2 );
106
		$centery = round( $height / 2 );
107
		$centerx = round( $width / 2 );
108
109
		// Cast those colors into an array
110
		$points = array(
111
			imagecolorat( $img, $leftx, $topy ),
112
			imagecolorat( $img, $rightx, $topy ),
113
			imagecolorat( $img, $leftx, $bottomy ),
114
			imagecolorat( $img, $rightx, $bottomy ),
115
			imagecolorat( $img, $centerx, $centery ),
116
		);
117
118
		if ( 'hex' == $type ) {
119
			foreach ( $points as $i => $p ) {
120
				$c = imagecolorsforindex( $img, $p );
121
				$points[ $i ] = self::get_color( array(
122
					'r' => $c['red'],
123
					'g' => $c['green'],
124
					'b' => $c['blue'],
125
				), 'hex' );
126
			}
127
		}
128
129
		return $points;
130
	}
131
132
	/**
133
	 *
134
	 * Finds the average color of the image based on five sample points
135
	 *
136
	 * @param $image
137
	 * @return array() with rgb color
138
	 *
139
 	 */
140
	function grab_color() {
141
		$img = $this->image_obj;
142
		if ( ! $img )
143
			return false;
144
145
		$rgb = self::grab_points();
146
147
		// Process the color points
148
		// Find the average representation
149
		foreach ( $rgb as $color ) {
150
			$index = imagecolorsforindex( $img, $color );
151
			$r[] = $index['red'];
152
			$g[] = $index['green'];
153
			$b[] = $index['blue'];
154
155
			$red = round( array_sum( $r ) / 5 );
156
			$green = round( array_sum( $g ) / 5 );
157
			$blue = round( array_sum( $b ) / 5 );
158
		}
159
160
		// The average color of the image as rgb array
161
		$color = array(
162
			'r' => $red,
163
			'g' => $green,
164
			'b' => $blue,
165
		);
166
167
		return $color;
168
	}
169
170
	/**
171
	 *
172
	 * Get a Color object using /lib class.color
173
	 * Convert to appropriate type
174
	 *
175
	 * @return string
176
	 *
177
	 */
178
	function get_color( $color, $type ) {
179
		$c = new Jetpack_Color( $color, 'rgb' );
180
		$this->color = $c;
181
182
		switch ( $type ) {
183
			case 'rgb' :
184
				$color = implode( $c->toRgbInt(), ',' );
0 ignored issues
show
Consider using a different name than the parameter $color. This often makes code more readable.
Loading history...
185
				break;
186
			case 'hex' :
187
				$color = $c->toHex();
0 ignored issues
show
Consider using a different name than the parameter $color. This often makes code more readable.
Loading history...
188
				break;
189
			case 'hsv' :
190
				$color = implode( $c->toHsvInt(), ',' );
0 ignored issues
show
Consider using a different name than the parameter $color. This often makes code more readable.
Loading history...
191
				break;
192
			default:
193
				return $color = $c->toHex();
0 ignored issues
show
Consider using a different name than the parameter $color. This often makes code more readable.
Loading history...
194
		}
195
196
		return $color;
197
	}
198
199
	/**
200
	 *
201
	 * Checks contrast against main color
202
	 * Gives either black or white for using with opacity
203
	 *
204
	 * @return string
205
	 *
206
 	 */
207
	function contrast() {
208
	 	if ( ! $this->color )
209
			return false;
210
211
		$c = $this->color->getMaxContrastColor();
212
		return implode( $c->toRgbInt(), ',' );
213
	}
214
215
};
216