PageRenderTime 45ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/AkImage/AkImageColorScheme.php

http://akelosframework.googlecode.com/
PHP | 118 lines | 89 code | 15 blank | 14 comment | 8 complexity | 63be1ef0b7c413e6ca22cae1552c75fc MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
  3. // +----------------------------------------------------------------------+
  4. // | Akelos Framework - http://www.akelos.org |
  5. // +----------------------------------------------------------------------+
  6. // | Copyright (c) 2002-2006, Akelos Media, S.L. & Bermi Ferrer Martinez |
  7. // | Released under the GNU Lesser General Public License, see LICENSE.txt|
  8. // +----------------------------------------------------------------------+
  9. /**
  10. * @package AkelosFramework
  11. * @subpackage AkImage
  12. * @author Bermi Ferrer <bermi a.t akelos c.om>
  13. * @copyright Copyright (c) 2002-2006, Akelos Media, S.L. http://www.akelos.org
  14. * @license GNU Lesser General Public License <http://www.gnu.org/copyleft/lesser.html>
  15. */
  16. require_once(AK_LIB_DIR.DS.'AkImage.php');
  17. class AkImageColorScheme extends AkObject
  18. {
  19. var $number_of_colors = 12;
  20. var $calculate_negatives = true;
  21. var $minimum_hits_for_negative = 50;
  22. var $Image;
  23. var $_tmp_file;
  24. var $_frequentColors = array();
  25. function setImage($image_path)
  26. {
  27. $this->Image =& new AkImage($image_path);
  28. $this->Image->transform('resize',array('size'=>'24x24'));
  29. $this->_tmp_file = AK_CACHE_DIR.DS.'__AkImageColorScheme_'.Ak::randomString(32).'.jpg';
  30. $this->Image->save($this->_tmp_file);
  31. }
  32. function __destruct()
  33. {
  34. if(file_exists($this->_tmp_file)){
  35. @Ak::file_delete($this->_tmp_file);
  36. }
  37. }
  38. function getColorScheme($number_of_colors = null)
  39. {
  40. $colors = array();
  41. if($image = @imagecreatefromjpeg($this->_tmp_file)){
  42. $imgage_width = $this->Image->Transform->new_x;
  43. $imgage_height = $this->Image->Transform->new_y;
  44. $inverted_colors = array();
  45. for ($y=0; $y < $imgage_height; $y++){
  46. for ($x=0; $x < $imgage_width; $x++){
  47. $index = imagecolorat($image, $x, $y);
  48. $image_colors = imagecolorsforindex($image, $index);
  49. $hex = '';
  50. foreach ($image_colors as $color=>$value){
  51. $image_colors[$color] = intval((($image_colors[$color])+15)/32)*32;
  52. $image_colors[$color] = $image_colors[$color] >= 256 ? 240 : $image_colors[$color];
  53. $hex .= substr('0'.dechex($image_colors[$color]), -2);
  54. }
  55. $hex = substr($hex, 0, 6);
  56. if(strlen($hex) == 6){
  57. $colors[$hex] = empty($colors[$hex]) ? 1 : $colors[$hex]+1;
  58. $this->_addToFrequentColors($hex);
  59. if($this->calculate_negatives && $colors[$hex] > $this->minimum_hits_for_negative){
  60. $negative = $this->_getNegativeAsHex($image_colors['red'], $image_colors['green'], $image_colors['blue']);
  61. $colors[$negative] = empty($colors[$negative]) ? 1 : $colors[$negative]+1;
  62. $this->_addToFrequentColors($negative);
  63. }
  64. }
  65. }
  66. }
  67. }
  68. return $this->_getColorsFromCounterColorArray($colors, $number_of_colors);
  69. }
  70. function _getColorsFromCounterColorArray($colors_array, $number_of_colors = null)
  71. {
  72. $number_of_colors = empty($number_of_colors) ? $this->number_of_colors : $number_of_colors;
  73. asort($colors_array);
  74. $colors_array = array_slice(array_unique(array_keys(array_reverse($colors_array, true))), 0, $number_of_colors);
  75. natsort($colors_array);
  76. return $colors_array;
  77. }
  78. function _getNegativeAsHex($red, $green, $blue)
  79. {
  80. $rgb = $red*0.15 + $green*0.5 + $blue * 0.35;
  81. return $this->_rgbToHex(array(255-$rgb, 255-$rgb, 255-$rgb));
  82. }
  83. function _addToFrequentColors($hex_color)
  84. {
  85. $this->_frequentColors[$hex_color] = empty($this->_frequentColors[$hex_color]) ? 1 : $this->_frequentColors[$hex_color]+1;
  86. }
  87. function resetFrequentColors()
  88. {
  89. $this->_frequentColors = array();
  90. }
  91. function getFrequentColors($number_of_colors = null)
  92. {
  93. return $this->_getColorsFromCounterColorArray($this->_frequentColors, $number_of_colors);
  94. }
  95. function _rgbToHex($rgb)
  96. {
  97. $r = str_pad(dechex($rgb[0]), 2, '0', STR_PAD_LEFT);
  98. $g = str_pad(dechex($rgb[1]), 2, '0', STR_PAD_LEFT);
  99. $b = str_pad(dechex($rgb[2]), 2, '0', STR_PAD_LEFT);
  100. return $r.$g.$b;
  101. }
  102. }
  103. ?>