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 print "Photo ". $orig_path ." does not exist!<br />\n";
144 if(!is_readable($orig_path)) {
145 print "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 if(file_exists($thumb_path)) {
157 $info = getimagesize($thumb_path);
159 $this->tmpl->assign('description', $details['description']);
160 $this->tmpl->assign('image_name', $details['name']);
162 $this->tmpl->assign('width', $info[0]);
163 $this->tmpl->assign('height', $info[1]);
164 $this->tmpl->assign('ExifMadeOn', strftime("%a %x %X", $meta['FileDateTime']));
165 $this->tmpl->assign('ExifMadeWith', $meta['Make'] ." ". $meta['Model']);
166 $this->tmpl->assign('ExifOrigResolution', $meta['ExifImageWidth'] ."x". $meta['ExifImageLength']);
167 $this->tmpl->assign('ExifFileSize', round($meta['FileSize']/1024, 1));
169 $this->tmpl->assign('image_url', 'phpfspot_img.php?idx='. $photo ."&width=". $this->cfg->photo_width);
170 $this->tmpl->assign('image_url_full', 'phpfspot_img.php?idx='. $photo);
172 $this->tmpl->assign('tags', $this->get_photo_tags($photo));
175 print "Can't open file ". $thumb_path ."\n";
179 $this->tmpl->assign('previous_url', "javascript:showImage(". $previous_img .");");
183 $this->tmpl->assign('next_url', "javascript:showImage(". $next_img .");");
186 $this->tmpl->show("single_photo.tpl");
190 public function getAvailableTags()
192 $result = $this->db->db_query("
193 SELECT tag_id as id, count(tag_id) as quantity
203 while($row = $this->db->db_fetch_object($result)) {
204 $tags[$row['id']] = $row['quantity'];
207 // change these font sizes if you will
208 $max_size = 125; // max font size in %
209 $min_size = 75; // min font size in %
211 // get the largest and smallest array values
212 $max_qty = max(array_values($tags));
213 $min_qty = min(array_values($tags));
215 // find the range of values
216 $spread = $max_qty - $min_qty;
217 if (0 == $spread) { // we don't want to divide by zero
221 // determine the font-size increment
222 // this is the increase per tag quantity (times used)
223 $step = ($max_size - $min_size)/($spread);
225 // loop through our tag array
226 foreach ($tags as $key => $value) {
228 if(isset($_SESSION['selected_tags']) && in_array($key, $_SESSION['selected_tags']))
231 // calculate CSS font-size
232 // find the $value in excess of $min_qty
233 // multiply by the font-size increment ($size)
234 // and add the $min_size set above
235 $size = $min_size + (($value - $min_qty) * $step);
236 // uncomment if you want sizes in whole %:
239 print "<a href=\"javascript:Tags('add', ". $key .");\" class=\"tag\" style=\"font-size: ". $size ."%;\">". $this->tags[$key] ."</a>, ";
243 } // getAvailableTags()
245 public function getSelectedTags()
247 foreach($this->avail_tags as $tag)
249 // return all selected tags
250 if(isset($_SESSION['selected_tags']) && in_array($tag, $_SESSION['selected_tags'])) {
251 print "<a href=\"javascript:Tags('del', ". $tag .");\" class=\"tag\">". $this->tags[$tag] ."</a> ";
256 } // getSelectedTags()
258 public function addTag($tag)
260 if(!isset($_SESSION['selected_tags']))
261 $_SESSION['selected_tags'] = Array();
263 if(!in_array($tag, $_SESSION['selected_tags']))
264 array_push($_SESSION['selected_tags'], $tag);
268 public function delTag($tag)
270 if(isset($_SESSION['selected_tags'])) {
271 $key = array_search($tag, $_SESSION['selected_tags']);
272 unset($_SESSION['selected_tags'][$key]);
273 sort($_SESSION['selected_tags']);
278 public function resetTags()
280 unset($_SESSION['selected_tags']);
284 public function resetTagSearch()
286 unset($_SESSION['searchfor']);
288 } // resetTagSearch()
290 public function getPhotoSelection()
292 $tagged_photos = Array();
294 /* return a search result */
295 if(isset($_SESSION['searchfor']) && $_SESSION['searchfor'] != '') {
296 $result = $this->db->db_query("
297 SELECT DISTINCT photo_id
303 WHERE t.name LIKE '%". $_SESSION['searchfor'] ."%'
306 while($row = $this->db->db_fetch_object($result)) {
307 array_push($tagged_photos, $row['photo_id']);
309 return $tagged_photos;
312 /* return according the selected tags */
313 if(isset($_SESSION['selected_tags']) && !empty($_SESSION['selected_tags'])) {
315 foreach($_SESSION['selected_tags'] as $tag)
316 $selected.= $tag .",";
317 $selected = substr($selected, 0, strlen($selected)-1);
319 if($_SESSION['tag_condition'] == 'or') {
320 $result = $this->db->db_query("
321 SELECT DISTINCT photo_id
325 WHERE pt.tag_id IN (". $selected .")
329 elseif($_SESSION['tag_condition'] == 'and') {
331 /* Join together a table looking like
333 pt1.photo_id pt1.tag_id pt2.photo_id pt2.tag_id ...
335 so the query can quickly return all images matching the
336 selected tags in an AND condition
341 SELECT DISTINCT pt1.photo_id
345 for($i = 0; $i < count($_SESSION['selected_tags']); $i++) {
347 INNER JOIN photo_tags pt". ($i+2) ."
348 ON pt1.photo_id=pt". ($i+2) .".photo_id
351 $query_str.= "WHERE pt1.tag_id=". $_SESSION['selected_tags'][0];
352 for($i = 1; $i < count($_SESSION['selected_tags']); $i++) {
354 AND pt". ($i+1) .".tag_id=". $_SESSION['selected_tags'][$i] ."
357 $result = $this->db->db_query($query_str);
360 while($row = $this->db->db_fetch_object($result)) {
361 array_push($tagged_photos, $row['photo_id']);
363 return $tagged_photos;
366 /* return all available photos */
367 $result = $this->db->db_query("
368 SELECT DISTINCT photo_id
374 while($row = $this->db->db_fetch_object($result)) {
375 array_push($tagged_photos, $row['photo_id']);
377 return $tagged_photos;
379 } // getPhotoSelection()
381 public function showPhotoIndex()
383 $photos = $this->getPhotoSelection();
385 $count = count($photos);
387 if(!$_SESSION['begin_with'] || $_SESSION['begin_with'] == 0)
390 $begin_with = $_SESSION['begin_with'];
392 if($this->cfg->rows_per_page == 0)
395 $end_with = $begin_with + ($this->cfg->rows_per_page * $this->cfg->thumbs_per_row);
400 $images[$rows] = Array();
401 $img_height[$rows] = Array();
402 $img_width[$rows] = Array();
404 for($i = $begin_with; $i < $end_with; $i++) {
406 $images[$rows][$cols] = $photos[$i];
408 $thumb_path = $this->cfg->base_path ."/thumbs/". $this->cfg->thumb_width ."_". $this->getMD5($photos[$i]);
410 if(file_exists($thumb_path)) {
411 $info = getimagesize($thumb_path);
412 $img_width[$rows][$cols] = $info[0];
413 $img_height[$rows][$cols] = $info[1];
416 if($cols == $this->cfg->thumbs_per_row-1) {
419 $images[$rows] = Array();
420 $img_width[$rows] = Array();
421 $img_height[$rows] = Array();
428 // +1 for for smarty's selection iteration
431 if(isset($_SESSION['searchfor']) && $_SESSION['searchfor'] != '')
432 $this->tmpl->assign('searchfor', $_SESSION['searchfor']);
434 if($this->cfg->rows_per_page != 0) {
435 $previous_start = $begin_with - ($this->cfg->rows_per_page * $this->cfg->thumbs_per_row);
436 $next_start = $begin_with + ($this->cfg->rows_per_page * $this->cfg->thumbs_per_row);
439 $this->tmpl->assign("previous_url", "javascript:showPhotoIndex(". $previous_start .");");
440 if($end_with < $count)
441 $this->tmpl->assign("next_url", "javascript:showPhotoIndex(". $next_start .");");
445 if($_SESSION['selected_tags'] != "") {
446 foreach($_SESSION['selected_tags'] as $tag)
447 $current_tags.= $tag .",";
448 $current_tags = substr($current_tags, 0, strlen($current_tags)-1);
451 $extern_link = "http://". $_SERVER['SERVER_NAME'] ."/index.php?mode=showpi&tags=". $current_tags;
453 $this->tmpl->assign('extern_link', $extern_link);
454 $this->tmpl->assign('count', $count);
455 $this->tmpl->assign('width', $this->cfg->thumb_width);
456 $this->tmpl->assign('images', $images);
457 $this->tmpl->assign('img_width', $img_width);
458 $this->tmpl->assign('img_height', $img_height);
459 $this->tmpl->assign('rows', $rows);
460 $this->tmpl->assign('columns', $this->cfg->thumbs_per_row);
461 $this->tmpl->show("photo_index.tpl");
464 } // showPhotoIndex()
466 public function showBubbleDetails($photo, $direction)
468 if($direction == "up")
469 $direction = "bubbleimg_up";
471 $direction = "bubbleimg_down";
473 $details = $this->get_photo_details($photo);
474 $orig_path = $this->translate_path($details['directory_path']) ."/". $details['name'];
476 $image_url = "phpfspot_img.php?idx=". $photo ."&width=". $this->cfg->bubble_width;
478 $filesize = filesize($orig_path);
479 $filesize = rand($filesize/1024, 2);
481 if(!file_exists($orig_path)) {
482 print "Photo ". $orig_path ." does not exist!<br />\n";
486 if(!is_readable($orig_path)) {
487 print "Photo ". $orig_path ." is not readable for user ". get_current_user() ."<br />\n";
491 $img = getimagesize($orig_path);
493 $this->tmpl->assign('file_size', $filesize);
494 $this->tmpl->assign('width', $img[0]);
495 $this->tmpl->assign('height', $img[1]);
496 $this->tmpl->assign('file_name', $details['name']);
497 $this->tmpl->assign('image_id', $direction);
498 $this->tmpl->assign('image_url', $image_url);
499 $this->tmpl->show("bubble_details.tpl");
501 } // showBubbleDetails()
503 public function showCredits()
505 $this->tmpl->assign('version', $this->cfg->version);
506 $this->tmpl->assign('product', $this->cfg->product);
507 $this->tmpl->show("credits.tpl");
511 public function create_thumbnail($orig_image, $thumb_image, $width)
513 if(!file_exists($orig_image))
516 $details = getimagesize($orig_image);
518 /* check if original photo is a support image type */
519 if(!$this->parent->checkifImageSupported($details['mime']))
522 $meta = $this->get_meta_informations($orig_image);
527 switch($meta['Orientation']) {
530 $rotate = 0; $flip = false; break;
532 $rotate = 0; $flip = true; break;
534 $rotate = 180; $flip = false; break;
536 $rotate = 180; $flip = true; break;
538 $rotate = 90; $flip = true; break;
540 $rotate = 90; $flip = false; break;
542 $rotate = 270; $flip = true; break;
544 $rotate = 270; $flip = false; break;
547 $src_img = @imagecreatefromjpeg($orig_image);
550 print "Can't load image from ". $orig_image ."\n";
554 /* grabs the height and width */
555 $new_w = imagesx($src_img);
556 $new_h = imagesy($src_img);
558 // If requested width is more then the actual image width,
559 // do not generate a thumbnail
561 if($width >= $new_w) {
562 imagedestroy($src_img);
566 /* calculates aspect ratio */
567 $aspect_ratio = $new_h / $new_w;
571 $new_h = abs($new_w * $aspect_ratio);
573 /* creates new image of that size */
574 $dst_img = imagecreatetruecolor($new_w, $new_h);
576 imagefill($dst_img, 0, 0, ImageColorAllocate($dst_img, 255, 255, 255));
578 /* copies resized portion of original image into new image */
579 imagecopyresampled($dst_img,$src_img,0,0,0,0,$new_w,$new_h,imagesx($src_img),imagesy($src_img));
581 /* needs the image to be flipped horizontal? */
585 for($x = 0; $x < $new_w; $x++) {
586 imagecopy($dst_img, $image, $x, 0, $w - $x - 1, 0, 1, $h);
592 $dst_img = $this->rotateImage($dst_img, $rotate);
595 /* write down new generated file */
596 $result = imagejpeg($dst_img, $thumb_image, 75);
599 imagedestroy($dst_img);
600 imagedestroy($src_img);
602 if($result === false) {
603 print "Can't write thumbnail ". $thumb_image ."\n";
609 } // create_thumbnail()
611 public function get_meta_informations($file)
613 return exif_read_data($file);
615 } // get_meta_informations()
617 public function check_config_table()
619 // if the config table doesn't exist yet, create it
620 if(!$this->cfg_db->db_check_table_exists("images")) {
621 $this->cfg_db->db_exec("
622 CREATE TABLE images (
623 img_idx int primary key,
629 } // check_config_table
632 * Generates a thumbnail from photo idx
634 * This function will generate JPEG thumbnails from provided F-Spot photo
637 * 1. Check if all thumbnail generations (width) are already in place and
639 * 2. Check if the md5sum of the original file has changed
640 * 3. Generate the thumbnails if needed
642 public function gen_thumb($idx = 0, $force = 0)
646 $resolutions = Array(
647 $this->cfg->thumb_width,
648 $this->cfg->bubble_width,
649 $this->cfg->photo_width,
652 /* get details from F-Spot's database */
653 $details = $this->get_photo_details($idx);
655 /* calculate file MD5 sum */
656 $full_path = $this->translate_path($details['directory_path']) ."/". $details['name'];
657 $file_md5 = md5_file($full_path);
659 $this->_debug("Image [". $idx ."] ". $details['name'] ." Thumbnails:");
661 foreach($resolutions as $resolution) {
663 $thumb_path = $this->cfg->base_path ."/thumbs/". $resolution ."_". $file_md5;
665 /* if the thumbnail file doesn't exist, create it */
666 if(!file_exists($thumb_path)) {
668 $this->_debug(" ". $resolution ."px");
669 if(!$this->create_thumbnail($full_path, $thumb_path, $resolution))
673 /* if the file hasn't changed there is no need to regen the thumb */
674 elseif($file_md5 != $this->getMD5($idx) || $force) {
676 $this->_debug(" ". $resolution ."px");
677 if(!$this->create_thumbnail($full_path, $thumb_path, $resolution))
683 /* set the new/changed MD5 sum for the current photo */
685 $this->setMD5($idx, $file_md5);
692 public function getMD5($idx)
694 $result = $this->cfg_db->db_query("
697 WHERE img_idx='". $idx ."'
703 $img = $this->cfg_db->db_fetch_object($result);
704 return $img['img_md5'];
708 private function setMD5($idx, $md5)
710 $result = $this->cfg_db->db_exec("
711 REPLACE INTO images (img_idx, img_md5)
712 VALUES ('". $idx ."', '". $md5 ."')
717 public function setTagCondition($mode)
719 $_SESSION['tag_condition'] = $mode;
721 } // setTagCondition()
723 public function startTagSearch($searchfor)
725 $_SESSION['searchfor'] = $searchfor;
726 $_SESSION['selected_tags'] = Array();
728 foreach($this->avail_tags as $tag) {
729 if(preg_match('/'. $searchfor .'/i', $this->tags[$tag]))
730 array_push($_SESSION['selected_tags'], $tag);
733 } // startTagSearch()
735 private function rotateImage($img, $degrees)
737 if(function_exists("imagerotate"))
738 $img = imagerotate($img, $degrees, 0);
741 function imagerotate($src_img, $angle)
743 $src_x = imagesx($src_img);
744 $src_y = imagesy($src_img);
749 elseif ($src_x <= $src_y) {
753 elseif ($src_x >= $src_y) {
758 $rotate=imagecreatetruecolor($dest_x,$dest_y);
759 imagealphablending($rotate, false);
764 for ($y = 0; $y < ($src_y); $y++) {
765 for ($x = 0; $x < ($src_x); $x++) {
766 $color = imagecolorat($src_img, $x, $y);
767 imagesetpixel($rotate, $dest_x - $y - 1, $x, $color);
773 for ($y = 0; $y < ($src_y); $y++) {
774 for ($x = 0; $x < ($src_x); $x++) {
775 $color = imagecolorat($src_img, $x, $y);
776 imagesetpixel($rotate, $y, $dest_y - $x - 1, $color);
782 for ($y = 0; $y < ($src_y); $y++) {
783 for ($x = 0; $x < ($src_x); $x++) {
784 $color = imagecolorat($src_img, $x, $y);
785 imagesetpixel($rotate, $dest_x - $x - 1, $dest_y - $y - 1, $color);
790 default: $rotate = $src_img;
797 $img = imagerotate($img, $degrees);
805 private function get_photo_tags($idx)
807 $result = $this->db->db_query("
810 INNER JOIN photo_tags pt
812 WHERE pt.photo_id='". $idx ."'
817 while($row = $this->db->db_fetch_object($result))
818 $tags[$row['id']] = $row['name'];
822 } // get_photo_tags()
824 public function showTextImage($txt, $color=000000, $space=4, $font=4, $w=300)
826 if (strlen($color) != 6)
829 $int = hexdec($color);
830 $h = imagefontheight($font);
831 $fw = imagefontwidth($font);
832 $txt = explode("\n", wordwrap($txt, ($w / $fw), "\n"));
833 $lines = count($txt);
834 $im = imagecreate($w, (($h * $lines) + ($lines * $space)));
835 $bg = imagecolorallocate($im, 255, 255, 255);
836 $color = imagecolorallocate($im, 0xFF & ($int >> 0x10), 0xFF & ($int >> 0x8), 0xFF & $int);
839 foreach ($txt as $text) {
840 $x = (($w - ($fw * strlen($text))) / 2);
841 imagestring($im, $font, $x, $y, $text, $color);
845 Header("Content-type: image/png");
850 private function checkRequirements()
852 if(!function_exists("imagecreatefromjpeg")) {
853 print "PHP GD library extension is missing<br />\n";
857 if(!function_exists("sqlite3_open")) {
858 print "PHP SQLite3 library extension is missing<br />\n";
862 /* Check for HTML_AJAX PEAR package, lent from Horde project */
863 ini_set('track_errors', 1);
864 @include_once 'HTML/AJAX/Server.php';
865 if(isset($php_errormsg)) {
866 print "PEAR HTML_AJAX package is missing<br />\n";
869 ini_restore('track_errors');
876 } // checkRequirements()
878 private function _debug($text)
886 public function checkifImageSupported($mime)
888 if(in_array($mime, Array("image/jpeg")))
893 } // checkifImageSupported()