c14274212bc1cc29fe98428bb3c4781888c496a3
[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
472             if($current_page == $i)
473                $style = "style=\"font-size: 125%;\"";
474             elseif($current_page-1 == $i || $current_page+1 == $i)
475                $style = "style=\"font-size: 105%;\"";
476             elseif(($current_page-5 >= $i) && ($i != 1) ||
477                ($current_page+5 <= $i) && ($i != $last_page))
478                $style = "style=\"font-size: 75%;\"";
479             else
480                $style = "";
481
482             $page_select.= "<a href=\"javascript:showPhotoIndex(". (($i*$photo_per_page)-$photo_per_page) .");\"";
483             if($style != "")
484                $page_select.= $style;
485             $page_select.= ">". $i ."</a>&nbsp;";
486
487          }
488
489          $this->tmpl->assign('page_selector', $page_select);
490       }
491
492       $current_tags = "";
493       if($_SESSION['selected_tags'] != "") {
494          foreach($_SESSION['selected_tags'] as $tag)
495             $current_tags.= $tag .",";
496          $current_tags = substr($current_tags, 0, strlen($current_tags)-1);
497       }
498
499       $extern_link = "http://". $_SERVER['SERVER_NAME'] ."/index.php?mode=showpi&tags=". $current_tags;
500
501       $this->tmpl->assign('extern_link', $extern_link);
502       $this->tmpl->assign('count', $count);
503       $this->tmpl->assign('width', $this->cfg->thumb_width);
504       $this->tmpl->assign('images', $images);
505       $this->tmpl->assign('img_width', $img_width);
506       $this->tmpl->assign('img_height', $img_height);
507       $this->tmpl->assign('rows', $rows);
508       $this->tmpl->assign('columns', $this->cfg->thumbs_per_row);
509       $this->tmpl->show("photo_index.tpl");
510
511
512    } // showPhotoIndex()
513
514    public function showBubbleDetails($photo, $direction)
515    {
516       if($direction == "up")
517          $direction = "bubbleimg_up";
518       else
519          $direction = "bubbleimg_down";
520
521       $details = $this->get_photo_details($photo);
522       $orig_path = $this->translate_path($details['directory_path'])  ."/". $details['name'];
523
524       $image_url = "phpfspot_img.php?idx=". $photo ."&amp;width=". $this->cfg->bubble_width;
525
526       $filesize = filesize($orig_path);
527       $filesize = rand($filesize/1024, 2);
528
529       if(!file_exists($orig_path)) {
530          $this->_warning("Photo ". $orig_path ." does not exist!<br />\n");
531          return;
532       }
533       
534       if(!is_readable($orig_path)) {
535          $this->_warning("Photo ". $orig_path ." is not readable for user ". get_current_user() ."<br />\n");
536          return;
537       }
538
539       $img = getimagesize($orig_path);
540
541       $this->tmpl->assign('file_size', $filesize);
542       $this->tmpl->assign('width', $img[0]);
543       $this->tmpl->assign('height', $img[1]);
544       $this->tmpl->assign('file_name', $details['name']);
545       $this->tmpl->assign('image_id', $direction);
546       $this->tmpl->assign('image_url', $image_url);
547       $this->tmpl->show("bubble_details.tpl");
548
549    } // showBubbleDetails()
550
551    public function showCredits()
552    {
553       $this->tmpl->assign('version', $this->cfg->version);
554       $this->tmpl->assign('product', $this->cfg->product);
555       $this->tmpl->show("credits.tpl");
556
557    } // showCredits()
558
559    public function create_thumbnail($orig_image, $thumb_image, $width)
560    {  
561       if(!file_exists($orig_image))
562          return false;
563
564       $details = getimagesize($orig_image);
565       
566       /* check if original photo is a support image type */
567       if(!$this->checkifImageSupported($details['mime']))
568          return false;
569
570       $meta = $this->get_meta_informations($orig_image);
571
572       $rotate = 0;
573       $flip = false;
574
575       switch($meta['Orientation']) {
576
577          case 1:
578             $rotate = 0; $flip = false; break;
579          case 2:
580             $rotate = 0; $flip = true; break;
581          case 3:
582             $rotate = 180; $flip = false; break;
583          case 4:
584             $rotate = 180; $flip = true; break;
585          case 5:
586             $rotate = 90; $flip = true; break;
587          case 6:
588             $rotate = 90; $flip = false; break;
589          case 7:
590             $rotate = 270; $flip = true; break;
591          case 8:
592             $rotate = 270; $flip = false; break;
593       }
594
595       $src_img = @imagecreatefromjpeg($orig_image);
596
597       if(!$src_img) {
598          print "Can't load image from ". $orig_image ."\n";
599          return false;
600       }
601
602       /* grabs the height and width */
603       $new_w = imagesx($src_img);
604       $new_h = imagesy($src_img);
605
606       // If requested width is more then the actual image width,
607       // do not generate a thumbnail
608
609       if($width >= $new_w) {
610          imagedestroy($src_img);
611          return true;
612       }
613
614       /* calculates aspect ratio */
615       $aspect_ratio = $new_h / $new_w;
616
617       /* sets new size */
618       $new_w = $width;
619       $new_h = abs($new_w * $aspect_ratio);
620
621       /* creates new image of that size */
622       $dst_img = imagecreatetruecolor($new_w, $new_h);
623
624       imagefill($dst_img, 0, 0, ImageColorAllocate($dst_img, 255, 255, 255));
625
626       /* copies resized portion of original image into new image */
627       imagecopyresampled($dst_img,$src_img,0,0,0,0,$new_w,$new_h,imagesx($src_img),imagesy($src_img));
628
629       /* needs the image to be flipped horizontal? */
630       if($flip) {
631          print "(FLIP)";
632          $image = $dst_img;
633          for($x = 0; $x < $new_w; $x++) {
634             imagecopy($dst_img, $image, $x, 0, $w - $x - 1, 0, 1, $h);
635          }
636       }
637
638       if($rotate) {
639          print "(ROTATE)";
640          $dst_img = $this->rotateImage($dst_img, $rotate);
641       }
642
643       /* write down new generated file */
644       $result = imagejpeg($dst_img, $thumb_image, 75);
645
646       /* free your mind */
647       imagedestroy($dst_img);
648       imagedestroy($src_img);
649
650       if($result === false) {
651          print "Can't write thumbnail ". $thumb_image ."\n";
652          return false;
653       }
654
655       return true;
656
657    } // create_thumbnail()
658
659    public function get_meta_informations($file)
660    {
661       return exif_read_data($file);
662
663    } // get_meta_informations()
664
665    public function check_config_table()
666    {
667       // if the config table doesn't exist yet, create it
668       if(!$this->cfg_db->db_check_table_exists("images")) {
669          $this->cfg_db->db_exec("
670             CREATE TABLE images (
671                img_idx int primary key,
672                img_md5 varchar(32)
673             )
674             ");
675       }
676
677    } // check_config_table
678
679    /**
680     * Generates a thumbnail from photo idx
681     *
682     * This function will generate JPEG thumbnails from provided F-Spot photo
683     * indizes.
684     *
685     * 1. Check if all thumbnail generations (width) are already in place and
686     *    readable
687     * 2. Check if the md5sum of the original file has changed
688     * 3. Generate the thumbnails if needed
689     */
690    public function gen_thumb($idx = 0, $force = 0)
691    {
692       $error = 0;
693
694       $resolutions = Array(
695          $this->cfg->thumb_width,
696          $this->cfg->bubble_width,
697          $this->cfg->photo_width,
698       );
699
700       /* get details from F-Spot's database */
701       $details = $this->get_photo_details($idx);
702
703       /* calculate file MD5 sum */
704       $full_path = $this->translate_path($details['directory_path'])  ."/". $details['name'];
705
706       if(!file_exists($full_path)) {
707          $this->_warning("File ". $full_path ." does not exist\n");
708          return;
709       }
710
711       if(!is_readable($full_path)) {
712          $this->_warning("File ". $full_path ." is not readable for ". get_current_user() ."\n");
713          return;
714       }
715
716       $file_md5 = md5_file($full_path);
717
718       $this->_debug("Image [". $idx ."] ". $details['name'] ." Thumbnails:");
719
720       foreach($resolutions as $resolution) {
721
722          $thumb_path = $this->cfg->base_path ."/thumbs/". $resolution ."_". $file_md5;
723
724          /* if the thumbnail file doesn't exist, create it */
725          if(!file_exists($thumb_path)) {
726
727             $this->_debug(" ". $resolution ."px");
728             if(!$this->create_thumbnail($full_path, $thumb_path, $resolution))
729                $error = 1;
730          }
731
732          /* if the file hasn't changed there is no need to regen the thumb */
733          elseif($file_md5 != $this->getMD5($idx) || $force) {
734
735             $this->_debug(" ". $resolution ."px");
736             if(!$this->create_thumbnail($full_path, $thumb_path, $resolution))
737                $error = 1;
738
739          }
740       }
741
742       /* set the new/changed MD5 sum for the current photo */
743       if(!$error) {
744          $this->setMD5($idx, $file_md5);
745       }
746
747       $this->_debug("\n");
748
749    } // gen_thumb()
750
751    public function getMD5($idx)
752    {
753       $result = $this->cfg_db->db_query("
754          SELECT img_md5 
755          FROM images
756          WHERE img_idx='". $idx ."'
757       ");
758
759       if(!$result)
760          return 0;
761
762       $img = $this->cfg_db->db_fetch_object($result);
763       return $img['img_md5'];
764       
765    } // getMD5()
766
767    private function setMD5($idx, $md5)
768    {
769       $result = $this->cfg_db->db_exec("
770          REPLACE INTO images (img_idx, img_md5)
771          VALUES ('". $idx ."', '". $md5 ."')
772       ");
773
774    } // setMD5()
775
776    public function setTagCondition($mode)
777    {
778       $_SESSION['tag_condition'] = $mode;
779
780    } // setTagCondition()
781
782    public function startTagSearch($searchfor)
783    {
784       $_SESSION['searchfor'] = $searchfor;
785       $_SESSION['selected_tags'] = Array();
786
787       foreach($this->avail_tags as $tag) {
788          if(preg_match('/'. $searchfor .'/i', $this->tags[$tag]))
789             array_push($_SESSION['selected_tags'], $tag);
790       }
791
792    } // startTagSearch()
793
794    private function rotateImage($img, $degrees)
795    {
796       if(function_exists("imagerotate"))
797          $img = imagerotate($img, $degrees, 0);
798       else
799       {
800          function imagerotate($src_img, $angle)
801          {
802             $src_x = imagesx($src_img);
803             $src_y = imagesy($src_img);
804             if ($angle == 180) {
805                $dest_x = $src_x;
806                $dest_y = $src_y;
807             }
808             elseif ($src_x <= $src_y) {
809                $dest_x = $src_y;
810                $dest_y = $src_x;
811             }
812             elseif ($src_x >= $src_y) {
813                $dest_x = $src_y;
814                $dest_y = $src_x;
815             }
816                
817             $rotate=imagecreatetruecolor($dest_x,$dest_y);
818             imagealphablending($rotate, false);
819                
820             switch ($angle) {
821             
822                case 90:
823                   for ($y = 0; $y < ($src_y); $y++) {
824                      for ($x = 0; $x < ($src_x); $x++) {
825                         $color = imagecolorat($src_img, $x, $y);
826                         imagesetpixel($rotate, $dest_x - $y - 1, $x, $color);
827                      }
828                   }
829                   break;
830
831                case 270:
832                   for ($y = 0; $y < ($src_y); $y++) {
833                      for ($x = 0; $x < ($src_x); $x++) {
834                         $color = imagecolorat($src_img, $x, $y);
835                         imagesetpixel($rotate, $y, $dest_y - $x - 1, $color);
836                      }
837                   }
838                   break;
839
840                case 180:
841                   for ($y = 0; $y < ($src_y); $y++) {
842                      for ($x = 0; $x < ($src_x); $x++) {
843                         $color = imagecolorat($src_img, $x, $y);
844                         imagesetpixel($rotate, $dest_x - $x - 1, $dest_y - $y - 1, $color);
845                      }
846                   }
847                   break;
848
849                default: $rotate = $src_img;
850             };
851
852             return $rotate;
853
854          }
855
856          $img = imagerotate($img, $degrees);
857
858       }
859
860       return $img;
861
862    } // rotateImage()
863
864    private function get_photo_tags($idx)
865    {
866       $result = $this->db->db_query("
867          SELECT t.id, t.name
868          FROM tags t
869          INNER JOIN photo_tags pt
870             ON t.id=pt.tag_id
871          WHERE pt.photo_id='". $idx ."'
872       ");
873
874       $tags = Array();
875
876       while($row = $this->db->db_fetch_object($result))
877          $tags[$row['id']] = $row['name'];
878
879       return $tags;
880
881    } // get_photo_tags()
882
883    public function showTextImage($txt, $color=000000, $space=4, $font=4, $w=300)
884    {
885       if (strlen($color) != 6) 
886          $color = 000000;
887
888       $int = hexdec($color);
889       $h = imagefontheight($font);
890       $fw = imagefontwidth($font);
891       $txt = explode("\n", wordwrap($txt, ($w / $fw), "\n"));
892       $lines = count($txt);
893       $im = imagecreate($w, (($h * $lines) + ($lines * $space)));
894       $bg = imagecolorallocate($im, 255, 255, 255);
895       $color = imagecolorallocate($im, 0xFF & ($int >> 0x10), 0xFF & ($int >> 0x8), 0xFF & $int);
896       $y = 0;
897
898       foreach ($txt as $text) {
899          $x = (($w - ($fw * strlen($text))) / 2);
900          imagestring($im, $font, $x, $y, $text, $color);
901          $y += ($h + $space);
902       }
903
904       Header("Content-type: image/png");
905       ImagePng($im);
906
907    } // showTextImage()
908
909    private function checkRequirements()
910    {
911       if(!function_exists("imagecreatefromjpeg")) {
912          print "PHP GD library extension is missing<br />\n";
913          $missing = true;
914       }
915
916       if(!function_exists("sqlite3_open")) {
917          print "PHP SQLite3 library extension is missing<br />\n";
918          $missing = true;
919       }
920
921       /* Check for HTML_AJAX PEAR package, lent from Horde project */
922       ini_set('track_errors', 1);
923       @include_once 'HTML/AJAX/Server.php';
924       if(isset($php_errormsg)) {
925          print "PEAR HTML_AJAX package is missing<br />\n";
926          $missing = true;
927       }
928       ini_restore('track_errors');
929
930       if(isset($missing))
931          return false;
932
933       return true;
934
935    } // checkRequirements()
936
937    private function _debug($text)
938    {
939       if($this->fromcmd) {
940          print $text;
941       }
942
943    } // _debug()
944
945    public function checkifImageSupported($mime)
946    {
947       if(in_array($mime, Array("image/jpeg")))
948          return true;
949
950       return false;
951
952    } // checkifImageSupported()
953
954    public function _warning($text)
955    {
956       print "<img src=\"resources/green_info.png\" alt=\"warning\" />\n";
957       print $text;
958
959    } // _warning()
960
961 }
962
963 ?>