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

Last change on this file since 28566 was 16010, checked in by grum, 12 years ago

feature:2639- compatibility with Piwigo 2.4

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