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