source: extensions/Histogram/hgram_root.class.inc.php @ 8804

Last change on this file since 8804 was 8509, checked in by grum, 13 years ago

First commit for histogram plugin

  • Property svn:executable set to *
File size: 6.5 KB
Line 
1<?php
2/* -----------------------------------------------------------------------------
3  Plugin     : Histogram
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  HGram_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('hgram_histogram.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 HGram_root extends CommonPlugin
25  {
26    const LOCAL_DIRECTORY='/local/plugins/Histogram/';
27
28    static public $pluginName='Histogram';
29    static public $pluginNameFile='hgram';
30    static public $pluginTables=array('histo');
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     * initialize default values
68     *
69     */
70    public function initConfig()
71    {
72      $this->config=array(
73        'histo_width'  => 256,
74        'histo_height' => 100,
75        'histo_method' => 'L',  // L:luminance, A:average, M:max
76        'color_bg' => 0xffffff,
77        'color_v'  => 0x606060,
78        'color_r'  => 0xC00000,
79        'color_g'  => 0x00C000,
80        'color_b'  => 0x0000C0,
81        'mode_v'   => 'surface',
82        'mode_r'   => 'line',
83        'mode_g'   => 'line',
84        'mode_b'   => 'line'
85      );
86    }
87
88    /**
89     * check if the given picture as histogram values
90     *
91     * @param Integer $id : piwigo image id
92     * @return Bool : true if histogram exists
93     */
94    public function checkHistogram($id)
95    {
96      $sql="SELECT COUNT(imageId) FROM ".$this->tables['histo']."
97            WHERE imageId='$id';";
98      $result=pwg_query($sql);
99      if($result)
100      {
101        $count=0;
102        while($row=pwg_db_fetch_row($result))
103        {
104          $count=$row[0];
105        }
106        return($count>0);
107      }
108      return(false);
109    }
110
111    public function readHistogram($id)
112    {
113      $sql="SELECT path FROM ".IMAGES_TABLE."
114            WHERE id='$id';";
115      $result=pwg_query($sql);
116      if($result)
117      {
118        while($row=pwg_db_fetch_assoc($result))
119        {
120          $fileName=GPCCore::getPiwigoSystemPath().'/'.$row['path'];
121        }
122
123        if(file_exists($fileName))
124        {
125          $values=Histogram::read($fileName);
126
127          $sql="REPLACE INTO ".$this->tables['histo']." VALUES ('$id', '".implode(';',$values['L'])."', '".implode(';',$values['A'])."', '".implode(';',$values['M'])."', '".implode(';',$values['R'])."', '".implode(';',$values['G'])."', '".implode(';',$values['B'])."', '')";
128          $result=pwg_query($sql);
129        }
130      }
131    }
132
133    public function getHistogram($id)
134    {
135      $returned=array(
136        'L' => array(),
137        'A' => array(),
138        'M' => array(),
139        'R' => array(),
140        'G' => array(),
141        'B' => array()
142      );
143      $sql="SELECT valueL AS L, valueA AS A, valueM AS M, valueR AS R, valueG AS G, valueB AS B
144            FROM ".$this->tables['histo']."
145            WHERE imageId='$id';";
146      $result=pwg_query($sql);
147      if($result)
148      {
149        while($row=pwg_db_fetch_assoc($result))
150        {
151          $returned['L']=explode(';', $row['L']);
152          $returned['A']=explode(';', $row['A']);
153          $returned['M']=explode(';', $row['M']);
154          $returned['R']=explode(';', $row['R']);
155          $returned['G']=explode(';', $row['G']);
156          $returned['B']=explode(';', $row['B']);
157        }
158      }
159
160      return($returned);
161    }
162
163
164    public function getHistogramGraph($id, $read=false)
165    {
166      $sql="SELECT graphFile FROM ".$this->tables['histo']."
167            WHERE imageId='$id';";
168      $result=pwg_query($sql);
169      if($result)
170      {
171        $fileName='';
172        $exist=false;
173        while($row=pwg_db_fetch_assoc($result))
174        {
175          $fileName=$row['graphFile'];
176          $exist=true;
177        }
178
179        if(!$exist)
180        {
181          if($read)
182          {
183            self::readHistogram($id);
184          }
185          else
186          {
187            return('');
188          }
189        }
190
191        if($fileName=='' or !file_exists(GPCCore::getPiwigoSystemPath().self::LOCAL_DIRECTORY.$fileName))
192        {
193          $fileName=md5(
194            sprintf("%s-%s",
195              $id,
196              implode('-', $this->config)
197            )
198          ).".png";
199
200          $values=self::getHistogram($id);
201
202          Histogram::build(GPCCore::getPiwigoSystemPath().self::LOCAL_DIRECTORY.$fileName, $values, $this->config);
203
204          $sql="UPDATE ".$this->tables['histo']."
205                SET graphFile='$fileName'
206                WHERE imageId='$id';";
207          pwg_query($sql);
208        }
209
210        return($fileName);
211      }
212      return('');
213    }
214
215    /**
216     * get a valid token for an image (used to build histogram)
217     *
218     * @param Integer $imageId : image id
219     * @return String : valid token
220     */
221    public function getToken($imageId)
222    {
223      global $conf;
224      return(hash_hmac('md5', "$imageId-".session_id(), $conf['secret_key']));
225    }
226
227    /**
228     * purge all histogram graph in directory cache
229     */
230    public function purgeHistoCache()
231    {
232      $files=scandir(GPCCore::getPiwigoSystemPath().self::LOCAL_DIRECTORY);
233      foreach($files as $fileName)
234      {
235        if(!is_dir($fileName)) unlink(GPCCore::getPiwigoSystemPath().self::LOCAL_DIRECTORY.'/'.$fileName);
236      }
237
238      $sql="UPDATE ".$this->tables['histo']."
239            SET graphFile='';";
240      pwg_query($sql);
241    }
242
243
244  } //class
245
246
247?>
Note: See TracBrowser for help on using the repository browser.