PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 2.7.5
Jetpack – WP Security, Backup, Speed, & Growth v2.7.5
16.2-beta 12.0.3 12.1.3 12.2.3 12.3.2 12.4.2 12.5.2 12.6.4 12.7.3 12.8.3 12.9.5 13.0.2 13.1.5 13.2.4 13.3.3 13.4.5 13.5.2 13.6.2 13.7.2 13.8.3 13.9.2 14.0.1 14.1.1 14.2.2 14.3.1 All 501 releases
jetpack / lib / tonesque.php

tonesque.php in Jetpack – WP Security, Backup, Speed, & Growth 2.7.5, at lib/tonesque.php

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