Tonesque

Plugin Name: Tonesque Plugin URI: http://automattic.com/ Description: Grab an average color representation from an image.

Defined (1)

The class is defined in the following location(s).

/_inc/lib/tonesque.php  
  1. class Tonesque { 
  2.  
  3. private $image_url = ''; 
  4. private $image_obj = NULL; 
  5. private $color = ''; 
  6.  
  7. function __construct( $image_url ) { 
  8. if ( ! class_exists( 'Jetpack_Color' ) ) 
  9. jetpack_require_lib( 'class.color' ); 
  10.  
  11. $this->image_url = esc_url_raw( $image_url ); 
  12. $this->image_url = trim( $this->image_url ); 
  13. /** 
  14. * Allows any image URL to be passed in for $this->image_url. 
  15. * @module theme-tools 
  16. * @since 2.5.0 
  17. * @param string $image_url The URL to any image 
  18. */ 
  19. $this->image_url = apply_filters( 'tonesque_image_url', $this->image_url ); 
  20.  
  21. $this->image_obj = self::imagecreatefromurl( $this->image_url ); 
  22.  
  23. public static function imagecreatefromurl( $image_url ) { 
  24. // Grab the extension 
  25. $file = strtolower( pathinfo( $image_url, PATHINFO_EXTENSION ) ); 
  26. $file = explode( '?', $file ); 
  27. $file = $file[ 0 ]; 
  28.  
  29. switch ( $file ) { 
  30. case 'gif' : 
  31. $image_obj = imagecreatefromgif( $image_url ); 
  32. break; 
  33. case 'png' : 
  34. $image_obj = imagecreatefrompng( $image_url ); 
  35. break; 
  36. case 'jpg' : 
  37. case 'jpeg' : 
  38. $image_obj = imagecreatefromjpeg( $image_url ); 
  39. break; 
  40. default: 
  41. return false; 
  42.  
  43. return $image_obj; 
  44.  
  45. /** 
  46. * Construct object from image. 
  47. * @param optional $type (hex, rgb, hsl) 
  48. * @return color as a string formatted as $type 
  49. */ 
  50. function color( $type = 'hex' ) { 
  51. // Bail if there is no image to work with 
  52. if ( ! $this->image_obj ) 
  53. return false; 
  54.  
  55. // Finds dominant color 
  56. $color = self::grab_color(); 
  57. // Passes value to Color class 
  58. $color = self::get_color( $color, $type ); 
  59. return $color; 
  60.  
  61. /** 
  62. * Grabs the color index for each of five sample points of the image 
  63. * @param $image 
  64. * @param $type can be 'index' or 'hex' 
  65. * @return array() with color indices 
  66. */ 
  67. function grab_points( $type = 'index' ) { 
  68. $img = $this->image_obj; 
  69. if ( ! $img ) 
  70. return false; 
  71.  
  72. $height = imagesy( $img ); 
  73. $width = imagesx( $img ); 
  74.  
  75. // Sample five points in the image 
  76. // Based on rule of thirds and center 
  77. $topy = round( $height / 3 ); 
  78. $bottomy = round( ( $height / 3 ) * 2 ); 
  79. $leftx = round( $width / 3 ); 
  80. $rightx = round( ( $width / 3 ) * 2 ); 
  81. $centery = round( $height / 2 ); 
  82. $centerx = round( $width / 2 ); 
  83.  
  84. // Cast those colors into an array 
  85. $points = array( 
  86. imagecolorat( $img, $leftx, $topy ),  
  87. imagecolorat( $img, $rightx, $topy ),  
  88. imagecolorat( $img, $leftx, $bottomy ),  
  89. imagecolorat( $img, $rightx, $bottomy ),  
  90. imagecolorat( $img, $centerx, $centery ),  
  91. ); 
  92.  
  93. if ( 'hex' == $type ) { 
  94. foreach ( $points as $i => $p ) { 
  95. $c = imagecolorsforindex( $img, $p ); 
  96. $points[ $i ] = self::get_color( array( 
  97. 'r' => $c['red'],  
  98. 'g' => $c['green'],  
  99. 'b' => $c['blue'],  
  100. ), 'hex' ); 
  101.  
  102. return $points; 
  103.  
  104. /** 
  105. * Finds the average color of the image based on five sample points 
  106. * @param $image 
  107. * @return array() with rgb color 
  108. */ 
  109. function grab_color() { 
  110. $img = $this->image_obj; 
  111. if ( ! $img ) 
  112. return false; 
  113.  
  114. $rgb = self::grab_points(); 
  115.  
  116. // Process the color points 
  117. // Find the average representation 
  118. foreach ( $rgb as $color ) { 
  119. $index = imagecolorsforindex( $img, $color ); 
  120. $r[] = $index['red']; 
  121. $g[] = $index['green']; 
  122. $b[] = $index['blue']; 
  123.  
  124. $red = round( array_sum( $r ) / 5 ); 
  125. $green = round( array_sum( $g ) / 5 ); 
  126. $blue = round( array_sum( $b ) / 5 ); 
  127.  
  128. // The average color of the image as rgb array 
  129. $color = array( 
  130. 'r' => $red,  
  131. 'g' => $green,  
  132. 'b' => $blue,  
  133. ); 
  134.  
  135. return $color; 
  136.  
  137. /** 
  138. * Get a Color object using /lib class.color 
  139. * Convert to appropriate type 
  140. * @return string 
  141. */ 
  142. function get_color( $color, $type ) { 
  143. $c = new Jetpack_Color( $color, 'rgb' ); 
  144. $this->color = $c; 
  145.  
  146. switch ( $type ) { 
  147. case 'rgb' : 
  148. $color = implode( $c->toRgbInt(), ', ' ); 
  149. break; 
  150. case 'hex' : 
  151. $color = $c->toHex(); 
  152. break; 
  153. case 'hsv' : 
  154. $color = implode( $c->toHsvInt(), ', ' ); 
  155. break; 
  156. default: 
  157. return $color = $c->toHex(); 
  158.  
  159. return $color; 
  160.  
  161. /** 
  162. * Checks contrast against main color 
  163. * Gives either black or white for using with opacity 
  164. * @return string 
  165. */ 
  166. function contrast() { 
  167. if ( ! $this->color ) 
  168. return false; 
  169.  
  170. $c = $this->color->getMaxContrastColor(); 
  171. return implode( $c->toRgbInt(), ', ' ); 
  172.  
  173. };