/var/www/hkosl.com/aga/wp-content/plugins/jetpack/_inc/lib/tonesque.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
<?php
/*
Plugin Name: Tonesque
Plugin URI: http://automattic.com/
Description: Grab an average color representation from an image.
Version: 1.0
Author: Automattic, Matias Ventura
Author URI: http://automattic.com/
License: GNU General Public License v2 or later
License URI: http://www.gnu.org/licenses/gpl-2.0.html
*/

class Tonesque {

    private 
$image_url '';
    private 
$image_obj NULL;
    private 
$color '';

    function 
__construct$image_url ) {
        if ( ! 
class_exists'Jetpack_Color' ) ) {
            
jetpack_require_lib'class.color' );
        }

        
$this->image_url esc_url_raw$image_url );
        
$this->image_url trim$this->image_url );
        
/**
         * Allows any image URL to be passed in for $this->image_url.
         *
         * @module theme-tools
         *
         * @since 2.5.0
         *
         * @param string $image_url The URL to any image
         */
        
$this->image_url apply_filters'tonesque_image_url'$this->image_url );

        
$this->image_obj self::imagecreatefromurl$this->image_url );
    }

    public static function 
imagecreatefromurl$image_url ) {
        
$data null;

        
// If it's a URL:
        
if ( preg_match'#^https?://#i'$image_url ) ) {
            
// If it's a url pointing to a local media library url:
            
$content_url content_url();
            
$_image_url  set_url_scheme$image_url );
            if ( 
wp_startswith$_image_url$content_url ) ) {
                
$_image_path str_replace$content_urlWP_CONTENT_DIR$_image_url );
                if ( 
file_exists$_image_path ) ) {
                    
$filetype wp_check_filetype$_image_path );
                    
$ext $filetype['ext'];
                    
$type $filetype['type'];

                    if ( 
wp_startswith$type'image/' ) ) {
                        
$data file_get_contents$_image_path );
                    }
                }
            }

            if ( empty( 
$data ) ) {
                
$response wp_remote_get$image_url );
                if ( 
is_wp_error$response ) ) {
                    return 
false;
                }
                
$data wp_remote_retrieve_body$response );
            }
        }

        
// If it's a local path in our WordPress install:
        
if ( file_exists$image_url ) ) {
            
$filetype wp_check_filetype$image_url );
            
$ext $filetype['ext'];
            
$type $filetype['type'];

            if ( 
wp_startswith$type'image/' ) ) {
                
$data file_get_contents$image_url );
            }
        }

        
// Now turn it into an image and return it.
        
return imagecreatefromstring$data );
    }

    
/**
     *
     * Construct object from image.
     *
     * @param optional $type (hex, rgb, hsv)
     * @return color as a string formatted as $type
     *
      */
    
function color$type 'hex' ) {
        
// Bail if there is no image to work with
         
if ( ! $this->image_obj )
            return 
false;

        
// Finds dominant color
        
$color self::grab_color();
        
// Passes value to Color class
        
$color self::get_color$color$type );
        return 
$color;
    }

    
/**
     *
     * Grabs the color index for each of five sample points of the image
     *
     * @param $image
     * @param $type can be 'index' or 'hex'
     * @return array() with color indices
     *
      */
    
function grab_points$type 'index' ) {
        
$img $this->image_obj;
        if ( ! 
$img )
            return 
false;

        
$height imagesy$img );
        
$width  imagesx$img );

        
// Sample five points in the image
        // Based on rule of thirds and center
        
$topy    round$height );
        
$bottomy round( ( $height ) * );
        
$leftx   round$width );
        
$rightx  round( ( $width ) * );
        
$centery round$height );
        
$centerx round$width );

        
// Cast those colors into an array
        
$points = array(
            
imagecolorat$img$leftx$topy ),
            
imagecolorat$img$rightx$topy ),
            
imagecolorat$img$leftx$bottomy ),
            
imagecolorat$img$rightx$bottomy ),
            
imagecolorat$img$centerx$centery ),
        );

        if ( 
'hex' == $type ) {
            foreach ( 
$points as $i => $p ) {
                
$c imagecolorsforindex$img$p );
                
$points$i ] = self::get_color( array(
                    
'r' => $c['red'],
                    
'g' => $c['green'],
                    
'b' => $c['blue'],
                ), 
'hex' );
            }
        }

        return 
$points;
    }

    
/**
     *
     * Finds the average color of the image based on five sample points
     *
     * @param $image
     * @return array() with rgb color
     *
      */
    
function grab_color() {
        
$img $this->image_obj;
        if ( ! 
$img )
            return 
false;

        
$rgb self::grab_points();

        
// Process the color points
        // Find the average representation
        
foreach ( $rgb as $color ) {
            
$index imagecolorsforindex$img$color );
            
$r[] = $index['red'];
            
$g[] = $index['green'];
            
$b[] = $index['blue'];

            
$red roundarray_sum$r ) / );
            
$green roundarray_sum$g ) / );
            
$blue roundarray_sum$b ) / );
        }

        
// The average color of the image as rgb array
        
$color = array(
            
'r' => $red,
            
'g' => $green,
            
'b' => $blue,
        );

        return 
$color;
    }

    
/**
     *
     * Get a Color object using /lib class.color
     * Convert to appropriate type
     *
     * @return string
     *
     */
    
function get_color$color$type ) {
        
$c = new Jetpack_Color$color'rgb' );
        
$this->color $c;

        switch ( 
$type ) {
            case 
'rgb' :
                
$color implode$c->toRgbInt(), ',' );
                break;
            case 
'hex' :
                
$color $c->toHex();
                break;
            case 
'hsv' :
                
$color implode$c->toHsvInt(), ',' );
                break;
            default:
                return 
$color $c->toHex();
        }

        return 
$color;
    }

    
/**
     *
     * Checks contrast against main color
     * Gives either black or white for using with opacity
     *
     * @return string
     *
      */
    
function contrast() {
         if ( ! 
$this->color )
            return 
false;

        
$c $this->color->getMaxContrastColor();
        return 
implode$c->toRgbInt(), ',' );
    }

};