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);
28 $this->cfg_db = new PHPFSPOT_DB(&$this, $this->cfg->phpfspot_db);
29 $this->check_config_table();
31 $this->tmpl = new PHPFSPOT_TMPL($this);
37 if(!isset($_SESSION['tag_condition']))
38 $_SESSION['tag_condition'] = 'or';
40 if(!isset($_SESSION['searchfor']))
41 $_SESSION['searchfor'] = '';
45 public function __destruct()
50 public function show()
52 $this->tmpl->assign('searchfor', $_SESSION['searchfor']);
53 $this->tmpl->assign('page_title', $this->cfg->page_title);
54 $this->tmpl->assign('current_condition', $_SESSION['tag_condition']);
56 switch($_GET['mode']) {
58 if(isset($_GET['tags'])) {
59 $_SESSION['selected_tags'] = split(',', $_GET['tags']);
64 $this->tmpl->assign('content_page', 'welcome.tpl');
65 $this->tmpl->show("index.tpl");
70 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, 0, 1);
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 getPhotoSelection()
286 $tagged_photos = Array();
288 /* return a search result */
289 if(isset($_SESSION['searchfor']) && $_SESSION['searchfor'] != '') {
290 $result = $this->db->db_query("
291 SELECT DISTINCT photo_id
297 WHERE t.name LIKE '%". $_SESSION['searchfor'] ."%'
300 while($row = $this->db->db_fetch_object($result)) {
301 array_push($tagged_photos, $row['photo_id']);
303 return $tagged_photos;
306 /* return according the selected tags */
307 if(isset($_SESSION['selected_tags']) && !empty($_SESSION['selected_tags'])) {
309 foreach($_SESSION['selected_tags'] as $tag)
310 $selected.= $tag .",";
311 $selected = substr($selected, 0, strlen($selected)-1);
313 if($_SESSION['tag_condition'] == 'or') {
314 $result = $this->db->db_query("
315 SELECT DISTINCT photo_id
319 WHERE pt.tag_id IN (". $selected .")
323 elseif($_SESSION['tag_condition'] == 'and') {
325 /* Join together a table looking like
327 pt1.photo_id pt1.tag_id pt2.photo_id pt2.tag_id ...
329 so the query can quickly return all images matching the
330 selected tags in an AND condition
335 SELECT DISTINCT pt1.photo_id
339 for($i = 0; $i < count($_SESSION['selected_tags']); $i++) {
341 INNER JOIN photo_tags pt". ($i+2) ."
342 ON pt1.photo_id=pt". ($i+2) .".photo_id
345 $query_str.= "WHERE pt1.tag_id=". $_SESSION['selected_tags'][0];
346 for($i = 1; $i < count($_SESSION['selected_tags']); $i++) {
348 AND pt". ($i+1) .".tag_id=". $_SESSION['selected_tags'][$i] ."
351 $result = $this->db->db_query($query_str);
354 while($row = $this->db->db_fetch_object($result)) {
355 array_push($tagged_photos, $row['photo_id']);
357 return $tagged_photos;
360 /* return all available photos */
361 $result = $this->db->db_query("
362 SELECT DISTINCT photo_id
368 while($row = $this->db->db_fetch_object($result)) {
369 array_push($tagged_photos, $row['photo_id']);
371 return $tagged_photos;
373 } // getPhotoSelection()
375 public function showPhotoIndex()
377 $photos = $this->getPhotoSelection();
379 $count = count($photos);
381 if(!$_SESSION['begin_with'] || $_SESSION['begin_with'] == 0)
384 $begin_with = $_SESSION['begin_with'];
386 if($this->cfg->rows_per_page == 0)
389 $end_with = $begin_with + ($this->cfg->rows_per_page * $this->cfg->thumbs_per_row);
394 $images[$rows] = Array();
395 $img_height[$rows] = Array();
396 $img_width[$rows] = Array();
398 for($i = $begin_with; $i < $end_with; $i++) {
400 $images[$rows][$cols] = $photos[$i];
402 $thumb_path = $this->cfg->base_path ."/thumbs/". $this->cfg->thumb_width ."_". $this->getMD5($photos[$i]);
404 if(file_exists($thumb_path)) {
405 $info = getimagesize($thumb_path);
406 $img_width[$rows][$cols] = $info[0];
407 $img_height[$rows][$cols] = $info[1];
410 if($cols == $this->cfg->thumbs_per_row-1) {
413 $images[$rows] = Array();
414 $img_width[$rows] = Array();
415 $img_height[$rows] = Array();
422 // +1 for for smarty's selection iteration
425 if(isset($_SESSION['searchfor']) && $_SESSION['searchfor'] != '')
426 $this->tmpl->assign('searchfor', $_SESSION['searchfor']);
428 if($this->cfg->rows_per_page != 0) {
429 $previous_start = $begin_with - ($this->cfg->rows_per_page * $this->cfg->thumbs_per_row);
430 $next_start = $begin_with + ($this->cfg->rows_per_page * $this->cfg->thumbs_per_row);
433 $this->tmpl->assign("previous_url", "javascript:showPhotoIndex(". $previous_start .");");
434 if($end_with < $count)
435 $this->tmpl->assign("next_url", "javascript:showPhotoIndex(". $next_start .");");
439 foreach($_SESSION['selected_tags'] as $tag)
440 $current_tags.= $tag .",";
441 $current_tags = substr($current_tags, 0, strlen($current_tags)-1);
442 $extern_link = "http://". $_SERVER['SERVER_NAME'] ."/index.php?mode=showpi&tags=". $current_tags;
444 $this->tmpl->assign('extern_link', $extern_link);
445 $this->tmpl->assign('count', $count);
446 $this->tmpl->assign('width', $this->cfg->thumb_width);
447 $this->tmpl->assign('images', $images);
448 $this->tmpl->assign('img_width', $img_width);
449 $this->tmpl->assign('img_height', $img_height);
450 $this->tmpl->assign('rows', $rows);
451 $this->tmpl->assign('columns', $this->cfg->thumbs_per_row);
452 $this->tmpl->show("photo_index.tpl");
455 } // showPhotoIndex()
457 public function showBubbleDetails($photo, $direction)
459 if($direction == "up")
460 $direction = "bubbleimg_up";
462 $direction = "bubbleimg_down";
464 $details = $this->get_photo_details($photo);
465 $orig_path = $this->translate_path($details['directory_path']) ."/". $details['name'];
467 $image_url = "phpfspot_img.php?idx=". $photo ."&width=". $this->cfg->bubble_width;
469 $filesize = filesize($orig_path);
470 $filesize = rand($filesize/1024, 2);
472 if(!file_exists($orig_path)) {
473 print "Photo ". $orig_path ." does not exist!<br />\n";
477 if(!is_readable($orig_path)) {
478 print "Photo ". $orig_path ." is not readable for user ". get_current_user() ."<br />\n";
482 $img = getimagesize($orig_path);
484 $this->tmpl->assign('file_size', $filesize);
485 $this->tmpl->assign('width', $img[0]);
486 $this->tmpl->assign('height', $img[1]);
487 $this->tmpl->assign('file_name', $details['name']);
488 $this->tmpl->assign('image_id', $direction);
489 $this->tmpl->assign('image_url', $image_url);
490 $this->tmpl->show("bubble_details.tpl");
492 } // showBubbleDetails()
494 public function showCredits()
496 $this->tmpl->assign('version', $this->cfg->version);
497 $this->tmpl->assign('product', $this->cfg->product);
498 $this->tmpl->show("credits.tpl");
502 public function create_thumbnail($orig_image, $thumb_image, $width)
504 if(!file_exists($orig_image))
507 $meta = $this->get_meta_informations($orig_image);
512 switch($meta['Orientation']) {
515 $rotate = 0; $flip = false; break;
517 $rotate = 0; $flip = true; break;
519 $rotate = 180; $flip = false; break;
521 $rotate = 180; $flip = true; break;
523 $rotate = 90; $flip = true; break;
525 $rotate = 90; $flip = false; break;
527 $rotate = 270; $flip = true; break;
529 $rotate = 270; $flip = false; break;
532 $src_img = @imagecreatefromjpeg($orig_image);
535 print "Can't load image from ". $orig_image ."\n";
539 /* grabs the height and width */
540 $new_w = imagesx($src_img);
541 $new_h = imagesy($src_img);
543 // If requested width is more then the actual image width,
544 // do not generate a thumbnail
546 if($width >= $new_w) {
547 imagedestroy($src_img);
551 /* calculates aspect ratio */
552 $aspect_ratio = $new_h / $new_w;
556 $new_h = abs($new_w * $aspect_ratio);
558 /* creates new image of that size */
559 $dst_img = imagecreatetruecolor($new_w, $new_h);
561 imagefill($dst_img, 0, 0, ImageColorAllocate($dst_img, 255, 255, 255));
563 /* copies resized portion of original image into new image */
564 imagecopyresampled($dst_img,$src_img,0,0,0,0,$new_w,$new_h,imagesx($src_img),imagesy($src_img));
566 /* needs the image to be flipped horizontal? */
570 for($x = 0; $x < $new_w; $x++) {
571 imagecopy($dst_img, $image, $x, 0, $w - $x - 1, 0, 1, $h);
577 $dst_img = $this->rotateImage($dst_img, $rotate);
580 /* write down new generated file */
581 $result = imagejpeg($dst_img, $thumb_image, 75);
584 imagedestroy($dst_img);
585 imagedestroy($src_img);
587 if($result === false) {
588 print "Can't write thumbnail ". $thumb_image ."\n";
594 } // create_thumbnail()
596 public function get_meta_informations($file)
598 return exif_read_data($file);
600 } // get_meta_informations()
602 public function check_config_table()
604 // if the config table doesn't exist yet, create it
605 if(!$this->cfg_db->db_check_table_exists("images")) {
606 $this->cfg_db->db_exec("
607 CREATE TABLE images (
608 img_idx int primary key,
614 } // check_config_table
617 * Generates a thumbnail from photo idx
619 * This function will generate JPEG thumbnails from provided F-Spot photo
622 * 1. Check if all thumbnail generations (width) are already in place and
624 * 2. Check if the md5sum of the original file has changed
625 * 3. Generate the thumbnails if needed
627 public function gen_thumb($idx = 0, $fromcmd = 0, $force = 0)
629 $resolutions = Array(
630 $this->cfg->thumb_width,
631 $this->cfg->bubble_width,
632 $this->cfg->photo_width,
635 $details = $this->get_photo_details($idx);
636 $full_path = $this->translate_path($details['directory_path']) ."/". $details['name'];
637 $file_md5 = md5_file($full_path);
639 if($fromcmd) print "Image [". $idx ."] ". $details['name'] ." Thumbnails:";
643 foreach($resolutions as $resolution) {
645 $thumb_path = $this->cfg->base_path ."/thumbs/". $resolution ."_". $file_md5;
647 /* if the thumbnail file doesn't exist, create it */
648 if(!file_exists($thumb_path)) {
650 if($fromcmd) print " ". $resolution ."px";
651 if(!$this->create_thumbnail($full_path, $thumb_path, $resolution))
656 /* if the file hasn't changed there is no need to regen the thumb */
657 elseif($file_md5 != $this->getMD5($idx) || $force) {
659 if($fromcmd) print " ". $resolution ."px";
660 if(!$this->create_thumbnail($full_path, $thumb_path, $resolution))
666 /* set the new/changed MD5 sum for the current photo */
668 $this->setMD5($idx, $file_md5);
670 if($fromcmd) print "\n";
674 public function getMD5($idx)
676 $result = $this->cfg_db->db_query("
679 WHERE img_idx='". $idx ."'
685 $img = $this->cfg_db->db_fetch_object($result);
686 return $img['img_md5'];
690 private function setMD5($idx, $md5)
692 $result = $this->cfg_db->db_exec("
693 REPLACE INTO images (img_idx, img_md5)
694 VALUES ('". $idx ."', '". $md5 ."')
699 public function setTagCondition($mode)
701 $_SESSION['tag_condition'] = $mode;
703 } // setTagCondition()
705 public function startTagSearch($searchfor)
707 $_SESSION['searchfor'] = $searchfor;
708 $_SESSION['selected_tags'] = Array();
710 foreach($this->avail_tags as $tag) {
711 if(preg_match('/'. $searchfor .'/i', $this->tags[$tag]))
712 array_push($_SESSION['selected_tags'], $tag);
715 } // startTagSearch()
717 private function rotateImage($img, $degrees)
719 if(function_exists("imagerotate"))
720 $img = imagerotate($img, $degrees, 0);
723 function imagerotate($src_img, $angle)
725 $src_x = imagesx($src_img);
726 $src_y = imagesy($src_img);
731 elseif ($src_x <= $src_y) {
735 elseif ($src_x >= $src_y) {
740 $rotate=imagecreatetruecolor($dest_x,$dest_y);
741 imagealphablending($rotate, false);
746 for ($y = 0; $y < ($src_y); $y++) {
747 for ($x = 0; $x < ($src_x); $x++) {
748 $color = imagecolorat($src_img, $x, $y);
749 imagesetpixel($rotate, $dest_x - $y - 1, $x, $color);
755 for ($y = 0; $y < ($src_y); $y++) {
756 for ($x = 0; $x < ($src_x); $x++) {
757 $color = imagecolorat($src_img, $x, $y);
758 imagesetpixel($rotate, $y, $dest_y - $x - 1, $color);
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 - $x - 1, $dest_y - $y - 1, $color);
772 default: $rotate = $src_img;
779 $img = imagerotate($img, $degrees);
787 private function get_photo_tags($idx)
789 $result = $this->db->db_query("
792 INNER JOIN photo_tags pt
794 WHERE pt.photo_id='". $idx ."'
799 while($row = $this->db->db_fetch_object($result))
800 $tags[$row['id']] = $row['name'];
804 } // get_photo_tags()
806 public function showTextImage($txt, $color=000000, $space=4, $font=4, $w=300)
808 if (strlen($color) != 6)
811 $int = hexdec($color);
812 $h = imagefontheight($font);
813 $fw = imagefontwidth($font);
814 $txt = explode("\n", wordwrap($txt, ($w / $fw), "\n"));
815 $lines = count($txt);
816 $im = imagecreate($w, (($h * $lines) + ($lines * $space)));
817 $bg = imagecolorallocate($im, 255, 255, 255);
818 $color = imagecolorallocate($im, 0xFF & ($int >> 0x10), 0xFF & ($int >> 0x8), 0xFF & $int);
821 foreach ($txt as $text) {
822 $x = (($w - ($fw * strlen($text))) / 2);
823 imagestring($im, $font, $x, $y, $text, $color);
827 Header("Content-type: image/png");
832 private function checkRequirements()
834 if(!function_exists("imagecreatefromjpeg")) {
835 print "PHP GD library extension is missing<br />\n";
839 if(!function_exists("sqlite3_open")) {
840 print "PHP SQLite3 library extension is missing<br />\n";
844 /* Check for HTML_AJAX PEAR package, lent from Horde project */
845 ini_set('track_errors', 1);
846 @include 'HTML/AJAX/Server.php';
847 if(isset($php_errormsg)) {
848 print "PEAR HTML_AJAX package is missing<br />\n";
851 ini_restore('track_errors');
858 } // checkRequirements()