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