some cleanup in gen_thumb code
[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       /* Check necessary requirements */
20       if(!$this->checkRequirements()) {
21          exit(1);
22       }
23
24       $this->cfg = new PHPFSPOT_CFG;
25
26       $this->db = new PHPFSPOT_DB(&$this, $this->cfg->fspot_db);
27
28       $this->cfg_db = new PHPFSPOT_DB(&$this, $this->cfg->phpfspot_db);
29       $this->check_config_table();
30
31       $this->tmpl = new PHPFSPOT_TMPL($this);
32
33       $this->get_tags();
34
35       session_start();
36
37       if(!isset($_SESSION['tag_condition']))
38          $_SESSION['tag_condition'] = 'or';
39
40       if(!isset($_SESSION['searchfor']))
41          $_SESSION['searchfor'] = '';
42
43    } // __construct()
44
45    public function __destruct()
46    {
47
48    } // __destruct()
49
50    public function show()
51    {
52       $this->tmpl->assign('searchfor', $_SESSION['searchfor']);
53       $this->tmpl->assign('page_title', $this->cfg->page_title);
54       $this->tmpl->assign('current_condition', $_SESSION['tag_condition']);
55
56       switch($_GET['mode']) {
57          case 'showpi':
58             if(isset($_GET['tags'])) {
59                $_SESSION['selected_tags'] = split(',', $_GET['tags']);
60             }
61             break;
62       }
63
64       $this->tmpl->assign('content_page', 'welcome.tpl');
65       $this->tmpl->show("index.tpl");
66
67
68    } // show()
69
70    private function get_tags()
71    {
72       $this->avail_tags = Array();
73       $count = 0;
74    
75       $result = $this->db->db_query("
76          SELECT id,name
77          FROM tags
78          ORDER BY sort_priority ASC
79       ");
80       
81       while($row = $this->db->db_fetch_object($result)) {
82
83          $tag_id = $row['id'];
84          $tag_name = $row['name'];
85
86          /* check if config requests to ignore this tag */
87          if(in_array($row['name'], $this->cfg->hide_tags))
88             continue;
89
90          $this->tags[$tag_id] = $tag_name; 
91          $this->avail_tags[$count] = $tag_id;
92
93          $count++;
94
95       }
96
97    } // get_tags()
98
99    public function get_photo_details($idx)
100    {
101       $result = $this->db->db_query("
102          SELECT *
103          FROM photos
104          WHERE id='". $idx ."'
105       ");
106       
107       return $this->db->db_fetch_object($result);
108
109    } // get_photo_details
110
111    public function translate_path($path, $width = 0)
112    {  
113       return str_replace($this->cfg->path_replace_from, $this->cfg->path_replace_to, $path);
114
115    } // translate_path
116
117    public function showPhoto($photo)
118    {
119       $all_photos = $this->getPhotoSelection();
120
121       foreach($all_photos as $all_photo) {
122          
123          if($get_next) {
124             $next_img = $all_photo;
125             break;
126          }
127
128          if($all_photo == $photo) {
129             $get_next = 1;
130          }
131          else {
132             $previous_img = $all_photo;
133          }
134       }
135
136       $details = $this->get_photo_details($photo);
137       $orig_path = $this->translate_path($details['directory_path']) ."/". $details['name'];
138       $thumb_path = $this->cfg->base_path ."/thumbs/". $this->cfg->photo_width ."_". $this->getMD5($photo);
139
140       if(!file_exists($orig_path)) {
141          print "Photo ". $orig_path ." does not exist!<br />\n";
142       }
143
144       if(!is_readable($orig_path)) {
145          print "Photo ". $orig_path ." is not readable for user ". get_current_user() ."<br />\n";
146       }
147
148       /* If the thumbnail doesn't exist yet, try to create it */
149       if(!file_exists($thumb_path)) {
150          $this->gen_thumb($photo, true);
151       }
152
153       $meta = $this->get_meta_informations($orig_path);
154
155       if(file_exists($thumb_path)) {
156
157          $info = getimagesize($thumb_path);
158
159          $this->tmpl->assign('description', $details['description']);
160          $this->tmpl->assign('image_name', $details['name']);
161
162          $this->tmpl->assign('width', $info[0]);
163          $this->tmpl->assign('height', $info[1]);
164          $this->tmpl->assign('ExifMadeOn', strftime("%a %x %X", $meta['FileDateTime']));
165          $this->tmpl->assign('ExifMadeWith', $meta['Make'] ." ". $meta['Model']);
166          $this->tmpl->assign('ExifOrigResolution', $meta['ExifImageWidth'] ."x". $meta['ExifImageLength']);
167          $this->tmpl->assign('ExifFileSize', round($meta['FileSize']/1024, 1));
168     
169          $this->tmpl->assign('image_url', 'phpfspot_img.php?idx='. $photo ."&amp;width=". $this->cfg->photo_width);
170          $this->tmpl->assign('image_url_full', 'phpfspot_img.php?idx='. $photo);
171
172          $this->tmpl->assign('tags', $this->get_photo_tags($photo));
173       }
174       else {
175          print "Can't open file ". $thumb_path ."\n";
176       }
177
178       if($previous_img) {
179          $this->tmpl->assign('previous_url', "javascript:showImage(". $previous_img .");");
180       }
181
182       if($next_img) {
183          $this->tmpl->assign('next_url', "javascript:showImage(". $next_img .");");
184       }
185
186       $this->tmpl->show("single_photo.tpl");
187
188    } // showPhoto()
189
190    public function getAvailableTags()
191    {
192       $result = $this->db->db_query("
193          SELECT tag_id as id, count(tag_id) as quantity
194          FROM photo_tags
195          INNER JOIN tags t
196             ON t.id = tag_id
197          GROUP BY tag_id
198          ORDER BY t.name ASC
199       ");
200
201       $tags = Array();
202
203       while($row = $this->db->db_fetch_object($result)) {
204          $tags[$row['id']] = $row['quantity'];
205       }
206
207       // change these font sizes if you will
208       $max_size = 125; // max font size in %
209       $min_size = 75; // min font size in %
210
211       // get the largest and smallest array values
212       $max_qty = max(array_values($tags));
213       $min_qty = min(array_values($tags));
214
215       // find the range of values
216       $spread = $max_qty - $min_qty;
217       if (0 == $spread) { // we don't want to divide by zero
218          $spread = 1;
219       }
220
221       // determine the font-size increment
222       // this is the increase per tag quantity (times used)
223       $step = ($max_size - $min_size)/($spread);
224
225       // loop through our tag array
226       foreach ($tags as $key => $value) {
227
228          if(isset($_SESSION['selected_tags']) && in_array($key, $_SESSION['selected_tags']))
229             continue;
230
231           // calculate CSS font-size
232           // find the $value in excess of $min_qty
233           // multiply by the font-size increment ($size)
234           // and add the $min_size set above
235          $size = $min_size + (($value - $min_qty) * $step);
236           // uncomment if you want sizes in whole %:
237          $size = ceil($size);
238
239          print "<a href=\"javascript:Tags('add', ". $key .");\" class=\"tag\" style=\"font-size: ". $size ."%;\">". $this->tags[$key] ."</a>, ";
240
241       }
242
243    } // getAvailableTags()
244
245    public function getSelectedTags()
246    {
247       foreach($this->avail_tags as $tag)
248       {
249          // return all selected tags
250          if(isset($_SESSION['selected_tags']) && in_array($tag, $_SESSION['selected_tags'])) {
251             print "<a href=\"javascript:Tags('del', ". $tag .");\" class=\"tag\">". $this->tags[$tag] ."</a>&nbsp;";
252          }
253
254       }
255
256    } // getSelectedTags()
257
258    public function addTag($tag)
259    {
260       if(!isset($_SESSION['selected_tags']))
261          $_SESSION['selected_tags'] = Array();
262
263       if(!in_array($tag, $_SESSION['selected_tags']))
264          array_push($_SESSION['selected_tags'], $tag);
265    
266    } // addTag()
267
268    public function delTag($tag)
269    {
270       if(isset($_SESSION['selected_tags'])) {
271          $key = array_search($tag, $_SESSION['selected_tags']);
272          unset($_SESSION['selected_tags'][$key]);
273          sort($_SESSION['selected_tags']);
274       }
275
276    } // delTag()
277
278    public function resetTags()
279    {
280       unset($_SESSION['selected_tags']);
281
282    } // resetTags()
283
284    public function getPhotoSelection()
285    {  
286       $tagged_photos = Array();
287
288       /* return a search result */
289       if(isset($_SESSION['searchfor']) && $_SESSION['searchfor'] != '') {
290          $result = $this->db->db_query("
291             SELECT DISTINCT photo_id
292                FROM photo_tags pt
293             INNER JOIN photos p
294                ON p.id=pt.photo_id
295             INNER JOIN tags t
296                ON pt.tag_id=t.id
297             WHERE t.name LIKE '%". $_SESSION['searchfor'] ."%'
298                ORDER BY p.time ASC
299          ");
300          while($row = $this->db->db_fetch_object($result)) {
301             array_push($tagged_photos, $row['photo_id']);
302          }
303          return $tagged_photos;
304       }
305
306       /* return according the selected tags */
307       if(isset($_SESSION['selected_tags']) && !empty($_SESSION['selected_tags'])) {
308          $selected = "";
309          foreach($_SESSION['selected_tags'] as $tag)
310             $selected.= $tag .",";
311          $selected = substr($selected, 0, strlen($selected)-1);
312
313          if($_SESSION['tag_condition'] == 'or') {
314             $result = $this->db->db_query("
315                SELECT DISTINCT photo_id
316                   FROM photo_tags pt
317                INNER JOIN photos p
318                   ON p.id=pt.photo_id
319                WHERE pt.tag_id IN (". $selected .")
320                ORDER BY p.time ASC
321             ");
322          }
323          elseif($_SESSION['tag_condition'] == 'and') {
324
325             /* Join together a table looking like
326
327                pt1.photo_id pt1.tag_id pt2.photo_id pt2.tag_id ...
328
329                so the query can quickly return all images matching the
330                selected tags in an AND condition
331
332             */
333
334             $query_str = "
335                SELECT DISTINCT pt1.photo_id
336                   FROM photo_tags pt1
337             ";
338
339             for($i = 0; $i < count($_SESSION['selected_tags']); $i++) {
340                $query_str.= "
341                   INNER JOIN photo_tags pt". ($i+2) ."
342                      ON pt1.photo_id=pt". ($i+2) .".photo_id
343                ";
344             }
345             $query_str.= "WHERE pt1.tag_id=". $_SESSION['selected_tags'][0];
346             for($i = 1; $i < count($_SESSION['selected_tags']); $i++) {
347                $query_str.= "
348                   AND pt". ($i+1) .".tag_id=". $_SESSION['selected_tags'][$i] ."
349                "; 
350             }
351             $result = $this->db->db_query($query_str);
352          }
353
354          while($row = $this->db->db_fetch_object($result)) {
355             array_push($tagged_photos, $row['photo_id']);
356          }
357          return $tagged_photos;
358       }
359
360       /* return all available photos */
361       $result = $this->db->db_query("
362          SELECT DISTINCT photo_id
363             FROM photo_tags pt
364          INNER JOIN photos p
365             ON p.id=pt.photo_id
366          ORDER BY p.time ASC
367       ");
368       while($row = $this->db->db_fetch_object($result)) {
369          array_push($tagged_photos, $row['photo_id']);
370       }
371       return $tagged_photos;
372
373    } // getPhotoSelection()
374
375    public function showPhotoIndex()
376    {
377       $photos = $this->getPhotoSelection();
378
379       $count = count($photos);
380
381       if(!$_SESSION['begin_with'] || $_SESSION['begin_with'] == 0)
382          $begin_with = 0;
383       else
384          $begin_with = $_SESSION['begin_with'];
385
386       if($this->cfg->rows_per_page == 0)
387          $end_with = $count;
388       else
389          $end_with = $begin_with + ($this->cfg->rows_per_page * $this->cfg->thumbs_per_row);
390
391    
392       $rows = 0;
393       $cols = 0;
394       $images[$rows] = Array();
395       $img_height[$rows] = Array();
396       $img_width[$rows] = Array();
397
398       for($i = $begin_with; $i < $end_with; $i++) {
399
400          $images[$rows][$cols] = $photos[$i];
401
402          $thumb_path = $this->cfg->base_path ."/thumbs/". $this->cfg->thumb_width ."_". $this->getMD5($photos[$i]);
403
404          if(file_exists($thumb_path)) {
405             $info = getimagesize($thumb_path); 
406             $img_width[$rows][$cols] = $info[0];
407             $img_height[$rows][$cols] = $info[1];
408          }
409
410          if($cols == $this->cfg->thumbs_per_row-1) {
411             $cols = 0;
412             $rows++;
413             $images[$rows] = Array();
414             $img_width[$rows] = Array();
415             $img_height[$rows] = Array();
416          }
417          else {
418             $cols++;
419          }
420       } 
421
422       // +1 for for smarty's selection iteration
423       $rows++;
424
425       if(isset($_SESSION['searchfor']) && $_SESSION['searchfor'] != '')
426          $this->tmpl->assign('searchfor', $_SESSION['searchfor']);
427
428       if($this->cfg->rows_per_page != 0) {
429          $previous_start = $begin_with - ($this->cfg->rows_per_page * $this->cfg->thumbs_per_row);
430          $next_start = $begin_with + ($this->cfg->rows_per_page * $this->cfg->thumbs_per_row);
431
432          if($begin_with != 0) 
433             $this->tmpl->assign("previous_url", "javascript:showPhotoIndex(". $previous_start .");"); 
434          if($end_with < $count)
435             $this->tmpl->assign("next_url", "javascript:showPhotoIndex(". $next_start .");"); 
436       }
437
438       $current_tags = "";
439       foreach($_SESSION['selected_tags'] as $tag)
440          $current_tags.= $tag .",";
441       $current_tags = substr($current_tags, 0, strlen($current_tags)-1);
442       $extern_link = "http://". $_SERVER['SERVER_NAME'] ."/index.php?mode=showpi&tags=". $current_tags;
443
444       $this->tmpl->assign('extern_link', $extern_link);
445       $this->tmpl->assign('count', $count);
446       $this->tmpl->assign('width', $this->cfg->thumb_width);
447       $this->tmpl->assign('images', $images);
448       $this->tmpl->assign('img_width', $img_width);
449       $this->tmpl->assign('img_height', $img_height);
450       $this->tmpl->assign('rows', $rows);
451       $this->tmpl->assign('columns', $this->cfg->thumbs_per_row);
452       $this->tmpl->show("photo_index.tpl");
453
454
455    } // showPhotoIndex()
456
457    public function showBubbleDetails($photo, $direction)
458    {
459       if($direction == "up")
460          $direction = "bubbleimg_up";
461       else
462          $direction = "bubbleimg_down";
463
464       $details = $this->get_photo_details($photo);
465       $orig_path = $this->translate_path($details['directory_path'])  ."/". $details['name'];
466
467       $image_url = "phpfspot_img.php?idx=". $photo ."&amp;width=". $this->cfg->bubble_width;
468
469       $filesize = filesize($orig_path);
470       $filesize = rand($filesize/1024, 2);
471
472       if(!file_exists($orig_path)) {
473          print "Photo ". $orig_path ." does not exist!<br />\n";
474          return;
475       }
476       
477       if(!is_readable($orig_path)) {
478          print "Photo ". $orig_path ." is not readable for user ". get_current_user() ."<br />\n";
479          return;
480       }
481
482       $img = getimagesize($orig_path);
483
484       $this->tmpl->assign('file_size', $filesize);
485       $this->tmpl->assign('width', $img[0]);
486       $this->tmpl->assign('height', $img[1]);
487       $this->tmpl->assign('file_name', $details['name']);
488       $this->tmpl->assign('image_id', $direction);
489       $this->tmpl->assign('image_url', $image_url);
490       $this->tmpl->show("bubble_details.tpl");
491
492    } // showBubbleDetails()
493
494    public function showCredits()
495    {
496       $this->tmpl->assign('version', $this->cfg->version);
497       $this->tmpl->assign('product', $this->cfg->product);
498       $this->tmpl->show("credits.tpl");
499
500    } // showCredits()
501
502    public function create_thumbnail($orig_image, $thumb_image, $width)
503    {  
504       if(!file_exists($orig_image))
505          return false;
506
507       $meta = $this->get_meta_informations($orig_image);
508
509       $rotate = 0;
510       $flip = false;
511
512       switch($meta['Orientation']) {
513
514          case 1:
515             $rotate = 0; $flip = false; break;
516          case 2:
517             $rotate = 0; $flip = true; break;
518          case 3:
519             $rotate = 180; $flip = false; break;
520          case 4:
521             $rotate = 180; $flip = true; break;
522          case 5:
523             $rotate = 90; $flip = true; break;
524          case 6:
525             $rotate = 90; $flip = false; break;
526          case 7:
527             $rotate = 270; $flip = true; break;
528          case 8:
529             $rotate = 270; $flip = false; break;
530       }
531
532       $src_img = @imagecreatefromjpeg($orig_image);
533
534       if(!$src_img) {
535          print "Can't load image from ". $orig_image ."\n";
536          return false;
537       }
538
539       /* grabs the height and width */
540       $new_w = imagesx($src_img);
541       $new_h = imagesy($src_img);
542
543       // If requested width is more then the actual image width,
544       // do not generate a thumbnail
545
546       if($width >= $new_w) {
547          imagedestroy($src_img);
548          return true;
549       }
550
551       /* calculates aspect ratio */
552       $aspect_ratio = $new_h / $new_w;
553
554       /* sets new size */
555       $new_w = $width;
556       $new_h = abs($new_w * $aspect_ratio);
557
558       /* creates new image of that size */
559       $dst_img = imagecreatetruecolor($new_w, $new_h);
560
561       imagefill($dst_img, 0, 0, ImageColorAllocate($dst_img, 255, 255, 255));
562
563       /* copies resized portion of original image into new image */
564       imagecopyresampled($dst_img,$src_img,0,0,0,0,$new_w,$new_h,imagesx($src_img),imagesy($src_img));
565
566       /* needs the image to be flipped horizontal? */
567       if($flip) {
568          print "(FLIP)";
569          $image = $dst_img;
570          for($x = 0; $x < $new_w; $x++) {
571             imagecopy($dst_img, $image, $x, 0, $w - $x - 1, 0, 1, $h);
572          }
573       }
574
575       if($rotate) {
576          print "(ROTATE)";
577          $dst_img = $this->rotateImage($dst_img, $rotate);
578       }
579
580       /* write down new generated file */
581       $result = imagejpeg($dst_img, $thumb_image, 75);
582
583       /* free your mind */
584       imagedestroy($dst_img);
585       imagedestroy($src_img);
586
587       if($result === false) {
588          print "Can't write thumbnail ". $thumb_image ."\n";
589          return false;
590       }
591
592       return true;
593
594    } // create_thumbnail()
595
596    public function get_meta_informations($file)
597    {
598       return exif_read_data($file);
599
600    } // get_meta_informations()
601
602    public function check_config_table()
603    {
604       // if the config table doesn't exist yet, create it
605       if(!$this->cfg_db->db_check_table_exists("images")) {
606          $this->cfg_db->db_exec("
607             CREATE TABLE images (
608                img_idx int primary key,
609                img_md5 varchar(32)
610             )
611             ");
612       }
613
614    } // check_config_table
615
616    /**
617     * Generates a thumbnail from photo idx
618     *
619     * This function will generate JPEG thumbnails from provided F-Spot photo
620     * indizes.
621     *
622     * 1. Check if all thumbnail generations (width) are already in place and
623     *    readable
624     * 2. Check if the md5sum of the original file has changed
625     * 3. Generate the thumbnails if needed
626     */
627    public function gen_thumb($idx = 0, $force = 0)
628    {
629       $error = 0;
630
631       $resolutions = Array(
632          $this->cfg->thumb_width,
633          $this->cfg->bubble_width,
634          $this->cfg->photo_width,
635       );
636
637       /* get details from F-Spot's database */
638       $details = $this->get_photo_details($idx);
639
640       /* calculate file MD5 sum */
641       $full_path = $this->translate_path($details['directory_path'])  ."/". $details['name'];
642       $file_md5 = md5_file($full_path);
643
644       $this->_debug("Image [". $idx ."] ". $details['name'] ." Thumbnails:");
645
646       foreach($resolutions as $resolution) {
647
648          $thumb_path = $this->cfg->base_path ."/thumbs/". $resolution ."_". $file_md5;
649
650          /* if the thumbnail file doesn't exist, create it */
651          if(!file_exists($thumb_path)) {
652
653             $this->_debug(" ". $resolution ."px");
654             if(!$this->create_thumbnail($full_path, $thumb_path, $resolution))
655                $error = 1;
656          }
657
658          /* if the file hasn't changed there is no need to regen the thumb */
659          elseif($file_md5 != $this->getMD5($idx) || $force) {
660
661             $this->_debug(" ". $resolution ."px");
662             if(!$this->create_thumbnail($full_path, $thumb_path, $resolution))
663                $error = 1;
664
665          }
666       }
667
668       /* set the new/changed MD5 sum for the current photo */
669       if(!$error) {
670          $this->setMD5($idx, $file_md5);
671       }
672
673       $this->_debug("\n");
674
675    } // gen_thumb()
676
677    public function getMD5($idx)
678    {
679       $result = $this->cfg_db->db_query("
680          SELECT img_md5 
681          FROM images
682          WHERE img_idx='". $idx ."'
683       ");
684
685       if(!$result)
686          return 0;
687
688       $img = $this->cfg_db->db_fetch_object($result);
689       return $img['img_md5'];
690       
691    } // getMD5()
692
693    private function setMD5($idx, $md5)
694    {
695       $result = $this->cfg_db->db_exec("
696          REPLACE INTO images (img_idx, img_md5)
697          VALUES ('". $idx ."', '". $md5 ."')
698       ");
699
700    } // setMD5()
701
702    public function setTagCondition($mode)
703    {
704       $_SESSION['tag_condition'] = $mode;
705
706    } // setTagCondition()
707
708    public function startTagSearch($searchfor)
709    {
710       $_SESSION['searchfor'] = $searchfor;
711       $_SESSION['selected_tags'] = Array();
712
713       foreach($this->avail_tags as $tag) {
714          if(preg_match('/'. $searchfor .'/i', $this->tags[$tag]))
715             array_push($_SESSION['selected_tags'], $tag);
716       }
717
718    } // startTagSearch()
719
720    private function rotateImage($img, $degrees)
721    {
722       if(function_exists("imagerotate"))
723          $img = imagerotate($img, $degrees, 0);
724       else
725       {
726          function imagerotate($src_img, $angle)
727          {
728             $src_x = imagesx($src_img);
729             $src_y = imagesy($src_img);
730             if ($angle == 180) {
731                $dest_x = $src_x;
732                $dest_y = $src_y;
733             }
734             elseif ($src_x <= $src_y) {
735                $dest_x = $src_y;
736                $dest_y = $src_x;
737             }
738             elseif ($src_x >= $src_y) {
739                $dest_x = $src_y;
740                $dest_y = $src_x;
741             }
742                
743             $rotate=imagecreatetruecolor($dest_x,$dest_y);
744             imagealphablending($rotate, false);
745                
746             switch ($angle) {
747             
748                case 90:
749                   for ($y = 0; $y < ($src_y); $y++) {
750                      for ($x = 0; $x < ($src_x); $x++) {
751                         $color = imagecolorat($src_img, $x, $y);
752                         imagesetpixel($rotate, $dest_x - $y - 1, $x, $color);
753                      }
754                   }
755                   break;
756
757                case 270:
758                   for ($y = 0; $y < ($src_y); $y++) {
759                      for ($x = 0; $x < ($src_x); $x++) {
760                         $color = imagecolorat($src_img, $x, $y);
761                         imagesetpixel($rotate, $y, $dest_y - $x - 1, $color);
762                      }
763                   }
764                   break;
765
766                case 180:
767                   for ($y = 0; $y < ($src_y); $y++) {
768                      for ($x = 0; $x < ($src_x); $x++) {
769                         $color = imagecolorat($src_img, $x, $y);
770                         imagesetpixel($rotate, $dest_x - $x - 1, $dest_y - $y - 1, $color);
771                      }
772                   }
773                   break;
774
775                default: $rotate = $src_img;
776             };
777
778             return $rotate;
779
780          }
781
782          $img = imagerotate($img, $degrees);
783
784       }
785
786       return $img;
787
788    } // rotateImage()
789
790    private function get_photo_tags($idx)
791    {
792       $result = $this->db->db_query("
793          SELECT t.id, t.name
794          FROM tags t
795          INNER JOIN photo_tags pt
796             ON t.id=pt.tag_id
797          WHERE pt.photo_id='". $idx ."'
798       ");
799
800       $tags = Array();
801
802       while($row = $this->db->db_fetch_object($result))
803          $tags[$row['id']] = $row['name'];
804
805       return $tags;
806
807    } // get_photo_tags()
808
809    public function showTextImage($txt, $color=000000, $space=4, $font=4, $w=300)
810    {
811       if (strlen($color) != 6) 
812          $color = 000000;
813
814       $int = hexdec($color);
815       $h = imagefontheight($font);
816       $fw = imagefontwidth($font);
817       $txt = explode("\n", wordwrap($txt, ($w / $fw), "\n"));
818       $lines = count($txt);
819       $im = imagecreate($w, (($h * $lines) + ($lines * $space)));
820       $bg = imagecolorallocate($im, 255, 255, 255);
821       $color = imagecolorallocate($im, 0xFF & ($int >> 0x10), 0xFF & ($int >> 0x8), 0xFF & $int);
822       $y = 0;
823
824       foreach ($txt as $text) {
825          $x = (($w - ($fw * strlen($text))) / 2);
826          imagestring($im, $font, $x, $y, $text, $color);
827          $y += ($h + $space);
828       }
829
830       Header("Content-type: image/png");
831       ImagePng($im);
832
833    } // showTextImage()
834
835    private function checkRequirements()
836    {
837       if(!function_exists("imagecreatefromjpeg")) {
838          print "PHP GD library extension is missing<br />\n";
839          $missing = true;
840       }
841
842       if(!function_exists("sqlite3_open")) {
843          print "PHP SQLite3 library extension is missing<br />\n";
844          $missing = true;
845       }
846
847       /* Check for HTML_AJAX PEAR package, lent from Horde project */
848       ini_set('track_errors', 1);
849       @include 'HTML/AJAX/Server.php';
850       if(isset($php_errormsg)) {
851          print "PEAR HTML_AJAX package is missing<br />\n";
852          $missing = true;
853       }
854       ini_restore('track_errors');
855
856       if(isset($missing))
857          return false;
858
859       return true;
860
861    } // checkRequirements()
862
863    private function _debug($text)
864    {
865       if($this->fromcmd) {
866          print $text;
867       }
868
869    } // _debug()
870
871 }
872
873 ?>