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

Last change on this file since 14518 was 10630, checked in by grum, 13 years ago

bug:2147 ; Compatibility with Piwigo 2.2

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