source: extensions/ColorStat/cstat_root.class.inc.php @ 6107

Last change on this file since 6107 was 6107, checked in by grum, 14 years ago

Plugin is now in a usable state (color analysis is not yet tuned)

  • Property svn:executable set to *
File size: 8.3 KB
Line 
1<?php
2/* -----------------------------------------------------------------------------
3  Plugin     : ColorStat
4  Author     : Grum
5    email    : grum@piwigo.org
6    website  : http://photos.grum.fr
7
8    << May the Little SpaceFrog be with you ! >>
9  ------------------------------------------------------------------------------
10  See main.inc.php for release information
11
12  CStat_root : common classe for admin and public classes
13
14  --------------------------------------------------------------------------- */
15
16  if(!defined('PHPWG_ROOT_PATH')) die('Hacking attempt!');
17
18
19  include_once('cstat_colorstat.class.inc.php');
20  include_once(PHPWG_PLUGINS_PATH.'GrumPluginClasses/classes/CommonPlugin.class.inc.php');
21  include_once(PHPWG_PLUGINS_PATH.'GrumPluginClasses/classes/GPCCss.class.inc.php');
22  include_once(PHPWG_PLUGINS_PATH.'GrumPluginClasses/classes/GPCAjax.class.inc.php');
23
24  class CStat_root extends CommonPlugin
25  {
26    protected $css;   //the css object
27    protected $colorTableSize=Array('small' => Array(30,20), 'large' => Array(10,10));
28
29    public function __construct($prefixeTable, $filelocation)
30    {
31      $this->setPluginName('ColorStat');
32      $this->setPluginNameFiles("cstat");
33      parent::__construct($prefixeTable, $filelocation);
34      $this->section_name=$this->getPluginNameFiles();
35
36      $this->setTablesList(array('images', 'color_table', 'images_colors'));
37
38
39      $this->css = new GPCCss(dirname($this->getFileLocation()).'/'.$this->getPluginNameFiles().".css");
40    }
41
42    public function __destruct()
43    {
44      unset($this->css);
45      parent::__destruct();
46    }
47
48    public function initEvents()
49    {
50      parent::initEvents();
51      add_event_handler('blockmanager_register_blocks', array(&$this, 'register_blocks') );
52    }
53
54    /*
55      menu block management
56    */
57    public function register_blocks()
58    {
59    }
60
61    /*
62      intialize default values
63    */
64    public function initConfig()
65    {
66      //global $user;
67      $this->config=array(
68        'newInstall' => 'n',
69        'analyze_maxTime' => 0.1,
70        'analyze_colorTable' => 'small',
71        'analyze_pps' => 0,
72        'analyze_itemPerRequest' => 10,
73        'display_gallery_showColors' => 'n',
74        'display_stat_orderType' => 'img',
75        'stat_minPct' => 3.5,
76      );
77    }
78
79
80    /**
81     * return HTML code for a given colorTable
82     *
83     * @param Array $colorTable : a color table, typically made with the
84     *                            ColorStat::getColorTable() function
85     * @param Int $size         : size for colorbox in the HTML table
86     * @return String : HTML code
87     */
88    public function htmlColorTable($colorTable, $size=5, $id="", $class="")
89    {
90      global $template;
91
92      $template->set_filename('color_table_page',
93                    dirname($this->getFileLocation()).'/templates/cstat_color_table.tpl');
94
95      $nbHue=count($colorTable);
96      $nbStep=count($colorTable[0]);
97      $colors=Array();
98      for($step=0;$step<$nbStep;$step++)
99      {
100        $row=Array();
101        for($hue=0;$hue<$nbHue;$hue++)
102        {
103
104          if($colorTable[$hue][$step] instanceof HSV)
105          {
106            $row[]=Array('color' => $colorTable[$hue][$step]->getRGB()->getHexString(), 'pct' => "", 'num' => "");
107          }
108          elseif(is_array($colorTable[$hue][$step]))
109          {
110            $row[]=Array(
111              'color' => (array_key_exists('color', $colorTable[$hue][$step]))?$colorTable[$hue][$step]['color']:"",
112              'pct' => (array_key_exists('pct', $colorTable[$hue][$step]))?$colorTable[$hue][$step]['pct']:"",
113              'num' => (array_key_exists('num', $colorTable[$hue][$step]))?$colorTable[$hue][$step]['num']:"",
114            );
115          }
116          else
117          {
118            $row[]=Array('color' => $colorTable[$hue][$step], 'pct' => "", 'num' => "");
119          }
120        }
121        $colors[]=$row;
122        unset($row);
123      }
124
125      $data=array(
126        'colorTable' => $colors,
127        'size' => $size,
128        'id' => $id,
129        'class' => $class
130      );
131
132      $template->assign('data', $data);
133      unset($data);
134
135      return($template->parse('color_table_page', true));
136    }
137
138    /**
139     * return HTML code for a given colors list
140     *
141     * @param Array $colors : list of colors
142     * @param Int $size     : size for colorbox in the HTML table
143     * @return String : HTML code
144     */
145    public function htmlColorList($colorList, $split=8, $size=5, $id="", $class="", $br='<br>')
146    {
147      global $template;
148
149      $template->set_filename('color_table_page',
150                    dirname($this->getFileLocation()).'/templates/cstat_color_table.tpl');
151
152      $colors=Array();
153
154      $row=0;
155      $num=0;
156      foreach($colorList as $key => $val)
157      {
158        $colors[$row][]=Array('color' => $key, 'pct' => $val['pct'], 'num' => "");
159        $num++;
160        if($num==$split)
161        {
162          $num=0;
163          $row++;
164        }
165      }
166
167      $data=array(
168        'colorTable' => $colors,
169        'size' => $size,
170        'id' => $id,
171        'class' => $class,
172        'br' => $br,
173      );
174
175      $template->assign('data', $data);
176      unset($data);
177
178      return($template->parse('color_table_page', true));
179    }
180
181    /**
182     * returns an array of colors & colors percent of an image
183     *
184     * @param Integer $imageId : id of the image
185     * @return Array('colors' => Array(), 'colors_pct' => Array())
186     */
187    public function getImageColors($imageId)
188    {
189      $returned=Array(
190        'colors' => Array(),
191        'colors_pct' => Array(),
192      );
193
194      $sql="SELECT colors, colors_pct
195            FROM ".$this->tables['images']."
196            WHERE image_id='".$imageId."'";
197      $result=pwg_query($sql);
198      if($result)
199      {
200        while($row=pwg_db_fetch_assoc($result))
201        {
202          $returned['colors']=explode(',', $row['colors']);
203          $returned['colors_pct']=explode(',', $row['colors_pct']);
204        }
205      }
206      return($returned);
207    }
208
209
210
211    /**
212     *  return all HTML&JS code necessary to display a dialogbox to choose
213     *  colors
214     */
215    protected function dialogBoxColor()
216    {
217      global $template;
218
219      $template->set_filename('colors_choose',
220                    dirname($this->getFileLocation()).'/templates/cstat_dialog_colors_choose.tpl');
221
222      $colorTable=$this->getColorTableWithStat();
223
224      $datas=Array(
225        'colorTable' => $this->htmlColorTable(
226                          $colorTable,
227                          ($this->config['analyze_colorTable']=='small')?16:8,
228                          "",
229                          "color0px"
230                        ),
231        //'urlRequest' => $this->getAdminLink(),
232      );
233
234      $template->assign('datas', $datas);
235      unset($data);
236
237      return($template->parse('colors_choose', true));
238    }
239
240
241    /**
242     * return a color table with stat on color
243     *
244     * @return Array : a color table with statistics on colors
245     */
246    protected function getColorTableWithStat()
247    {
248      $generalStats=$this->getGeneralStats();
249
250      $colors=Array();
251      $sql="SELECT color_id, num_images, num_pixels
252            FROM ".$this->tables['color_table']."
253            WHERE num_images > 0
254            ORDER BY color_id ";
255      $result=pwg_query($sql);
256      if($result)
257      {
258        while($row=pwg_db_fetch_assoc($result))
259        {
260          $colors[$row['color_id']]=Array('num_images' => $row['num_images'], 'num_pixels' => $row['num_pixels']);
261        }
262      }
263
264      $colorTable=ColorStat::getColorTable(
265        $this->colorTableSize[$this->config['analyze_colorTable']][0],
266        $this->colorTableSize[$this->config['analyze_colorTable']][1]
267      );
268      foreach($colorTable as $key=>$val)
269      {
270        foreach($val as $key2=>$val2)
271        {
272          $rgb=$val2->getRGB()->getHexString();
273          $colorTable[$key][$key2]=Array(
274            'color' => $rgb,
275            'pct'   => '', //(array_key_exists($rgb, $colors))?sprintf("%.2f", round(100*$colors[$rgb]['num_pixels']/$generalStats['pixelsAnalyzedSum'],2)):"",
276            'num'   => (array_key_exists($rgb, $colors))?$colors[$rgb]['num_images']:"",
277          );
278        }
279      }
280
281      unset($colors);
282      return($colorTable);
283    }
284
285
286
287
288
289    /* ---------------------------------------------------------------------------
290      ajax functions
291    --------------------------------------------------------------------------- */
292
293
294  } //class
295
296?>
Note: See TracBrowser for help on using the repository browser.