thumbnail generator script for command line usage
[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
106       $this->tmpl->assign('image_name', $details['name']);
107       $this->tmpl->assign('image_url', 'phpfspot_img.php?idx='. $photo ."&amp;width=". $this->cfg->photo_width);
108
109       if($previous_img) {
110          $this->tmpl->assign('previous_url', "javascript:showImage(". $previous_img .");");
111       }
112
113       if($next_img) {
114          $this->tmpl->assign('next_url', "javascript:showImage(". $next_img .");");
115       }
116
117       $this->tmpl->show("single_photo.tpl");
118
119    } // showPhoto()
120
121    public function getAvailableTags()
122    {
123       foreach($this->avail_tags as $tag)
124       {
125          if(isset($_SESSION['selected_tags']) && in_array($tag, $_SESSION['selected_tags']))
126             continue;
127
128          // return all available (= not selected) tags
129          print "<a href=\"javascript:Tags('add', ". $tag .");\">". $this->tags[$tag] ."</a>&nbsp;";
130
131       }
132
133    } // getAvailableTags()
134
135    public function getSelectedTags()
136    {
137       foreach($this->avail_tags as $tag)
138       {
139          // return all selected tags
140          if(isset($_SESSION['selected_tags']) && in_array($tag, $_SESSION['selected_tags'])) {
141             print "<a href=\"javascript:Tags('del', ". $tag .");\">". $this->tags[$tag] ."</a>&nbsp;";
142          }
143
144       }
145
146    } // getSelectedTags()
147
148    public function addTag($tag)
149    {
150       if(!isset($_SESSION['selected_tags']))
151          $_SESSION['selected_tags'] = Array();
152
153       array_push($_SESSION['selected_tags'], $tag);
154    
155    } // addTag()
156
157    public function delTag($tag)
158    {
159       if(isset($_SESSION['selected_tags'])) {
160          $key = array_search($tag, $_SESSION['selected_tags']);
161          unset($_SESSION['selected_tags'][$key]);
162       }
163
164    } // delTag()
165
166    public function resetTags()
167    {
168       unset($_SESSION['selected_tags']);
169
170    } // resetTags()
171
172    public function getAllTagPhotos()
173    {  
174       $tagged_photos = Array();
175
176       if(isset($_SESSION['selected_tags'])) {
177          $selected = "";
178          foreach($_SESSION['selected_tags'] as $tag)
179             $selected.= $tag .",";
180          $selected = substr($selected, 0, strlen($selected)-1);
181          $result = $this->db->db_query("
182             SELECT DISTINCT photo_id
183                FROM photo_tags pt
184             INNER JOIN photos p
185                ON p.id=pt.photo_id
186             WHERE pt.tag_id IN (". $selected .")
187             ORDER BY p.time ASC
188          ");
189       }
190       else {
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             ORDER BY p.time ASC
197          ");
198       }
199
200       while($row = $this->db->db_fetch_object($result)) {
201          array_push($tagged_photos, $row['photo_id']);
202       }
203
204       return $tagged_photos;
205
206    } // getAllTagPhotos()
207
208    public function showPhotoIndex()
209    {
210       $photos = $this->getAllTagPhotos();
211       $count = count($photos);
212
213       $rows = 0;
214       $cols = 0;
215       $images[$rows] = Array();
216
217       for($i = 0; $i < $count; $i++) {
218
219          $images[$rows][$cols] = $photos[$i];
220
221          if($cols == $this->cfg->thumbs_per_row-1) {
222             $cols = 0;
223             $rows++;
224             $images[$rows] = Array();
225          }
226          else {
227             $cols++;
228          }
229       } 
230
231       // +1 for for smarty's selection iteration
232       $rows++;
233
234          //$images.= "<img src=\"phpfspot_img.php?idx=". $photo ."&amp;width=". $this->cfg->thumb_width ."\" /><br />\n";
235
236       $this->tmpl->assign('count', $count);
237       $this->tmpl->assign('width', $this->cfg->thumb_width);
238       $this->tmpl->assign('images', $images);
239       $this->tmpl->assign('rows', $rows);
240       $this->tmpl->assign('columns', $this->cfg->thumbs_per_row);
241       $this->tmpl->show("photo_index.tpl");
242
243
244    } // showPhotoIndex()
245
246    public function showBubbleDetails($photo, $direction)
247    {
248       if($direction == "up")
249          $direction = "bubbleimg_up";
250       else
251          $direction = "bubbleimg_down";
252
253       $details = $this->get_photo_details($photo);
254
255       $image_url = "phpfspot_img.php?idx=". $photo ."&amp;width=200";
256
257       $filesize = filesize($this->translate_path($details['directory_path'])  ."/". $details['name']);
258       $filesize = rand($filesize/1024, 2);
259
260       $img = getimagesize($this->translate_path($details['directory_path'])  ."/". $details['name']);
261
262       $this->tmpl->assign('file_size', $filesize);
263       $this->tmpl->assign('width', $img[0]);
264       $this->tmpl->assign('height', $img[1]);
265       $this->tmpl->assign('file_name', $details['name']);
266       $this->tmpl->assign('image_id', $direction);
267       $this->tmpl->assign('image_url', $image_url);
268       $this->tmpl->show("bubble_details.tpl");
269
270    } // showBubbleDetails()
271
272    public function showCredits()
273    {
274       $this->tmpl->assign('version', $this->cfg->version);
275       $this->tmpl->assign('product', $this->cfg->product);
276       $this->tmpl->show("credits.tpl");
277
278    } // showCredits()
279
280    public function resize_image($image, $width)
281    {  
282       // if thumbnail already exists, don't recreate it
283       if(file_exists(dirname($image) ."/thumbs/". $width ."_". basename($image)))
284          return;
285
286       $src_img = @imagecreatefromjpeg($image);
287
288       if($src_img)
289       {  
290          /* grabs the height and width */
291          $new_w = imagesx($src_img);
292          $new_h = imagesy($src_img);
293
294          // If requested width is more then the actual image width,
295          // do not generate a thumbnail
296
297          if($width >= $new_w) {
298             imagedestroy($src_img);
299             return;
300          }
301
302          /* calculates aspect ratio */
303          $aspect_ratio = $new_h / $new_w;
304
305          /* sets new size */
306          $new_w = $width;
307          $new_h = abs($new_w * $aspect_ratio);
308
309          /* creates new image of that size */
310          $dst_img = imagecreatetruecolor($new_w,$new_h);
311
312          imagefill($dst_img, 0, 0, ImageColorAllocate($dst_img, 255, 255, 255));
313
314          /* copies resized portion of original image into new image */
315          imagecopyresampled($dst_img,$src_img,0,0,0,0,$new_w,$new_h,imagesx($src_img),imagesy($src_img));
316
317          /* write down new generated file */
318
319          if(!file_exists(dirname($image) ."/thumbs"))
320             mkdir(dirname($image) ."/thumbs");
321
322          $newfile = dirname($image) ."/thumbs/". $width ."_". basename($image);
323          imagejpeg($dst_img, $newfile, 75);
324
325          /* free your mind */
326          imagedestroy($dst_img);
327          imagedestroy($src_img);
328       }
329
330    } // resize_image()
331
332 }
333
334 ?>