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

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

Improve algorythm for colors analysis + use GPCRequestBuilder interface instead of a local interface

  • Property svn:executable set to *
File size: 12.2 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    static public $pluginName='ColorStat';
27    static public $pluginNameFile='cstat';
28    static public $pluginTables=array('images', 'color_table', 'images_colors');
29    static public $colorTableSize=Array('small' => Array(30,20), 'large' => Array(10,10));
30
31    protected $css;   //the css object
32
33    public function __construct($prefixeTable, $filelocation)
34    {
35      $this->setPluginName(self::$pluginName);
36      $this->setPluginNameFiles(self::$pluginNameFile);
37      parent::__construct($prefixeTable, $filelocation);
38      $this->section_name=$this->getPluginNameFiles();
39
40      $this->setTablesList(self::$pluginTables);
41
42
43      $this->css = new GPCCss(dirname($this->getFileLocation()).'/'.$this->getPluginNameFiles().".css");
44    }
45
46    public function __destruct()
47    {
48      unset($this->css);
49      parent::__destruct();
50    }
51
52    public function initEvents()
53    {
54      parent::initEvents();
55      add_event_handler('blockmanager_register_blocks', array(&$this, 'register_blocks') );
56    }
57
58    /*
59      menu block management
60    */
61    public function register_blocks()
62    {
63    }
64
65    /*
66      intialize default values
67    */
68    public function initConfig()
69    {
70      //global $user;
71      $this->config=array(
72        'newInstall' => 'n',
73        'analyze_maxTime' => 1,
74        'analyze_colorTable' => 'small',
75        'analyze_pps' => 0,
76        'analyze_itemPerRequest' => 10,
77        'display_gallery_showColors' => 'n',
78        'display_stat_orderType' => 'img',
79        'stat_minPct' => 1.5,
80      );
81    }
82
83    /* ---------------------------------------------------------------------------
84      ajax functions
85    --------------------------------------------------------------------------- */
86
87
88  } //class
89
90
91  class CStat_functions
92  {
93    static private $tables = Array();
94    static private $config = Array();
95
96    /**
97     * initialise the class
98     *
99     * @param String $prefixeTable : the piwigo prefixe used on tables name
100     * @param String $pluginNameFile : the plugin name used for tables name
101     */
102    static public function init($prefixeTable)
103    {
104      GPCCore::loadConfig(CStat_root::$pluginNameFile, self::$config);
105      $list=CStat_root::$pluginTables;
106
107      for($i=0;$i<count($list);$i++)
108      {
109        self::$tables[$list[$i]]=$prefixeTable.CStat_root::$pluginNameFile.'_'.$list[$i];
110      }
111    }
112
113    /**
114     * return a color table with stat on color
115     *
116     * @return Array : a color table with statistics on colors
117     */
118    static public function getColorTableWithStat()
119    {
120      $generalStats=self::getGeneralStats();
121
122      $colors=Array();
123      $sql="SELECT color_id, num_images, num_pixels
124            FROM ".self::$tables['color_table']."
125            WHERE num_images > 0
126            ORDER BY color_id ";
127      $result=pwg_query($sql);
128      if($result)
129      {
130        while($row=pwg_db_fetch_assoc($result))
131        {
132          $colors[$row['color_id']]=Array('num_images' => $row['num_images'], 'num_pixels' => $row['num_pixels']);
133        }
134      }
135
136      $colorTable=ColorStat::getColorTable(
137       CStat_root::$colorTableSize[self::$config['analyze_colorTable']][0],
138       CStat_root::$colorTableSize[self::$config['analyze_colorTable']][1]
139      );
140
141      foreach($colorTable as $key=>$hue)
142      {
143        foreach($hue as $key2=>$saturation)
144        {
145          foreach($saturation as $key3=>$value)
146          {
147            $rgb=$value->getRGB()->getHexString();
148            $colorTable[$key][$key2][$key3]=Array(
149              'color' => $rgb,
150              'pct'   => '', //(array_key_exists($rgb, $colors))?sprintf("%.2f", round(100*$colors[$rgb]['num_pixels']/$generalStats['pixelsAnalyzedSum'],2)):"",
151              'num'   => (array_key_exists($rgb, $colors))?$colors[$rgb]['num_images']:"",
152            );
153          }
154        }
155      }
156
157      unset($colors);
158      return($colorTable);
159    }
160
161
162    /**
163     * return HTML code for a given colorTable
164     *
165     * @param Array $colorTable : a color table, typically made with the
166     *                            ColorStat::getColorTable() function
167     * @param Int $size         : size for colorbox in the HTML table
168     * @return String : HTML code
169     */
170    static public function htmlColorTable($colorTable, $size=5, $id="", $class="", $br='<br>')
171    {
172      global $template;
173
174      $template->set_filename('color_table_page',
175                    dirname(__FILE__).'/templates/cstat_color_table.tpl');
176
177      switch(count($colorTable))
178      {
179        case 12;
180          $break=4;
181          $size=10;
182          break;
183        case 15;
184          $break=5;
185          $size=10;
186          break;
187        case 18;
188          $break=6;
189          $size=5;
190          break;
191        case 20;
192          $break=5;
193          $size=10;
194          break;
195        case 24;
196          $break=6;
197          $size=5;
198          break;
199        case 30;
200          $break=6;
201          $size=5;
202          break;
203        case 36;
204          $break=6;
205          $size=5;
206          break;
207        case 45;
208          $break=9;
209          $size=3;
210          break;
211      }
212
213      $colors=Array();
214      $nbHue=count($colorTable);
215      $nbStep=count($colorTable[0]);
216      $col=0;
217      $row=0;
218      foreach($colorTable as $key => $hue)
219      {
220        $lrow=0;
221        foreach($hue as $key2 => $saturation)
222        {
223          $lcol=0;
224          foreach($saturation as $key3 => $value)
225          {
226            //if(!($col!=0 and $hsv['V']==0) and !($row!=0 and $hsv['S']==0))
227            $trow=$lcol+$col*$nbStep;
228            $tcol=$lrow+$row*$nbStep;
229
230            if($colorTable[$key][$key2][$key3] instanceof HSV)
231            {
232              $colors[$tcol][$trow]=Array('color' => $colorTable[$key][$key2][$key3]->getRGB()->getHexString(), 'pct' => "", 'num' => "");
233            }
234            elseif(is_array($colorTable[$key][$key2][$key3]))
235            {
236              $colors[$tcol][$trow]=Array(
237                'color' => (array_key_exists('color', $colorTable[$key][$key2][$key3]))?$colorTable[$key][$key2][$key3]['color']:"",
238                'pct' => (array_key_exists('pct', $colorTable[$key][$key2][$key3]))?$colorTable[$key][$key2][$key3]['pct']:"",
239                'num' => (array_key_exists('num', $colorTable[$key][$key2][$key3]))?$colorTable[$key][$key2][$key3]['num']:"",
240              );
241            }
242            else
243            {
244              $colors[$tcol][$trow]=Array('color' => $colorTable[$key][$key2][$key3], 'pct' => "", 'num' => "");
245            }
246            $lcol++;
247          }
248          $lrow++;
249        }
250
251        $col++;
252        if($col==$break)
253        {
254          $col=0;
255          $row++;
256        }
257      }
258
259      $data=array(
260        'colorTable' => $colors,
261        'size' => $size,
262        'id' => $id,
263        'class' => $class,
264        'br' => $br,
265      );
266
267      $template->assign('data', $data);
268      unset($data);
269
270      return($template->parse('color_table_page', true));
271    }
272
273    /**
274     * return HTML code for a given colors list
275     *
276     * @param Array $colors : list of colors
277     * @param Int $size     : size for colorbox in the HTML table
278     * @return String : HTML code
279     */
280    static public function htmlColorList($colorList, $split=8, $size=5, $id="", $class="", $br='<br>')
281    {
282      global $template;
283
284      $template->set_filename('color_table_page',
285                    dirname(__FILE__).'/templates/cstat_color_table.tpl');
286
287      $colors=Array();
288
289      $row=0;
290      $num=0;
291      foreach($colorList as $key => $val)
292      {
293        $colors[$row][]=Array('color' => $key, 'pct' => $val['pct'], 'num' => "");
294        $num++;
295        if($num==$split)
296        {
297          $num=0;
298          $row++;
299        }
300      }
301
302      $data=array(
303        'colorTable' => $colors,
304        'size' => $size,
305        'id' => $id,
306        'class' => $class,
307        'br' => $br,
308      );
309
310      $template->assign('data', $data);
311      unset($data);
312
313      return($template->parse('color_table_page', true));
314    }
315
316    /**
317     * returns an array of colors & colors percent of an image
318     *
319     * @param Integer $imageId : id of the image
320     * @return Array('colors' => Array(), 'colors_pct' => Array())
321     */
322    static public function getImageColors($imageId)
323    {
324      $returned=Array(
325        'colors' => Array(),
326        'colors_pct' => Array(),
327      );
328
329      $sql="SELECT colors, colors_pct
330            FROM ".self::$tables['images']."
331            WHERE image_id='".$imageId."'";
332      $result=pwg_query($sql);
333      if($result)
334      {
335        while($row=pwg_db_fetch_assoc($result))
336        {
337          $returned['colors']=explode(',', $row['colors']);
338          $returned['colors_pct']=explode(',', $row['colors_pct']);
339        }
340      }
341      return($returned);
342    }
343
344
345    /**
346     *  return all HTML&JS code necessary to display a dialogbox to choose
347     *  colors
348     */
349    static public function dialogBoxColor()
350    {
351      global $template;
352
353      $template->set_filename('colors_choose',
354                    dirname(__FILE__).'/templates/cstat_dialog_colors_choose.tpl');
355
356      $colorTable=CStat_functions::getColorTableWithStat();
357
358      $datas=Array(
359        'colorTable' => CStat_functions::htmlColorTable(
360                          $colorTable,
361                          (self::$config['analyze_colorTable']=='small')?16:8,
362                          "",
363                          "color0px"
364                        ),
365        //'urlRequest' => $this->getAdminLink(),
366      );
367
368      $template->assign('datas', $datas);
369      unset($data);
370
371      return($template->parse('colors_choose', true));
372    }
373
374
375    /**
376     * returns general stats on the analyzed colors process
377     *
378     * @return Array : array with keys
379     *                  'nbImages',  'timeMax',  'timeMin', 'timeSum',
380     *                  'pixelsAnalyzedMax', 'pixelsAnalyzedMin',
381     *                  'pixelsAnalyzedAvg', 'pixelsAnalyzedSum', 'totalPixels',
382     *                  'ppsMax', 'ppsMin', 'ppsAvg', 'qualityMax',
383     *                  'qualityMin', 'qualityAvg'
384     */
385    static public function getGeneralStats()
386    {
387      $returned=Array(
388        'nbImages' => 0,
389        'timeMax' => 0,
390        'timeMin' => 0,
391        'timeSum' => 0,
392        'pixelsAnalyzedMax' => 0,
393        'pixelsAnalyzedMin' => 0,
394        'pixelsAnalyzedAvg' => 0,
395        'pixelsAnalyzedSum' => 0,
396        'totalPixels' => 0,
397        'ppsMax' => 0,
398        'ppsMin' => 0,
399        'ppsAvg' => 0,
400        'qualityMax' => 0,
401        'qualityMin' => 0,
402        'qualityAvg' => 0,
403      );
404      $sql="SELECT COUNT(image_id) AS nbImages,
405                   MAX(time) AS timeMax,
406                   MIN(time) AS timeMin,
407                   SUM(time) AS timeSum,
408                   MAX(analyzed_pixels) AS pixelsAnalyzedMax,
409                   MIN(analyzed_pixels) AS pixelsAnalyzedMin,
410                   AVG(analyzed_pixels) AS pixelsAnalyzedAvg,
411                   SUM(analyzed_pixels) AS pixelsAnalyzedSum,
412                   SUM(num_pixels) AS totalPixels,
413                   MAX(pps) AS ppsMax,
414                   MIN(pps) AS ppsMin,
415                   AVG(pps) AS ppsAvg,
416                   MAX(quality) AS qualityMax,
417                   MIN(quality) AS qualityMin,
418                   AVG(quality) AS qualityAvg
419            FROM ".self::$tables['images']."
420            WHERE analyzed='y';";
421      $result=pwg_query($sql);
422      if($result)
423      {
424        while($row=pwg_db_fetch_assoc($result))
425        {
426          $returned=$row;
427        }
428      }
429      return($returned);
430    }
431
432  } //CStat_functions
433
434?>
Note: See TracBrowser for help on using the repository browser.