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

Last change on this file since 15907 was 15350, checked in by grum, 12 years ago

feature:2639 - Compatibility with Piwigo 2.4
Fix some problem with user interface display

  • Property svn:executable set to *
File size: 13.4 KB
RevLine 
[5961]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  --------------------------------------------------------------------------- */
[6107]15
16  if(!defined('PHPWG_ROOT_PATH')) die('Hacking attempt!');
17
18
[10630]19  include_once('cstat_version.inc.php'); // => Don't forget to update this file !!
[5961]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  {
[6176]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
[5961]32    protected $css;   //the css object
33
34    public function __construct($prefixeTable, $filelocation)
35    {
[6176]36      $this->setPluginName(self::$pluginName);
37      $this->setPluginNameFiles(self::$pluginNameFile);
[5961]38      parent::__construct($prefixeTable, $filelocation);
39      $this->section_name=$this->getPluginNameFiles();
40
[6176]41      $this->setTablesList(self::$pluginTables);
[5961]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',
[6176]74        'analyze_maxTime' => 1,
[6107]75        'analyze_colorTable' => 'small',
76        'analyze_pps' => 0,
[6210]77        'analyze_ppsQuality' => 7500,
[6107]78        'analyze_itemPerRequest' => 10,
79        'display_gallery_showColors' => 'n',
[6210]80        'display_gallery_colorSize' => 15,
[6107]81        'display_stat_orderType' => 'img',
[6176]82        'stat_minPct' => 1.5,
[5961]83      );
84    }
85
[6176]86    /* ---------------------------------------------------------------------------
87      ajax functions
88    --------------------------------------------------------------------------- */
[5961]89
[6210]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      );
[6176]106
[6210]107      if($update)
108      {
109        $this->config['analyze_pps']=ColorStat::$fileColorsStat['pps'];
110        $this->saveConfig();
111      }
112
113      return(ColorStat::$fileColorsStat['pps']);
114    }
115
[6176]116  } //class
117
118
119  class CStat_functions
120  {
121    static private $tables = Array();
122    static private $config = Array();
123
[5961]124    /**
[6176]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    /**
[5961]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     */
[6176]198    static public function htmlColorTable($colorTable, $size=5, $id="", $class="", $br='<br>')
[5961]199    {
200      global $template;
201
202      $template->set_filename('color_table_page',
[6176]203                    dirname(__FILE__).'/templates/cstat_color_table.tpl');
[5961]204
[6176]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();
[5961]242      $nbHue=count($colorTable);
243      $nbStep=count($colorTable[0]);
[6176]244      $col=0;
245      $row=0;
246      foreach($colorTable as $key => $hue)
[5961]247      {
[6176]248        $lrow=0;
249        foreach($hue as $key2 => $saturation)
[5961]250        {
[6176]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;
[5961]257
[6176]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++;
[5961]275          }
[6176]276          $lrow++;
[5961]277        }
[6176]278
279        $col++;
280        if($col==$break)
281        {
282          $col=0;
283          $row++;
284        }
[5961]285      }
286
287      $data=array(
288        'colorTable' => $colors,
289        'size' => $size,
290        'id' => $id,
[6176]291        'class' => $class,
292        'br' => $br,
[5961]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     */
[6176]308    static public function htmlColorList($colorList, $split=8, $size=5, $id="", $class="", $br='<br>')
[5961]309    {
310      global $template;
311
312      $template->set_filename('color_table_page',
[6176]313                    dirname(__FILE__).'/templates/cstat_color_table.tpl');
[5961]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,
[6107]334        'class' => $class,
335        'br' => $br,
[5961]336      );
337
338      $template->assign('data', $data);
339      unset($data);
340
341      return($template->parse('color_table_page', true));
342    }
343
[6107]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     */
[6176]350    static public function getImageColors($imageId)
[6107]351    {
352      $returned=Array(
353        'colors' => Array(),
354        'colors_pct' => Array(),
355      );
[5961]356
[6107]357      $sql="SELECT colors, colors_pct
[6176]358            FROM ".self::$tables['images']."
[6107]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    }
[5961]371
372
[6107]373    /**
374     *  return all HTML&JS code necessary to display a dialogbox to choose
375     *  colors
376     */
[6176]377    static public function dialogBoxColor()
[5961]378    {
[6107]379      global $template;
[5961]380
[15350]381      GPCCore::addUI('simpleTip');
[6107]382      $template->set_filename('colors_choose',
[6176]383                    dirname(__FILE__).'/templates/cstat_dialog_colors_choose.tpl');
[6107]384
[6176]385      $colorTable=CStat_functions::getColorTableWithStat();
[6107]386
387      $datas=Array(
[6176]388        'colorTable' => CStat_functions::htmlColorTable(
[6107]389                          $colorTable,
[6176]390                          (self::$config['analyze_colorTable']=='small')?16:8,
[6107]391                          "",
392                          "color0px"
393                        ),
394        //'urlRequest' => $this->getAdminLink(),
395      );
396
397      $template->assign('datas', $datas);
398      unset($data);
399
400      return($template->parse('colors_choose', true));
401    }
402
403
404    /**
[6176]405     * returns general stats on the analyzed colors process
[6107]406     *
[6176]407     * @return Array : array with keys
408     *                  'nbImages',  'timeMax',  'timeMin', 'timeSum',
409     *                  'pixelsAnalyzedMax', 'pixelsAnalyzedMin',
410     *                  'pixelsAnalyzedAvg', 'pixelsAnalyzedSum', 'totalPixels',
411     *                  'ppsMax', 'ppsMin', 'ppsAvg', 'qualityMax',
412     *                  'qualityMin', 'qualityAvg'
[6107]413     */
[6176]414    static public function getGeneralStats()
[6107]415    {
[6176]416      $returned=Array(
417        'nbImages' => 0,
418        'timeMax' => 0,
419        'timeMin' => 0,
420        'timeSum' => 0,
421        'pixelsAnalyzedMax' => 0,
422        'pixelsAnalyzedMin' => 0,
423        'pixelsAnalyzedAvg' => 0,
424        'pixelsAnalyzedSum' => 0,
425        'totalPixels' => 0,
426        'ppsMax' => 0,
427        'ppsMin' => 0,
428        'ppsAvg' => 0,
429        'qualityMax' => 0,
430        'qualityMin' => 0,
431        'qualityAvg' => 0,
432      );
433      $sql="SELECT COUNT(image_id) AS nbImages,
434                   MAX(time) AS timeMax,
435                   MIN(time) AS timeMin,
436                   SUM(time) AS timeSum,
437                   MAX(analyzed_pixels) AS pixelsAnalyzedMax,
438                   MIN(analyzed_pixels) AS pixelsAnalyzedMin,
439                   AVG(analyzed_pixels) AS pixelsAnalyzedAvg,
440                   SUM(analyzed_pixels) AS pixelsAnalyzedSum,
441                   SUM(num_pixels) AS totalPixels,
442                   MAX(pps) AS ppsMax,
443                   MIN(pps) AS ppsMin,
444                   AVG(pps) AS ppsAvg,
445                   MAX(quality) AS qualityMax,
446                   MIN(quality) AS qualityMin,
447                   AVG(quality) AS qualityAvg
448            FROM ".self::$tables['images']."
449            WHERE analyzed='y';";
[6107]450      $result=pwg_query($sql);
[5961]451      if($result)
452      {
[6107]453        while($row=pwg_db_fetch_assoc($result))
454        {
[6176]455          $returned=$row;
[6107]456        }
[5961]457      }
[6176]458      return($returned);
[5961]459    }
460
[6176]461  } //CStat_functions
[5961]462
463?>
Note: See TracBrowser for help on using the repository browser.