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