de2adc086f001265f2d53123c7048493f10f187d
[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       $this->cfg_db = new PHPFSPOT_DB(&$this, $this->cfg->phpfspot_db);
23       $this->tmpl = new PHPFSPOT_TMPL($this);
24
25       $this->get_tags();
26
27    } // __construct()
28
29    public function __destruct()
30    {
31
32    } // __destruct()
33
34    public function show()
35    {
36       $this->tmpl->assign('page_title', $this->cfg->page_title);
37       $this->tmpl->show("index.tpl");
38
39    } // show()
40
41    private function get_tags()
42    {
43       $this->avail_tags = Array();
44       $count = 0;
45    
46       $result = $this->db->db_query("
47          SELECT id,name
48          FROM tags
49          ORDER BY sort_priority ASC
50       ");
51       
52       while($row = $this->db->db_fetch_object($result)) {
53
54          $tag_id = $row['id'];
55          $tag_name = $row['name'];
56
57          $this->tags[$tag_id] = $tag_name; 
58          $this->avail_tags[$count] = $tag_id;
59
60          $count++;
61
62       }
63
64    } // get_tags()
65
66    public function get_photo_details($idx)
67    {
68       $result = $this->db->db_query("
69          SELECT *
70          FROM photos
71          WHERE id='". $idx ."'
72       ");
73       
74       return $this->db->db_fetch_object($result);
75
76    } // get_photo_details
77
78    public function translate_path($path, $width = 0)
79    {  
80       return str_replace($this->cfg->path_replace_from, $this->cfg->path_replace_to, $path);
81
82    } // translate_path
83
84    public function showPhoto($photo)
85    {
86       $all_photos = $this->getAllTagPhotos();
87
88       foreach($all_photos as $all_photo) {
89          
90          if($get_next) {
91             $next_img = $all_photo;
92             break;
93          }
94
95          if($all_photo == $photo) {
96             $get_next = 1;
97          }
98          else {
99             $previous_img = $all_photo;
100          }
101       }
102
103
104       $details = $this->get_photo_details($photo);
105       $meta = $this->get_meta_informations($this->translate_path($details['directory_path']) ."/". $details['name']);
106       $info = getimagesize($this->translate_path($details['directory_path']) ."/thumbs/". $this->cfg->photo_width ."_". $details['name']);
107
108       $this->tmpl->assign('width', $info[0]);
109       $this->tmpl->assign('height', $info[1]);
110       $this->tmpl->assign('c_date', $meta['DateTime']);
111       $this->tmpl->assign('madewith', $meta['Make'] ." ". $meta['Model']);
112       $this->tmpl->assign('image_name', $details['name']);
113       $this->tmpl->assign('image_url', 'phpfspot_img.php?idx='. $photo ."&amp;width=". $this->cfg->photo_width);
114       $this->tmpl->assign('image_url_full', 'phpfspot_img.php?idx='. $photo);
115
116       if($previous_img) {
117          $this->tmpl->assign('previous_url', "javascript:showImage(". $previous_img .");");
118       }
119
120       if($next_img) {
121          $this->tmpl->assign('next_url', "javascript:showImage(". $next_img .");");
122       }
123
124       $this->tmpl->show("single_photo.tpl");
125
126    } // showPhoto()
127
128    public function getAvailableTags()
129    {
130       foreach($this->avail_tags as $tag)
131       {
132          if(isset($_SESSION['selected_tags']) && in_array($tag, $_SESSION['selected_tags']))
133             continue;
134
135          // return all available (= not selected) tags
136          print "<a href=\"javascript:Tags('add', ". $tag .");\">". $this->tags[$tag] ."</a>&nbsp;";
137
138       }
139
140    } // getAvailableTags()
141
142    public function getSelectedTags()
143    {
144       foreach($this->avail_tags as $tag)
145       {
146          // return all selected tags
147          if(isset($_SESSION['selected_tags']) && in_array($tag, $_SESSION['selected_tags'])) {
148             print "<a href=\"javascript:Tags('del', ". $tag .");\">". $this->tags[$tag] ."</a>&nbsp;";
149          }
150
151       }
152
153    } // getSelectedTags()
154
155    public function addTag($tag)
156    {
157       if(!isset($_SESSION['selected_tags']))
158          $_SESSION['selected_tags'] = Array();
159
160       array_push($_SESSION['selected_tags'], $tag);
161    
162    } // addTag()
163
164    public function delTag($tag)
165    {
166       if(isset($_SESSION['selected_tags'])) {
167          $key = array_search($tag, $_SESSION['selected_tags']);
168          unset($_SESSION['selected_tags'][$key]);
169       }
170
171    } // delTag()
172
173    public function resetTags()
174    {
175       unset($_SESSION['selected_tags']);
176
177    } // resetTags()
178
179    public function getAllTagPhotos()
180    {  
181       $tagged_photos = Array();
182
183       if(isset($_SESSION['selected_tags'])) {
184          $selected = "";
185          foreach($_SESSION['selected_tags'] as $tag)
186             $selected.= $tag .",";
187          $selected = substr($selected, 0, strlen($selected)-1);
188          $result = $this->db->db_query("
189             SELECT DISTINCT photo_id
190                FROM photo_tags pt
191             INNER JOIN photos p
192                ON p.id=pt.photo_id
193             WHERE pt.tag_id IN (". $selected .")
194             ORDER BY p.time ASC
195          ");
196       }
197       else {
198          $result = $this->db->db_query("
199             SELECT DISTINCT photo_id
200                FROM photo_tags pt
201             INNER JOIN photos p
202                ON p.id=pt.photo_id
203             ORDER BY p.time ASC
204          ");
205       }
206
207       while($row = $this->db->db_fetch_object($result)) {
208          array_push($tagged_photos, $row['photo_id']);
209       }
210
211       return $tagged_photos;
212
213    } // getAllTagPhotos()
214
215    public function showPhotoIndex()
216    {
217       $photos = $this->getAllTagPhotos();
218       $count = count($photos);
219
220       $rows = 0;
221       $cols = 0;
222       $images[$rows] = Array();
223
224       for($i = 0; $i < $count; $i++) {
225
226          $images[$rows][$cols] = $photos[$i];
227
228          if($cols == $this->cfg->thumbs_per_row-1) {
229             $cols = 0;
230             $rows++;
231             $images[$rows] = Array();
232          }
233          else {
234             $cols++;
235          }
236       } 
237
238       // +1 for for smarty's selection iteration
239       $rows++;
240
241          //$images.= "<img src=\"phpfspot_img.php?idx=". $photo ."&amp;width=". $this->cfg->thumb_width ."\" /><br />\n";
242
243       $this->tmpl->assign('count', $count);
244       $this->tmpl->assign('width', $this->cfg->thumb_width);
245       $this->tmpl->assign('images', $images);
246       $this->tmpl->assign('rows', $rows);
247       $this->tmpl->assign('columns', $this->cfg->thumbs_per_row);
248       $this->tmpl->show("photo_index.tpl");
249
250
251    } // showPhotoIndex()
252
253    public function showBubbleDetails($photo, $direction)
254    {
255       if($direction == "up")
256          $direction = "bubbleimg_up";
257       else
258          $direction = "bubbleimg_down";
259
260       $details = $this->get_photo_details($photo);
261
262       $image_url = "phpfspot_img.php?idx=". $photo ."&amp;width=200";
263
264       $filesize = filesize($this->translate_path($details['directory_path'])  ."/". $details['name']);
265       $filesize = rand($filesize/1024, 2);
266
267       $img = getimagesize($this->translate_path($details['directory_path'])  ."/". $details['name']);
268
269       $this->tmpl->assign('file_size', $filesize);
270       $this->tmpl->assign('width', $img[0]);
271       $this->tmpl->assign('height', $img[1]);
272       $this->tmpl->assign('file_name', $details['name']);
273       $this->tmpl->assign('image_id', $direction);
274       $this->tmpl->assign('image_url', $image_url);
275       $this->tmpl->show("bubble_details.tpl");
276
277    } // showBubbleDetails()
278
279    public function showCredits()
280    {
281       $this->tmpl->assign('version', $this->cfg->version);
282       $this->tmpl->assign('product', $this->cfg->product);
283       $this->tmpl->show("credits.tpl");
284
285    } // showCredits()
286
287    public function resize_image($image, $width)
288    {  
289       // if thumbnail already exists, don't recreate it
290       if(file_exists(dirname($image) ."/thumbs/". $width ."_". basename($image)))
291          return;
292
293       $src_img = @imagecreatefromjpeg($image);
294
295       if($src_img)
296       {  
297          /* grabs the height and width */
298          $new_w = imagesx($src_img);
299          $new_h = imagesy($src_img);
300
301          // If requested width is more then the actual image width,
302          // do not generate a thumbnail
303
304          if($width >= $new_w) {
305             imagedestroy($src_img);
306             return;
307          }
308
309          /* calculates aspect ratio */
310          $aspect_ratio = $new_h / $new_w;
311
312          /* sets new size */
313          $new_w = $width;
314          $new_h = abs($new_w * $aspect_ratio);
315
316          /* creates new image of that size */
317          $dst_img = imagecreatetruecolor($new_w,$new_h);
318
319          imagefill($dst_img, 0, 0, ImageColorAllocate($dst_img, 255, 255, 255));
320
321          /* copies resized portion of original image into new image */
322          imagecopyresampled($dst_img,$src_img,0,0,0,0,$new_w,$new_h,imagesx($src_img),imagesy($src_img));
323
324          /* write down new generated file */
325
326          if(!file_exists(dirname($image) ."/thumbs"))
327             mkdir(dirname($image) ."/thumbs");
328
329          $newfile = dirname($image) ."/thumbs/". $width ."_". basename($image);
330          imagejpeg($dst_img, $newfile, 75);
331
332          /* free your mind */
333          imagedestroy($dst_img);
334          imagedestroy($src_img);
335       }
336
337    } // resize_image()
338
339    public function get_meta_informations($file)
340    {
341
342       return exif_read_data($file);
343
344    } // get_meta_informations()
345
346 }
347
348 ?>