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