tag ignore list
[phpfspot.git] / phpfspot.class.php
1 <?php
2
3 require_once "phpfspot_cfg.php";
4 require_once "phpfspot_db.php";
5 require_once "phpfspot_tmpl.php";
6
7 class PHPFSPOT {
8
9    var $cfg;
10    var $db;
11    var $cfg_db;
12    var $tmpl;
13    var $tags;
14    var $avail_tags;
15    var $current_tags;
16
17    public function __construct()
18    {
19       $this->cfg = new PHPFSPOT_CFG;
20
21       $this->db = new PHPFSPOT_DB(&$this, $this->cfg->fspot_db);
22
23       $this->cfg_db = new PHPFSPOT_DB(&$this, $this->cfg->phpfspot_db);
24       $this->check_config_table();
25
26       $this->tmpl = new PHPFSPOT_TMPL($this);
27
28       $this->get_tags();
29
30    } // __construct()
31
32    public function __destruct()
33    {
34
35    } // __destruct()
36
37    public function show()
38    {
39       $this->tmpl->assign('page_title', $this->cfg->page_title);
40       $this->tmpl->show("index.tpl");
41
42    } // show()
43
44    private function get_tags()
45    {
46       $this->avail_tags = Array();
47       $count = 0;
48    
49       $result = $this->db->db_query("
50          SELECT id,name
51          FROM tags
52          ORDER BY sort_priority ASC
53       ");
54       
55       while($row = $this->db->db_fetch_object($result)) {
56
57          $tag_id = $row['id'];
58          $tag_name = $row['name'];
59
60          /* check if config requests to ignore this tag */
61          if(in_array($row['name'], $this->cfg->hide_tags))
62             continue;
63
64          $this->tags[$tag_id] = $tag_name; 
65          $this->avail_tags[$count] = $tag_id;
66
67          $count++;
68
69       }
70
71    } // get_tags()
72
73    public function get_photo_details($idx)
74    {
75       $result = $this->db->db_query("
76          SELECT *
77          FROM photos
78          WHERE id='". $idx ."'
79       ");
80       
81       return $this->db->db_fetch_object($result);
82
83    } // get_photo_details
84
85    public function translate_path($path, $width = 0)
86    {  
87       return str_replace($this->cfg->path_replace_from, $this->cfg->path_replace_to, $path);
88
89    } // translate_path
90
91    public function showPhoto($photo)
92    {
93       $all_photos = $this->getAllTagPhotos();
94
95       foreach($all_photos as $all_photo) {
96          
97          if($get_next) {
98             $next_img = $all_photo;
99             break;
100          }
101
102          if($all_photo == $photo) {
103             $get_next = 1;
104          }
105          else {
106             $previous_img = $all_photo;
107          }
108       }
109
110
111       $details = $this->get_photo_details($photo);
112       $meta = $this->get_meta_informations($this->translate_path($details['directory_path']) ."/". $details['name']);
113       $info = getimagesize($this->translate_path($details['directory_path']) ."/thumbs/". $this->cfg->photo_width ."_". $details['name']);
114
115       $this->tmpl->assign('width', $info[0]);
116       $this->tmpl->assign('height', $info[1]);
117       $this->tmpl->assign('c_date', $meta['DateTime']);
118       $this->tmpl->assign('madewith', $meta['Make'] ." ". $meta['Model']);
119       $this->tmpl->assign('image_name', $details['name']);
120       $this->tmpl->assign('image_url', 'phpfspot_img.php?idx='. $photo ."&amp;width=". $this->cfg->photo_width);
121       $this->tmpl->assign('image_url_full', 'phpfspot_img.php?idx='. $photo);
122
123       if($previous_img) {
124          $this->tmpl->assign('previous_url', "javascript:showImage(". $previous_img .");");
125       }
126
127       if($next_img) {
128          $this->tmpl->assign('next_url', "javascript:showImage(". $next_img .");");
129       }
130
131       $this->tmpl->show("single_photo.tpl");
132
133    } // showPhoto()
134
135    public function getAvailableTags()
136    {
137       foreach($this->avail_tags as $tag)
138       {
139          if(isset($_SESSION['selected_tags']) && in_array($tag, $_SESSION['selected_tags']))
140             continue;
141
142          // return all available (= not selected) tags
143          print "<a href=\"javascript:Tags('add', ". $tag .");\">". $this->tags[$tag] ."</a>&nbsp;";
144
145       }
146
147    } // getAvailableTags()
148
149    public function getSelectedTags()
150    {
151       foreach($this->avail_tags as $tag)
152       {
153          // return all selected tags
154          if(isset($_SESSION['selected_tags']) && in_array($tag, $_SESSION['selected_tags'])) {
155             print "<a href=\"javascript:Tags('del', ". $tag .");\">". $this->tags[$tag] ."</a>&nbsp;";
156          }
157
158       }
159
160    } // getSelectedTags()
161
162    public function addTag($tag)
163    {
164       if(!isset($_SESSION['selected_tags']))
165          $_SESSION['selected_tags'] = Array();
166
167       array_push($_SESSION['selected_tags'], $tag);
168    
169    } // addTag()
170
171    public function delTag($tag)
172    {
173       if(isset($_SESSION['selected_tags'])) {
174          $key = array_search($tag, $_SESSION['selected_tags']);
175          unset($_SESSION['selected_tags'][$key]);
176       }
177
178    } // delTag()
179
180    public function resetTags()
181    {
182       unset($_SESSION['selected_tags']);
183
184    } // resetTags()
185
186    public function getAllTagPhotos()
187    {  
188       $tagged_photos = Array();
189
190       if(isset($_SESSION['selected_tags'])) {
191          $selected = "";
192          foreach($_SESSION['selected_tags'] as $tag)
193             $selected.= $tag .",";
194          $selected = substr($selected, 0, strlen($selected)-1);
195          $result = $this->db->db_query("
196             SELECT DISTINCT photo_id
197                FROM photo_tags pt
198             INNER JOIN photos p
199                ON p.id=pt.photo_id
200             WHERE pt.tag_id IN (". $selected .")
201             ORDER BY p.time ASC
202          ");
203       }
204       else {
205          $result = $this->db->db_query("
206             SELECT DISTINCT photo_id
207                FROM photo_tags pt
208             INNER JOIN photos p
209                ON p.id=pt.photo_id
210             ORDER BY p.time ASC
211          ");
212       }
213
214       while($row = $this->db->db_fetch_object($result)) {
215          array_push($tagged_photos, $row['photo_id']);
216       }
217
218       return $tagged_photos;
219
220    } // getAllTagPhotos()
221
222    public function showPhotoIndex()
223    {
224       $photos = $this->getAllTagPhotos();
225       $count = count($photos);
226
227       $rows = 0;
228       $cols = 0;
229       $images[$rows] = Array();
230
231       for($i = 0; $i < $count; $i++) {
232
233          $images[$rows][$cols] = $photos[$i];
234
235          if($cols == $this->cfg->thumbs_per_row-1) {
236             $cols = 0;
237             $rows++;
238             $images[$rows] = Array();
239          }
240          else {
241             $cols++;
242          }
243       } 
244
245       // +1 for for smarty's selection iteration
246       $rows++;
247
248          //$images.= "<img src=\"phpfspot_img.php?idx=". $photo ."&amp;width=". $this->cfg->thumb_width ."\" /><br />\n";
249
250       $this->tmpl->assign('count', $count);
251       $this->tmpl->assign('width', $this->cfg->thumb_width);
252       $this->tmpl->assign('images', $images);
253       $this->tmpl->assign('rows', $rows);
254       $this->tmpl->assign('columns', $this->cfg->thumbs_per_row);
255       $this->tmpl->show("photo_index.tpl");
256
257
258    } // showPhotoIndex()
259
260    public function showBubbleDetails($photo, $direction)
261    {
262       if($direction == "up")
263          $direction = "bubbleimg_up";
264       else
265          $direction = "bubbleimg_down";
266
267       $details = $this->get_photo_details($photo);
268
269       $image_url = "phpfspot_img.php?idx=". $photo ."&amp;width=". $this->cfg->bubble_width;
270
271       $filesize = filesize($this->translate_path($details['directory_path'])  ."/". $details['name']);
272       $filesize = rand($filesize/1024, 2);
273
274       $img = getimagesize($this->translate_path($details['directory_path'])  ."/". $details['name']);
275
276       $this->tmpl->assign('file_size', $filesize);
277       $this->tmpl->assign('width', $img[0]);
278       $this->tmpl->assign('height', $img[1]);
279       $this->tmpl->assign('file_name', $details['name']);
280       $this->tmpl->assign('image_id', $direction);
281       $this->tmpl->assign('image_url', $image_url);
282       $this->tmpl->show("bubble_details.tpl");
283
284    } // showBubbleDetails()
285
286    public function showCredits()
287    {
288       $this->tmpl->assign('version', $this->cfg->version);
289       $this->tmpl->assign('product', $this->cfg->product);
290       $this->tmpl->show("credits.tpl");
291
292    } // showCredits()
293
294    public function create_thumbnail($image, $width)
295    {  
296       // if thumbnail already exists, don't recreate it
297       if(file_exists(dirname($image) ."/thumbs/". $width ."_". basename($image)))
298          return;
299
300       $src_img = @imagecreatefromjpeg($image);
301
302       if($src_img)
303       {  
304          /* grabs the height and width */
305          $new_w = imagesx($src_img);
306          $new_h = imagesy($src_img);
307
308          // If requested width is more then the actual image width,
309          // do not generate a thumbnail
310
311          if($width >= $new_w) {
312             imagedestroy($src_img);
313             return;
314          }
315
316          /* calculates aspect ratio */
317          $aspect_ratio = $new_h / $new_w;
318
319          /* sets new size */
320          $new_w = $width;
321          $new_h = abs($new_w * $aspect_ratio);
322
323          /* creates new image of that size */
324          $dst_img = imagecreatetruecolor($new_w,$new_h);
325
326          imagefill($dst_img, 0, 0, ImageColorAllocate($dst_img, 255, 255, 255));
327
328          /* copies resized portion of original image into new image */
329          imagecopyresampled($dst_img,$src_img,0,0,0,0,$new_w,$new_h,imagesx($src_img),imagesy($src_img));
330
331          /* write down new generated file */
332
333          if(!file_exists(dirname($image) ."/thumbs"))
334             mkdir(dirname($image) ."/thumbs");
335
336          $newfile = dirname($image) ."/thumbs/". $width ."_". basename($image);
337          imagejpeg($dst_img, $newfile, 75);
338
339          /* free your mind */
340          imagedestroy($dst_img);
341          imagedestroy($src_img);
342       }
343
344    } // create_thumbnail()
345
346    public function get_meta_informations($file)
347    {
348
349       return exif_read_data($file);
350
351    } // get_meta_informations()
352
353    public function check_config_table()
354    {
355       // if the config table doesn't exist yet, create it
356       if(!$this->cfg_db->db_check_table_exists("images")) {
357          $this->cfg_db->db_exec("
358             CREATE TABLE images (
359                img_idx int primary key,
360                img_md5 varchar(32)
361             )
362             ");
363       }
364
365    } // check_config_table
366
367    public function gen_thumbs($idx = 0, $fromcmd = 0)
368    {
369       if(!$idx) {
370          /* get all available photos */
371          $all = $this->getAllTagPhotos();
372       }
373       else
374          $all = Array($idx);
375       
376       foreach($all as $photo) {
377
378          $details = $this->get_photo_details($photo);
379
380          $full_path = $this->translate_path($details['directory_path'])  ."/". $details['name'];
381          $file_md5 = md5_file($full_path);
382
383          if($fromcmd) print "Image ". $details['name'] ." Thumbnails:";
384
385          /* if the file hasn't changed there is no need to regen the thumb */
386          if($file_md5 == $this->getMD5($photo)) {
387             if($fromcmd) print " file has not changed - skipping\n";
388             continue;
389          }
390
391          /* set the new/changed MD5 sum for the current photo */
392          $this->setMD5($photo, $file_md5);
393
394          $resolutions = Array(
395                            $this->cfg->thumb_width,
396                            $this->cfg->bubble_width,
397                            $this->cfg->photo_width
398                         );
399
400          /* create thumbnails for the requested resolutions */
401          foreach($resolutions as $resolution) {
402             if($fromcmd) print " ". $resolution ."px";
403             $this->create_thumbnail($full_path, $resolution);
404          }
405
406          if($fromcmd) print "\n";
407
408       }
409
410    } // gen_thumbs()
411
412    private function getMD5($idx)
413    {
414       $result = $this->cfg_db->db_query("
415          SELECT img_md5 
416          FROM images
417          WHERE img_idx='". $idx ."'
418       ");
419
420       if(!$result)
421          return 0;
422
423       $img = $this->cfg_db->db_fetch_object($result);
424       return $img['img_md5'];
425       
426    } // getMD5()
427
428    private function setMD5($idx, $md5)
429    {
430       $result = $this->cfg_db->db_exec("
431          REPLACE INTO images (img_idx, img_md5)
432          VALUES ('". $idx ."', '". $md5 ."')
433       ");
434
435    } // setMD5()
436
437 }
438
439 ?>