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

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

prepare release 1.0.0

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