3 require_once "phpfspot_cfg.php";
4 require_once "phpfspot_db.php";
5 require_once "phpfspot_tmpl.php";
17 public function __construct()
19 /* Check necessary requirements */
20 if(!$this->checkRequirements()) {
24 $this->cfg = new PHPFSPOT_CFG;
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();
30 $this->tmpl = new PHPFSPOT_TMPL($this);
36 if(!isset($_SESSION['tag_condition']))
37 $_SESSION['tag_condition'] = 'or';
39 if(!isset($_SESSION['searchfor']))
40 $_SESSION['searchfor'] = '';
44 public function __destruct()
49 public function show()
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']);
55 switch($_GET['mode']) {
57 if(isset($_GET['tags'])) {
58 $_SESSION['selected_tags'] = split(',', $_GET['tags']);
63 $this->tmpl->assign('content_page', 'welcome.tpl');
64 $this->tmpl->show("index.tpl");
69 private function get_tags()
72 $this->avail_tags = Array();
75 $result = $this->db->db_query("
78 ORDER BY sort_priority ASC
81 while($row = $this->db->db_fetch_object($result)) {
84 $tag_name = $row['name'];
86 /* check if config requests to ignore this tag */
87 if(in_array($row['name'], $this->cfg->hide_tags))
90 $this->tags[$tag_id] = $tag_name;
91 $this->avail_tags[$count] = $tag_id;
99 public function get_photo_details($idx)
101 $result = $this->db->db_query("
104 WHERE id='". $idx ."'
107 return $this->db->db_fetch_object($result);
109 } // get_photo_details
111 public function translate_path($path, $width = 0)
113 return str_replace($this->cfg->path_replace_from, $this->cfg->path_replace_to, $path);
117 public function showPhoto($photo)
119 $all_photos = $this->getPhotoSelection();
121 foreach($all_photos as $all_photo) {
124 $next_img = $all_photo;
128 if($all_photo == $photo) {
132 $previous_img = $all_photo;
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);
140 if(!file_exists($orig_path)) {
141 $this->_warning("Photo ". $orig_path ." does not exist!<br />\n");
144 if(!is_readable($orig_path)) {
145 $this->_warning("Photo ". $orig_path ." is not readable for user ". get_current_user() ."<br />\n");
148 /* If the thumbnail doesn't exist yet, try to create it */
149 if(!file_exists($thumb_path)) {
150 $this->gen_thumb($photo, true);
153 $meta = $this->get_meta_informations($orig_path);
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";
160 if(file_exists($thumb_path)) {
162 $info = getimagesize($thumb_path);
164 $this->tmpl->assign('description', $details['description']);
165 $this->tmpl->assign('image_name', $details['name']);
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);
174 $this->tmpl->assign('image_url', 'phpfspot_img.php?idx='. $photo ."&width=". $this->cfg->photo_width);
175 $this->tmpl->assign('image_url_full', 'phpfspot_img.php?idx='. $photo);
177 $this->tmpl->assign('tags', $this->get_photo_tags($photo));
180 $this->_warning("Can't open file ". $thumb_path ."\n");
184 $this->tmpl->assign('previous_url', "javascript:showImage(". $previous_img .");");
188 $this->tmpl->assign('next_url', "javascript:showImage(". $next_img .");");
191 $this->tmpl->show("single_photo.tpl");
195 public function getAvailableTags()
197 $result = $this->db->db_query("
198 SELECT tag_id as id, count(tag_id) as quantity
208 while($row = $this->db->db_fetch_object($result)) {
209 $tags[$row['id']] = $row['quantity'];
212 // change these font sizes if you will
213 $max_size = 125; // max font size in %
214 $min_size = 75; // min font size in %
216 // get the largest and smallest array values
217 $max_qty = max(array_values($tags));
218 $min_qty = min(array_values($tags));
220 // find the range of values
221 $spread = $max_qty - $min_qty;
222 if (0 == $spread) { // we don't want to divide by zero
226 // determine the font-size increment
227 // this is the increase per tag quantity (times used)
228 $step = ($max_size - $min_size)/($spread);
230 // loop through our tag array
231 foreach ($tags as $key => $value) {
233 if(isset($_SESSION['selected_tags']) && in_array($key, $_SESSION['selected_tags']))
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 %:
244 print "<a href=\"javascript:Tags('add', ". $key .");\" class=\"tag\" style=\"font-size: ". $size ."%;\">". $this->tags[$key] ."</a>, ";
248 } // getAvailableTags()
250 public function getSelectedTags()
252 foreach($this->avail_tags as $tag)
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> ";
261 } // getSelectedTags()
263 public function addTag($tag)
265 if(!isset($_SESSION['selected_tags']))
266 $_SESSION['selected_tags'] = Array();
268 if(!in_array($tag, $_SESSION['selected_tags']))
269 array_push($_SESSION['selected_tags'], $tag);
273 public function delTag($tag)
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']);
283 public function resetTags()
285 unset($_SESSION['selected_tags']);
289 public function resetTagSearch()
291 unset($_SESSION['searchfor']);
293 } // resetTagSearch()
295 public function getPhotoSelection()
297 $tagged_photos = Array();
299 /* return a search result */
300 if(isset($_SESSION['searchfor']) && $_SESSION['searchfor'] != '') {
301 $result = $this->db->db_query("
302 SELECT DISTINCT photo_id
308 WHERE t.name LIKE '%". $_SESSION['searchfor'] ."%'
311 while($row = $this->db->db_fetch_object($result)) {
312 array_push($tagged_photos, $row['photo_id']);
314 return $tagged_photos;
317 /* return according the selected tags */
318 if(isset($_SESSION['selected_tags']) && !empty($_SESSION['selected_tags'])) {
320 foreach($_SESSION['selected_tags'] as $tag)
321 $selected.= $tag .",";
322 $selected = substr($selected, 0, strlen($selected)-1);
324 if($_SESSION['tag_condition'] == 'or') {
325 $result = $this->db->db_query("
326 SELECT DISTINCT photo_id
330 WHERE pt.tag_id IN (". $selected .")
334 elseif($_SESSION['tag_condition'] == 'and') {
336 /* Join together a table looking like
338 pt1.photo_id pt1.tag_id pt2.photo_id pt2.tag_id ...
340 so the query can quickly return all images matching the
341 selected tags in an AND condition
346 SELECT DISTINCT pt1.photo_id
350 for($i = 0; $i < count($_SESSION['selected_tags']); $i++) {
352 INNER JOIN photo_tags pt". ($i+2) ."
353 ON pt1.photo_id=pt". ($i+2) .".photo_id
356 $query_str.= "WHERE pt1.tag_id=". $_SESSION['selected_tags'][0];
357 for($i = 1; $i < count($_SESSION['selected_tags']); $i++) {
359 AND pt". ($i+1) .".tag_id=". $_SESSION['selected_tags'][$i] ."
362 $result = $this->db->db_query($query_str);
365 while($row = $this->db->db_fetch_object($result)) {
366 array_push($tagged_photos, $row['photo_id']);
368 return $tagged_photos;
371 /* return all available photos */
372 $result = $this->db->db_query("
373 SELECT DISTINCT photo_id
379 while($row = $this->db->db_fetch_object($result)) {
380 array_push($tagged_photos, $row['photo_id']);
382 return $tagged_photos;
384 } // getPhotoSelection()
386 public function showPhotoIndex()
388 $photos = $this->getPhotoSelection();
390 $count = count($photos);
392 if(!$_SESSION['begin_with'] || $_SESSION['begin_with'] == 0)
395 $begin_with = $_SESSION['begin_with'];
397 if($this->cfg->rows_per_page == 0)
400 $end_with = $begin_with + ($this->cfg->rows_per_page * $this->cfg->thumbs_per_row);
405 $images[$rows] = Array();
406 $img_height[$rows] = Array();
407 $img_width[$rows] = Array();
409 for($i = $begin_with; $i < $end_with; $i++) {
411 $images[$rows][$cols] = $photos[$i];
413 $thumb_path = $this->cfg->base_path ."/thumbs/". $this->cfg->thumb_width ."_". $this->getMD5($photos[$i]);
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];
421 if($cols == $this->cfg->thumbs_per_row-1) {
424 $images[$rows] = Array();
425 $img_width[$rows] = Array();
426 $img_height[$rows] = Array();
433 // +1 for for smarty's selection iteration
436 if(isset($_SESSION['searchfor']) && $_SESSION['searchfor'] != '')
437 $this->tmpl->assign('searchfor', $_SESSION['searchfor']);
439 if($this->cfg->rows_per_page != 0) {
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);
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 .");");
451 $photo_per_page = $this->cfg->rows_per_page * $this->cfg->thumbs_per_row;
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
457 $last_page = ceil($count / $photo_per_page);
459 if($begin_with == 0) {
464 for($i = $begin_with; $i >= 0; $i-=$photo_per_page) {
469 for($i = 1; $i <= $last_page; $i++) {
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%;\"";
482 $page_select.= "<a href=\"javascript:showPhotoIndex(". (($i*$photo_per_page)-$photo_per_page) .");\"";
484 $page_select.= $style;
485 $page_select.= ">". $i ."</a> ";
489 $this->tmpl->assign('page_selector', $page_select);
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);
499 $extern_link = "http://". $_SERVER['SERVER_NAME'] ."/index.php?mode=showpi&tags=". $current_tags;
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");
512 } // showPhotoIndex()
514 public function showBubbleDetails($photo, $direction)
516 if($direction == "up")
517 $direction = "bubbleimg_up";
519 $direction = "bubbleimg_down";
521 $details = $this->get_photo_details($photo);
522 $orig_path = $this->translate_path($details['directory_path']) ."/". $details['name'];
524 $image_url = "phpfspot_img.php?idx=". $photo ."&width=". $this->cfg->bubble_width;
526 $filesize = filesize($orig_path);
527 $filesize = rand($filesize/1024, 2);
529 if(!file_exists($orig_path)) {
530 $this->_warning("Photo ". $orig_path ." does not exist!<br />\n");
534 if(!is_readable($orig_path)) {
535 $this->_warning("Photo ". $orig_path ." is not readable for user ". get_current_user() ."<br />\n");
539 $img = getimagesize($orig_path);
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");
549 } // showBubbleDetails()
551 public function showCredits()
553 $this->tmpl->assign('version', $this->cfg->version);
554 $this->tmpl->assign('product', $this->cfg->product);
555 $this->tmpl->show("credits.tpl");
559 public function create_thumbnail($orig_image, $thumb_image, $width)
561 if(!file_exists($orig_image))
564 $details = getimagesize($orig_image);
566 /* check if original photo is a support image type */
567 if(!$this->checkifImageSupported($details['mime']))
570 $meta = $this->get_meta_informations($orig_image);
575 switch($meta['Orientation']) {
578 $rotate = 0; $flip = false; break;
580 $rotate = 0; $flip = true; break;
582 $rotate = 180; $flip = false; break;
584 $rotate = 180; $flip = true; break;
586 $rotate = 90; $flip = true; break;
588 $rotate = 90; $flip = false; break;
590 $rotate = 270; $flip = true; break;
592 $rotate = 270; $flip = false; break;
595 $src_img = @imagecreatefromjpeg($orig_image);
598 print "Can't load image from ". $orig_image ."\n";
602 /* grabs the height and width */
603 $new_w = imagesx($src_img);
604 $new_h = imagesy($src_img);
606 // If requested width is more then the actual image width,
607 // do not generate a thumbnail
609 if($width >= $new_w) {
610 imagedestroy($src_img);
614 /* calculates aspect ratio */
615 $aspect_ratio = $new_h / $new_w;
619 $new_h = abs($new_w * $aspect_ratio);
621 /* creates new image of that size */
622 $dst_img = imagecreatetruecolor($new_w, $new_h);
624 imagefill($dst_img, 0, 0, ImageColorAllocate($dst_img, 255, 255, 255));
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));
629 /* needs the image to be flipped horizontal? */
633 for($x = 0; $x < $new_w; $x++) {
634 imagecopy($dst_img, $image, $x, 0, $w - $x - 1, 0, 1, $h);
640 $dst_img = $this->rotateImage($dst_img, $rotate);
643 /* write down new generated file */
644 $result = imagejpeg($dst_img, $thumb_image, 75);
647 imagedestroy($dst_img);
648 imagedestroy($src_img);
650 if($result === false) {
651 print "Can't write thumbnail ". $thumb_image ."\n";
657 } // create_thumbnail()
659 public function get_meta_informations($file)
661 return exif_read_data($file);
663 } // get_meta_informations()
665 public function check_config_table()
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,
677 } // check_config_table
680 * Generates a thumbnail from photo idx
682 * This function will generate JPEG thumbnails from provided F-Spot photo
685 * 1. Check if all thumbnail generations (width) are already in place and
687 * 2. Check if the md5sum of the original file has changed
688 * 3. Generate the thumbnails if needed
690 public function gen_thumb($idx = 0, $force = 0)
694 $resolutions = Array(
695 $this->cfg->thumb_width,
696 $this->cfg->bubble_width,
697 $this->cfg->photo_width,
700 /* get details from F-Spot's database */
701 $details = $this->get_photo_details($idx);
703 /* calculate file MD5 sum */
704 $full_path = $this->translate_path($details['directory_path']) ."/". $details['name'];
706 if(!file_exists($full_path)) {
707 $this->_warning("File ". $full_path ." does not exist\n");
711 if(!is_readable($full_path)) {
712 $this->_warning("File ". $full_path ." is not readable for ". get_current_user() ."\n");
716 $file_md5 = md5_file($full_path);
718 $this->_debug("Image [". $idx ."] ". $details['name'] ." Thumbnails:");
720 foreach($resolutions as $resolution) {
722 $thumb_path = $this->cfg->base_path ."/thumbs/". $resolution ."_". $file_md5;
724 /* if the thumbnail file doesn't exist, create it */
725 if(!file_exists($thumb_path)) {
727 $this->_debug(" ". $resolution ."px");
728 if(!$this->create_thumbnail($full_path, $thumb_path, $resolution))
732 /* if the file hasn't changed there is no need to regen the thumb */
733 elseif($file_md5 != $this->getMD5($idx) || $force) {
735 $this->_debug(" ". $resolution ."px");
736 if(!$this->create_thumbnail($full_path, $thumb_path, $resolution))
742 /* set the new/changed MD5 sum for the current photo */
744 $this->setMD5($idx, $file_md5);
751 public function getMD5($idx)
753 $result = $this->cfg_db->db_query("
756 WHERE img_idx='". $idx ."'
762 $img = $this->cfg_db->db_fetch_object($result);
763 return $img['img_md5'];
767 private function setMD5($idx, $md5)
769 $result = $this->cfg_db->db_exec("
770 REPLACE INTO images (img_idx, img_md5)
771 VALUES ('". $idx ."', '". $md5 ."')
776 public function setTagCondition($mode)
778 $_SESSION['tag_condition'] = $mode;
780 } // setTagCondition()
782 public function startTagSearch($searchfor)
784 $_SESSION['searchfor'] = $searchfor;
785 $_SESSION['selected_tags'] = Array();
787 foreach($this->avail_tags as $tag) {
788 if(preg_match('/'. $searchfor .'/i', $this->tags[$tag]))
789 array_push($_SESSION['selected_tags'], $tag);
792 } // startTagSearch()
794 private function rotateImage($img, $degrees)
796 if(function_exists("imagerotate"))
797 $img = imagerotate($img, $degrees, 0);
800 function imagerotate($src_img, $angle)
802 $src_x = imagesx($src_img);
803 $src_y = imagesy($src_img);
808 elseif ($src_x <= $src_y) {
812 elseif ($src_x >= $src_y) {
817 $rotate=imagecreatetruecolor($dest_x,$dest_y);
818 imagealphablending($rotate, false);
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);
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);
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);
849 default: $rotate = $src_img;
856 $img = imagerotate($img, $degrees);
864 private function get_photo_tags($idx)
866 $result = $this->db->db_query("
869 INNER JOIN photo_tags pt
871 WHERE pt.photo_id='". $idx ."'
876 while($row = $this->db->db_fetch_object($result))
877 $tags[$row['id']] = $row['name'];
881 } // get_photo_tags()
883 public function showTextImage($txt, $color=000000, $space=4, $font=4, $w=300)
885 if (strlen($color) != 6)
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);
898 foreach ($txt as $text) {
899 $x = (($w - ($fw * strlen($text))) / 2);
900 imagestring($im, $font, $x, $y, $text, $color);
904 Header("Content-type: image/png");
909 private function checkRequirements()
911 if(!function_exists("imagecreatefromjpeg")) {
912 print "PHP GD library extension is missing<br />\n";
916 if(!function_exists("sqlite3_open")) {
917 print "PHP SQLite3 library extension is missing<br />\n";
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";
928 ini_restore('track_errors');
935 } // checkRequirements()
937 private function _debug($text)
945 public function checkifImageSupported($mime)
947 if(in_array($mime, Array("image/jpeg")))
952 } // checkifImageSupported()
954 public function _warning($text)
956 print "<img src=\"resources/green_info.png\" alt=\"warning\" />\n";