[ Index ]

PHP Cross Reference of Akelos Framework

title

Body

[close]

/AkImage/ -> AkImageColorScheme.php (source)

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


Generated: Mon Oct 27 12:43:49 2008 Cross-referenced by PHPXref 0.6