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