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'] = '';
42 // if begin_with is still set but rows_per_page is now 0, unset it
43 if(isset($_SESSION['begin_with']) && $this->cfg->rows_per_page == 0)
44 unset($_SESSION['begin_with']);
48 public function __destruct()
53 public function show()
55 $this->tmpl->assign('searchfor', $_SESSION['searchfor']);
56 $this->tmpl->assign('page_title', $this->cfg->page_title);
57 $this->tmpl->assign('current_condition', $_SESSION['tag_condition']);
59 switch($_GET['mode']) {
61 if(isset($_GET['tags'])) {
62 $_SESSION['selected_tags'] = split(',', $_GET['tags']);
67 $this->tmpl->assign('content_page', 'welcome.tpl');
68 $this->tmpl->show("index.tpl");
73 private function get_tags()
76 $this->avail_tags = Array();
79 $result = $this->db->db_query("
82 ORDER BY sort_priority ASC
85 while($row = $this->db->db_fetch_object($result)) {
88 $tag_name = $row['name'];
90 /* check if config requests to ignore this tag */
91 if(in_array($row['name'], $this->cfg->hide_tags))
94 $this->tags[$tag_id] = $tag_name;
95 $this->avail_tags[$count] = $tag_id;
103 public function get_photo_details($idx)
105 $result = $this->db->db_query("
108 WHERE id='". $idx ."'
111 return $this->db->db_fetch_object($result);
113 } // get_photo_details
115 public function translate_path($path, $width = 0)
117 return str_replace($this->cfg->path_replace_from, $this->cfg->path_replace_to, $path);
121 public function showPhoto($photo)
123 $all_photos = $this->getPhotoSelection();
124 $count = count($all_photos);
126 for($i = 0; $i < $count; $i++) {
129 $next_img = $all_photos[$i];
133 if($all_photos[$i] == $photo) {
137 $previous_img = $all_photos[$i];
140 if($photo == $all_photos[$i]) {
145 $details = $this->get_photo_details($photo);
146 $orig_path = $this->translate_path($details['directory_path']) ."/". $details['name'];
147 $thumb_path = $this->cfg->base_path ."/thumbs/". $this->cfg->photo_width ."_". $this->getMD5($photo);
149 if(!file_exists($orig_path)) {
150 $this->_warning("Photo ". $orig_path ." does not exist!<br />\n");
153 if(!is_readable($orig_path)) {
154 $this->_warning("Photo ". $orig_path ." is not readable for user ". get_current_user() ."<br />\n");
157 /* If the thumbnail doesn't exist yet, try to create it */
158 if(!file_exists($thumb_path)) {
159 $this->gen_thumb($photo, true);
162 $meta = $this->get_meta_informations($orig_path);
164 $meta_date = isset($meta['FileDateTime']) ? strftime("%a %x %X", $meta['FileDateTime']) : "n/a";
165 $meta_make = isset($meta['Make']) ? $meta['Make'] ." ". $meta['Model'] : "n/a";
166 $meta_res = isset($meta['ExifImageWidth']) ? $meta['ExifImageWidth'] ."x". $meta['ExifImageLength'] : "n/a";
167 $meta_size = isset($meta['FileSize']) ? round($meta['FileSize']/1024, 1) ."kbyte" : "n/a";
169 if(file_exists($thumb_path)) {
171 $info = getimagesize($thumb_path);
173 $this->tmpl->assign('description', $details['description']);
174 $this->tmpl->assign('image_name', $details['name']);
176 $this->tmpl->assign('width', $info[0]);
177 $this->tmpl->assign('height', $info[1]);
178 $this->tmpl->assign('ExifMadeOn', $meta_date);
179 $this->tmpl->assign('ExifMadeWith', $meta_make);
180 $this->tmpl->assign('ExifOrigResolution', $meta_res);
181 $this->tmpl->assign('ExifFileSize', $meta_size);
183 $this->tmpl->assign('image_url', 'phpfspot_img.php?idx='. $photo ."&width=". $this->cfg->photo_width);
184 $this->tmpl->assign('image_url_full', 'phpfspot_img.php?idx='. $photo);
186 $this->tmpl->assign('tags', $this->get_photo_tags($photo));
187 $this->tmpl->assign('current', $current);
190 $this->_warning("Can't open file ". $thumb_path ."\n");
194 $this->tmpl->assign('previous_url', "javascript:showImage(". $previous_img .");");
198 $this->tmpl->assign('next_url', "javascript:showImage(". $next_img .");");
201 $this->tmpl->show("single_photo.tpl");
205 public function getAvailableTags()
207 $result = $this->db->db_query("
208 SELECT tag_id as id, count(tag_id) as quantity
218 while($row = $this->db->db_fetch_object($result)) {
219 $tags[$row['id']] = $row['quantity'];
222 // change these font sizes if you will
223 $max_size = 125; // max font size in %
224 $min_size = 75; // min font size in %
226 // get the largest and smallest array values
227 $max_qty = max(array_values($tags));
228 $min_qty = min(array_values($tags));
230 // find the range of values
231 $spread = $max_qty - $min_qty;
232 if (0 == $spread) { // we don't want to divide by zero
236 // determine the font-size increment
237 // this is the increase per tag quantity (times used)
238 $step = ($max_size - $min_size)/($spread);
240 // loop through our tag array
241 foreach ($tags as $key => $value) {
243 if(isset($_SESSION['selected_tags']) && in_array($key, $_SESSION['selected_tags']))
246 // calculate CSS font-size
247 // find the $value in excess of $min_qty
248 // multiply by the font-size increment ($size)
249 // and add the $min_size set above
250 $size = $min_size + (($value - $min_qty) * $step);
251 // uncomment if you want sizes in whole %:
254 print "<a href=\"javascript:Tags('add', ". $key .");\" class=\"tag\" style=\"font-size: ". $size ."%;\">". $this->tags[$key] ."</a>, ";
258 } // getAvailableTags()
260 public function getSelectedTags()
262 foreach($this->avail_tags as $tag)
264 // return all selected tags
265 if(isset($_SESSION['selected_tags']) && in_array($tag, $_SESSION['selected_tags'])) {
266 print "<a href=\"javascript:Tags('del', ". $tag .");\" class=\"tag\">". $this->tags[$tag] ."</a> ";
271 } // getSelectedTags()
273 public function addTag($tag)
275 if(!isset($_SESSION['selected_tags']))
276 $_SESSION['selected_tags'] = Array();
278 if(!in_array($tag, $_SESSION['selected_tags']))
279 array_push($_SESSION['selected_tags'], $tag);
283 public function delTag($tag)
285 if(isset($_SESSION['selected_tags'])) {
286 $key = array_search($tag, $_SESSION['selected_tags']);
287 unset($_SESSION['selected_tags'][$key]);
288 sort($_SESSION['selected_tags']);
293 public function resetTags()
295 unset($_SESSION['selected_tags']);
299 public function resetTagSearch()
301 unset($_SESSION['searchfor']);
303 } // resetTagSearch()
305 public function getPhotoSelection()
307 $tagged_photos = Array();
309 /* return a search result */
310 if(isset($_SESSION['searchfor']) && $_SESSION['searchfor'] != '') {
311 $result = $this->db->db_query("
312 SELECT DISTINCT photo_id
318 WHERE t.name LIKE '%". $_SESSION['searchfor'] ."%'
321 while($row = $this->db->db_fetch_object($result)) {
322 array_push($tagged_photos, $row['photo_id']);
324 return $tagged_photos;
327 /* return according the selected tags */
328 if(isset($_SESSION['selected_tags']) && !empty($_SESSION['selected_tags'])) {
330 foreach($_SESSION['selected_tags'] as $tag)
331 $selected.= $tag .",";
332 $selected = substr($selected, 0, strlen($selected)-1);
334 if($_SESSION['tag_condition'] == 'or') {
335 $result = $this->db->db_query("
336 SELECT DISTINCT photo_id
340 WHERE pt.tag_id IN (". $selected .")
344 elseif($_SESSION['tag_condition'] == 'and') {
346 /* Join together a table looking like
348 pt1.photo_id pt1.tag_id pt2.photo_id pt2.tag_id ...
350 so the query can quickly return all images matching the
351 selected tags in an AND condition
356 SELECT DISTINCT pt1.photo_id
360 for($i = 0; $i < count($_SESSION['selected_tags']); $i++) {
362 INNER JOIN photo_tags pt". ($i+2) ."
363 ON pt1.photo_id=pt". ($i+2) .".photo_id
366 $query_str.= "WHERE pt1.tag_id=". $_SESSION['selected_tags'][0];
367 for($i = 1; $i < count($_SESSION['selected_tags']); $i++) {
369 AND pt". ($i+1) .".tag_id=". $_SESSION['selected_tags'][$i] ."
372 $result = $this->db->db_query($query_str);
375 while($row = $this->db->db_fetch_object($result)) {
376 array_push($tagged_photos, $row['photo_id']);
378 return $tagged_photos;
381 /* return all available photos */
382 $result = $this->db->db_query("
383 SELECT DISTINCT photo_id
389 while($row = $this->db->db_fetch_object($result)) {
390 array_push($tagged_photos, $row['photo_id']);
392 return $tagged_photos;
394 } // getPhotoSelection()
396 public function showPhotoIndex()
398 $photos = $this->getPhotoSelection();
400 $count = count($photos);
402 $anchor = $_SESSION['begin_with'];
404 if(!isset($this->cfg->rows_per_page) || $this->cfg->rows_per_page == 0) {
410 elseif($this->cfg->rows_per_page > 0) {
412 if(!$_SESSION['begin_with'] || $_SESSION['begin_with'] == 0)
416 $begin_with = $_SESSION['begin_with'];
418 // verify $begin_with - perhaps the thumbs-per-rows or
419 // rows-per-page variables have changed or the jump back
420 // from a photo wasn't exact - so calculate the real new
422 $multiplicator = $this->cfg->rows_per_page * $this->cfg->thumbs_per_row;
423 for($i = 0; $i <= $count; $i+=$multiplicator) {
424 if($begin_with >= $i && $begin_with < $i+$multiplicator) {
431 $end_with = $begin_with + ($this->cfg->rows_per_page * $this->cfg->thumbs_per_row);
437 $images[$rows] = Array();
438 $img_height[$rows] = Array();
439 $img_width[$rows] = Array();
440 $img_id[$rows] = Array();
442 for($i = $begin_with; $i < $end_with; $i++) {
444 $images[$rows][$cols] = $photos[$i];
445 $img_id[$rows][$cols] = $i;
447 $thumb_path = $this->cfg->base_path ."/thumbs/". $this->cfg->thumb_width ."_". $this->getMD5($photos[$i]);
449 if(file_exists($thumb_path)) {
450 $info = getimagesize($thumb_path);
451 $img_width[$rows][$cols] = $info[0];
452 $img_height[$rows][$cols] = $info[1];
455 if($cols == $this->cfg->thumbs_per_row-1) {
458 $images[$rows] = Array();
459 $img_width[$rows] = Array();
460 $img_height[$rows] = Array();
467 // +1 for for smarty's selection iteration
470 if(isset($_SESSION['searchfor']) && $_SESSION['searchfor'] != '')
471 $this->tmpl->assign('searchfor', $_SESSION['searchfor']);
473 if($this->cfg->rows_per_page != 0) {
475 /* calculate the page switchers */
476 $previous_start = $begin_with - ($this->cfg->rows_per_page * $this->cfg->thumbs_per_row);
477 $next_start = $begin_with + ($this->cfg->rows_per_page * $this->cfg->thumbs_per_row);
480 $this->tmpl->assign("previous_url", "javascript:showPhotoIndex(". $previous_start .");");
481 if($end_with < $count)
482 $this->tmpl->assign("next_url", "javascript:showPhotoIndex(". $next_start .");");
485 $photo_per_page = $this->cfg->rows_per_page * $this->cfg->thumbs_per_row;
487 /* until 6 pages we show the selector from 1-6
488 everything large will be
489 1 ... 2 3 4 (5) 6 7 ... 9
491 $last_page = ceil($count / $photo_per_page);
493 if($begin_with == 0) {
498 for($i = $begin_with; $i >= 0; $i-=$photo_per_page) {
503 for($i = 1; $i <= $last_page; $i++) {
505 if($current_page == $i)
506 $style = "style=\"font-size: 125%;\"";
507 elseif($current_page-1 == $i || $current_page+1 == $i)
508 $style = "style=\"font-size: 105%;\"";
509 elseif(($current_page-5 >= $i) && ($i != 1) ||
510 ($current_page+5 <= $i) && ($i != $last_page))
511 $style = "style=\"font-size: 75%;\"";
515 $page_select.= "<a href=\"javascript:showPhotoIndex(". (($i*$photo_per_page)-$photo_per_page) .");\"";
517 $page_select.= $style;
518 $page_select.= ">". $i ."</a> ";
522 /* only show the page selector if we have more then one page */
524 $this->tmpl->assign('page_selector', $page_select);
528 if($_SESSION['selected_tags'] != "") {
529 foreach($_SESSION['selected_tags'] as $tag)
530 $current_tags.= $tag .",";
531 $current_tags = substr($current_tags, 0, strlen($current_tags)-1);
534 $extern_link = "http://". $_SERVER['SERVER_NAME'] ."/index.php?mode=showpi&tags=". $current_tags;
536 $this->tmpl->assign('extern_link', $extern_link);
537 $this->tmpl->assign('count', $count);
538 $this->tmpl->assign('width', $this->cfg->thumb_width);
539 $this->tmpl->assign('images', $images);
540 $this->tmpl->assign('img_width', $img_width);
541 $this->tmpl->assign('img_height', $img_height);
542 $this->tmpl->assign('img_id', $img_id);
543 $this->tmpl->assign('rows', $rows);
544 $this->tmpl->assign('columns', $this->cfg->thumbs_per_row);
546 $this->tmpl->show("photo_index.tpl");
548 print "<script language=\"JavaScript\">self.location.hash = '#image". $anchor ."';</script>\n";
550 } // showPhotoIndex()
552 public function showBubbleDetails($photo, $direction)
554 if($direction == "up")
555 $direction = "bubbleimg_up";
557 $direction = "bubbleimg_down";
559 $details = $this->get_photo_details($photo);
560 $orig_path = $this->translate_path($details['directory_path']) ."/". $details['name'];
562 $image_url = "phpfspot_img.php?idx=". $photo ."&width=". $this->cfg->bubble_width;
564 $filesize = filesize($orig_path);
565 $filesize = rand($filesize/1024, 2);
567 if(!file_exists($orig_path)) {
568 $this->_warning("Photo ". $orig_path ." does not exist!<br />\n");
572 if(!is_readable($orig_path)) {
573 $this->_warning("Photo ". $orig_path ." is not readable for user ". get_current_user() ."<br />\n");
577 $img = getimagesize($orig_path);
579 $this->tmpl->assign('file_size', $filesize);
580 $this->tmpl->assign('width', $img[0]);
581 $this->tmpl->assign('height', $img[1]);
582 $this->tmpl->assign('file_name', $details['name']);
583 $this->tmpl->assign('image_id', $direction);
584 $this->tmpl->assign('image_url', $image_url);
585 $this->tmpl->show("bubble_details.tpl");
587 } // showBubbleDetails()
589 public function showCredits()
591 $this->tmpl->assign('version', $this->cfg->version);
592 $this->tmpl->assign('product', $this->cfg->product);
593 $this->tmpl->show("credits.tpl");
597 public function create_thumbnail($orig_image, $thumb_image, $width)
599 if(!file_exists($orig_image))
602 $details = getimagesize($orig_image);
604 /* check if original photo is a support image type */
605 if(!$this->checkifImageSupported($details['mime']))
608 $meta = $this->get_meta_informations($orig_image);
613 switch($meta['Orientation']) {
616 $rotate = 0; $flip = false; break;
618 $rotate = 0; $flip = true; break;
620 $rotate = 180; $flip = false; break;
622 $rotate = 180; $flip = true; break;
624 $rotate = 90; $flip = true; break;
626 $rotate = 90; $flip = false; break;
628 $rotate = 270; $flip = true; break;
630 $rotate = 270; $flip = false; break;
633 $src_img = @imagecreatefromjpeg($orig_image);
636 print "Can't load image from ". $orig_image ."\n";
640 /* grabs the height and width */
641 $new_w = imagesx($src_img);
642 $new_h = imagesy($src_img);
644 // If requested width is more then the actual image width,
645 // do not generate a thumbnail
647 if($width >= $new_w) {
648 imagedestroy($src_img);
652 /* calculates aspect ratio */
653 $aspect_ratio = $new_h / $new_w;
657 $new_h = abs($new_w * $aspect_ratio);
659 /* creates new image of that size */
660 $dst_img = imagecreatetruecolor($new_w, $new_h);
662 imagefill($dst_img, 0, 0, ImageColorAllocate($dst_img, 255, 255, 255));
664 /* copies resized portion of original image into new image */
665 imagecopyresampled($dst_img,$src_img,0,0,0,0,$new_w,$new_h,imagesx($src_img),imagesy($src_img));
667 /* needs the image to be flipped horizontal? */
671 for($x = 0; $x < $new_w; $x++) {
672 imagecopy($dst_img, $image, $x, 0, $w - $x - 1, 0, 1, $h);
678 $dst_img = $this->rotateImage($dst_img, $rotate);
681 /* write down new generated file */
682 $result = imagejpeg($dst_img, $thumb_image, 75);
685 imagedestroy($dst_img);
686 imagedestroy($src_img);
688 if($result === false) {
689 print "Can't write thumbnail ". $thumb_image ."\n";
695 } // create_thumbnail()
697 public function get_meta_informations($file)
699 return exif_read_data($file);
701 } // get_meta_informations()
703 public function check_config_table()
705 // if the config table doesn't exist yet, create it
706 if(!$this->cfg_db->db_check_table_exists("images")) {
707 $this->cfg_db->db_exec("
708 CREATE TABLE images (
709 img_idx int primary key,
715 } // check_config_table
718 * Generates a thumbnail from photo idx
720 * This function will generate JPEG thumbnails from provided F-Spot photo
723 * 1. Check if all thumbnail generations (width) are already in place and
725 * 2. Check if the md5sum of the original file has changed
726 * 3. Generate the thumbnails if needed
728 public function gen_thumb($idx = 0, $force = 0)
732 $resolutions = Array(
733 $this->cfg->thumb_width,
734 $this->cfg->bubble_width,
735 $this->cfg->photo_width,
738 /* get details from F-Spot's database */
739 $details = $this->get_photo_details($idx);
741 /* calculate file MD5 sum */
742 $full_path = $this->translate_path($details['directory_path']) ."/". $details['name'];
744 if(!file_exists($full_path)) {
745 $this->_warning("File ". $full_path ." does not exist\n");
749 if(!is_readable($full_path)) {
750 $this->_warning("File ". $full_path ." is not readable for ". get_current_user() ."\n");
754 $file_md5 = md5_file($full_path);
756 $this->_debug("Image [". $idx ."] ". $details['name'] ." Thumbnails:");
758 foreach($resolutions as $resolution) {
760 $thumb_path = $this->cfg->base_path ."/thumbs/". $resolution ."_". $file_md5;
762 /* if the thumbnail file doesn't exist, create it */
763 if(!file_exists($thumb_path)) {
765 $this->_debug(" ". $resolution ."px");
766 if(!$this->create_thumbnail($full_path, $thumb_path, $resolution))
770 /* if the file hasn't changed there is no need to regen the thumb */
771 elseif($file_md5 != $this->getMD5($idx) || $force) {
773 $this->_debug(" ". $resolution ."px");
774 if(!$this->create_thumbnail($full_path, $thumb_path, $resolution))
780 /* set the new/changed MD5 sum for the current photo */
782 $this->setMD5($idx, $file_md5);
789 public function getMD5($idx)
791 $result = $this->cfg_db->db_query("
794 WHERE img_idx='". $idx ."'
800 $img = $this->cfg_db->db_fetch_object($result);
801 return $img['img_md5'];
805 private function setMD5($idx, $md5)
807 $result = $this->cfg_db->db_exec("
808 REPLACE INTO images (img_idx, img_md5)
809 VALUES ('". $idx ."', '". $md5 ."')
814 public function setTagCondition($mode)
816 $_SESSION['tag_condition'] = $mode;
818 } // setTagCondition()
820 public function startTagSearch($searchfor)
822 $_SESSION['searchfor'] = $searchfor;
823 $_SESSION['selected_tags'] = Array();
825 foreach($this->avail_tags as $tag) {
826 if(preg_match('/'. $searchfor .'/i', $this->tags[$tag]))
827 array_push($_SESSION['selected_tags'], $tag);
830 } // startTagSearch()
832 private function rotateImage($img, $degrees)
834 if(function_exists("imagerotate"))
835 $img = imagerotate($img, $degrees, 0);
838 function imagerotate($src_img, $angle)
840 $src_x = imagesx($src_img);
841 $src_y = imagesy($src_img);
846 elseif ($src_x <= $src_y) {
850 elseif ($src_x >= $src_y) {
855 $rotate=imagecreatetruecolor($dest_x,$dest_y);
856 imagealphablending($rotate, false);
861 for ($y = 0; $y < ($src_y); $y++) {
862 for ($x = 0; $x < ($src_x); $x++) {
863 $color = imagecolorat($src_img, $x, $y);
864 imagesetpixel($rotate, $dest_x - $y - 1, $x, $color);
870 for ($y = 0; $y < ($src_y); $y++) {
871 for ($x = 0; $x < ($src_x); $x++) {
872 $color = imagecolorat($src_img, $x, $y);
873 imagesetpixel($rotate, $y, $dest_y - $x - 1, $color);
879 for ($y = 0; $y < ($src_y); $y++) {
880 for ($x = 0; $x < ($src_x); $x++) {
881 $color = imagecolorat($src_img, $x, $y);
882 imagesetpixel($rotate, $dest_x - $x - 1, $dest_y - $y - 1, $color);
887 default: $rotate = $src_img;
894 $img = imagerotate($img, $degrees);
902 private function get_photo_tags($idx)
904 $result = $this->db->db_query("
907 INNER JOIN photo_tags pt
909 WHERE pt.photo_id='". $idx ."'
914 while($row = $this->db->db_fetch_object($result))
915 $tags[$row['id']] = $row['name'];
919 } // get_photo_tags()
921 public function showTextImage($txt, $color=000000, $space=4, $font=4, $w=300)
923 if (strlen($color) != 6)
926 $int = hexdec($color);
927 $h = imagefontheight($font);
928 $fw = imagefontwidth($font);
929 $txt = explode("\n", wordwrap($txt, ($w / $fw), "\n"));
930 $lines = count($txt);
931 $im = imagecreate($w, (($h * $lines) + ($lines * $space)));
932 $bg = imagecolorallocate($im, 255, 255, 255);
933 $color = imagecolorallocate($im, 0xFF & ($int >> 0x10), 0xFF & ($int >> 0x8), 0xFF & $int);
936 foreach ($txt as $text) {
937 $x = (($w - ($fw * strlen($text))) / 2);
938 imagestring($im, $font, $x, $y, $text, $color);
942 Header("Content-type: image/png");
947 private function checkRequirements()
949 if(!function_exists("imagecreatefromjpeg")) {
950 print "PHP GD library extension is missing<br />\n";
954 if(!function_exists("sqlite3_open")) {
955 print "PHP SQLite3 library extension is missing<br />\n";
959 /* Check for HTML_AJAX PEAR package, lent from Horde project */
960 ini_set('track_errors', 1);
961 @include_once 'HTML/AJAX/Server.php';
962 if(isset($php_errormsg)) {
963 print "PEAR HTML_AJAX package is missing<br />\n";
966 ini_restore('track_errors');
973 } // checkRequirements()
975 private function _debug($text)
983 public function checkifImageSupported($mime)
985 if(in_array($mime, Array("image/jpeg")))
990 } // checkifImageSupported()
992 public function _warning($text)
994 print "<img src=\"resources/green_info.png\" alt=\"warning\" />\n";