3 require_once "phpfspot_cfg.php";
4 require_once "phpfspot_db.php";
5 require_once "phpfspot_tmpl.php";
17 public function __construct()
19 $this->cfg = new PHPFSPOT_CFG;
21 $this->db = new PHPFSPOT_DB(&$this, $this->cfg->fspot_db);
23 $this->cfg_db = new PHPFSPOT_DB(&$this, $this->cfg->phpfspot_db);
24 $this->check_config_table();
26 $this->tmpl = new PHPFSPOT_TMPL($this);
32 if(!isset($_SESSION['tag_condition']))
33 $_SESSION['tag_condition'] = 'or';
35 if(!isset($_SESSION['searchfor']))
36 $_SESSION['searchfor'] = '';
40 public function __destruct()
45 public function show()
47 $this->tmpl->assign('searchfor', $_SESSION['searchfor']);
48 $this->tmpl->assign('page_title', $this->cfg->page_title);
49 $this->tmpl->assign('current_condition', $_SESSION['tag_condition']);
51 switch($_GET['mode']) {
53 if(isset($_GET['tags'])) {
54 $_SESSION['selected_tags'] = split(',', $_GET['tags']);
59 $this->tmpl->assign('content_page', 'welcome.tpl');
60 $this->tmpl->show("index.tpl");
65 private function get_tags()
67 $this->avail_tags = Array();
70 $result = $this->db->db_query("
73 ORDER BY sort_priority ASC
76 while($row = $this->db->db_fetch_object($result)) {
79 $tag_name = $row['name'];
81 /* check if config requests to ignore this tag */
82 if(in_array($row['name'], $this->cfg->hide_tags))
85 $this->tags[$tag_id] = $tag_name;
86 $this->avail_tags[$count] = $tag_id;
94 public function get_photo_details($idx)
96 $result = $this->db->db_query("
102 return $this->db->db_fetch_object($result);
104 } // get_photo_details
106 public function translate_path($path, $width = 0)
108 return str_replace($this->cfg->path_replace_from, $this->cfg->path_replace_to, $path);
112 public function showPhoto($photo)
114 $all_photos = $this->getPhotoSelection();
116 foreach($all_photos as $all_photo) {
119 $next_img = $all_photo;
123 if($all_photo == $photo) {
127 $previous_img = $all_photo;
131 $details = $this->get_photo_details($photo);
132 $orig_path = $this->translate_path($details['directory_path']) ."/". $details['name'];
133 $thumb_path = $this->cfg->base_path ."/thumbs/". $this->cfg->photo_width ."_". $this->getMD5($photo);
135 if(!file_exists($orig_path)) {
136 print "Photo ". $orig_path ." does not exist!<br />\n";
139 if(!is_readable($orig_path)) {
140 print "Photo ". $orig_path ." is not readable for user ". get_current_user() ."<br />\n";
143 /* If the thumbnail doesn't exist yet, try to create it */
144 if(!file_exists($thumb_path)) {
145 $this->gen_thumb($photo, 0, 1);
148 $meta = $this->get_meta_informations($orig_path);
150 if(file_exists($thumb_path)) {
152 $info = getimagesize($thumb_path);
154 $this->tmpl->assign('description', $details['description']);
155 $this->tmpl->assign('image_name', $details['name']);
157 $this->tmpl->assign('width', $info[0]);
158 $this->tmpl->assign('height', $info[1]);
159 $this->tmpl->assign('ExifMadeOn', strftime("%a %x %X", $meta['FileDateTime']));
160 $this->tmpl->assign('ExifMadeWith', $meta['Make'] ." ". $meta['Model']);
161 $this->tmpl->assign('ExifOrigResolution', $meta['ExifImageWidth'] ."x". $meta['ExifImageLength']);
162 $this->tmpl->assign('ExifFileSize', round($meta['FileSize']/1024, 1));
164 $this->tmpl->assign('image_url', 'phpfspot_img.php?idx='. $photo ."&width=". $this->cfg->photo_width);
165 $this->tmpl->assign('image_url_full', 'phpfspot_img.php?idx='. $photo);
167 $this->tmpl->assign('tags', $this->get_photo_tags($photo));
170 print "Can't open file ". $thumb_path ."\n";
174 $this->tmpl->assign('previous_url', "javascript:showImage(". $previous_img .");");
178 $this->tmpl->assign('next_url', "javascript:showImage(". $next_img .");");
181 $this->tmpl->show("single_photo.tpl");
185 public function getAvailableTags()
187 $result = $this->db->db_query("
188 SELECT tag_id as id, count(tag_id) as quantity
198 while($row = $this->db->db_fetch_object($result)) {
199 $tags[$row['id']] = $row['quantity'];
202 // change these font sizes if you will
203 $max_size = 125; // max font size in %
204 $min_size = 75; // min font size in %
206 // get the largest and smallest array values
207 $max_qty = max(array_values($tags));
208 $min_qty = min(array_values($tags));
210 // find the range of values
211 $spread = $max_qty - $min_qty;
212 if (0 == $spread) { // we don't want to divide by zero
216 // determine the font-size increment
217 // this is the increase per tag quantity (times used)
218 $step = ($max_size - $min_size)/($spread);
220 // loop through our tag array
221 foreach ($tags as $key => $value) {
223 if(isset($_SESSION['selected_tags']) && in_array($key, $_SESSION['selected_tags']))
226 // calculate CSS font-size
227 // find the $value in excess of $min_qty
228 // multiply by the font-size increment ($size)
229 // and add the $min_size set above
230 $size = $min_size + (($value - $min_qty) * $step);
231 // uncomment if you want sizes in whole %:
234 print "<a href=\"javascript:Tags('add', ". $key .");\" class=\"tag\" style=\"font-size: ". $size ."%;\">". $this->tags[$key] ."</a>, ";
238 } // getAvailableTags()
240 public function getSelectedTags()
242 foreach($this->avail_tags as $tag)
244 // return all selected tags
245 if(isset($_SESSION['selected_tags']) && in_array($tag, $_SESSION['selected_tags'])) {
246 print "<a href=\"javascript:Tags('del', ". $tag .");\" class=\"tag\">". $this->tags[$tag] ."</a> ";
251 } // getSelectedTags()
253 public function addTag($tag)
255 if(!isset($_SESSION['selected_tags']))
256 $_SESSION['selected_tags'] = Array();
258 if(!in_array($tag, $_SESSION['selected_tags']))
259 array_push($_SESSION['selected_tags'], $tag);
263 public function delTag($tag)
265 if(isset($_SESSION['selected_tags'])) {
266 $key = array_search($tag, $_SESSION['selected_tags']);
267 unset($_SESSION['selected_tags'][$key]);
268 sort($_SESSION['selected_tags']);
273 public function resetTags()
275 unset($_SESSION['selected_tags']);
279 public function getPhotoSelection()
281 $tagged_photos = Array();
283 /* return a search result */
284 if(isset($_SESSION['searchfor']) && $_SESSION['searchfor'] != '') {
285 $result = $this->db->db_query("
286 SELECT DISTINCT photo_id
292 WHERE t.name LIKE '%". $_SESSION['searchfor'] ."%'
295 while($row = $this->db->db_fetch_object($result)) {
296 array_push($tagged_photos, $row['photo_id']);
298 return $tagged_photos;
301 /* return according the selected tags */
302 if(isset($_SESSION['selected_tags']) && !empty($_SESSION['selected_tags'])) {
304 foreach($_SESSION['selected_tags'] as $tag)
305 $selected.= $tag .",";
306 $selected = substr($selected, 0, strlen($selected)-1);
308 if($_SESSION['tag_condition'] == 'or') {
309 $result = $this->db->db_query("
310 SELECT DISTINCT photo_id
314 WHERE pt.tag_id IN (". $selected .")
318 elseif($_SESSION['tag_condition'] == 'and') {
320 /* Join together a table looking like
322 pt1.photo_id pt1.tag_id pt2.photo_id pt2.tag_id ...
324 so the query can quickly return all images matching the
325 selected tags in an AND condition
330 SELECT DISTINCT pt1.photo_id
334 for($i = 0; $i < count($_SESSION['selected_tags']); $i++) {
336 INNER JOIN photo_tags pt". ($i+2) ."
337 ON pt1.photo_id=pt". ($i+2) .".photo_id
340 $query_str.= "WHERE pt1.tag_id=". $_SESSION['selected_tags'][0];
341 for($i = 1; $i < count($_SESSION['selected_tags']); $i++) {
343 AND pt". ($i+1) .".tag_id=". $_SESSION['selected_tags'][$i] ."
346 $result = $this->db->db_query($query_str);
349 while($row = $this->db->db_fetch_object($result)) {
350 array_push($tagged_photos, $row['photo_id']);
352 return $tagged_photos;
355 /* return all available photos */
356 $result = $this->db->db_query("
357 SELECT DISTINCT photo_id
363 while($row = $this->db->db_fetch_object($result)) {
364 array_push($tagged_photos, $row['photo_id']);
366 return $tagged_photos;
368 } // getPhotoSelection()
370 public function showPhotoIndex()
372 $photos = $this->getPhotoSelection();
374 $count = count($photos);
376 if(!$_SESSION['begin_with'] || $_SESSION['begin_with'] == 0)
379 $begin_with = $_SESSION['begin_with'];
381 if($this->cfg->rows_per_page == 0)
384 $end_with = $begin_with + ($this->cfg->rows_per_page * $this->cfg->thumbs_per_row);
389 $images[$rows] = Array();
390 $img_height[$rows] = Array();
391 $img_width[$rows] = Array();
393 for($i = $begin_with; $i < $end_with; $i++) {
395 $images[$rows][$cols] = $photos[$i];
397 $thumb_path = $this->cfg->base_path ."/thumbs/". $this->cfg->thumb_width ."_". $this->getMD5($photos[$i]);
399 if(file_exists($thumb_path)) {
400 $info = getimagesize($thumb_path);
401 $img_width[$rows][$cols] = $info[0];
402 $img_height[$rows][$cols] = $info[1];
405 if($cols == $this->cfg->thumbs_per_row-1) {
408 $images[$rows] = Array();
409 $img_width[$rows] = Array();
410 $img_height[$rows] = Array();
417 // +1 for for smarty's selection iteration
420 if(isset($_SESSION['searchfor']) && $_SESSION['searchfor'] != '')
421 $this->tmpl->assign('searchfor', $_SESSION['searchfor']);
423 if($this->cfg->rows_per_page != 0) {
424 $previous_start = $begin_with - ($this->cfg->rows_per_page * $this->cfg->thumbs_per_row);
425 $next_start = $begin_with + ($this->cfg->rows_per_page * $this->cfg->thumbs_per_row);
428 $this->tmpl->assign("previous_url", "javascript:showPhotoIndex(". $previous_start .");");
429 if($end_with < $count)
430 $this->tmpl->assign("next_url", "javascript:showPhotoIndex(". $next_start .");");
434 foreach($_SESSION['selected_tags'] as $tag)
435 $current_tags.= $tag .",";
436 $current_tags = substr($current_tags, 0, strlen($current_tags)-1);
437 $extern_link = "http://". $_SERVER['SERVER_NAME'] ."/index.php?mode=showpi&tags=". $current_tags;
439 $this->tmpl->assign('extern_link', $extern_link);
440 $this->tmpl->assign('count', $count);
441 $this->tmpl->assign('width', $this->cfg->thumb_width);
442 $this->tmpl->assign('images', $images);
443 $this->tmpl->assign('img_width', $img_width);
444 $this->tmpl->assign('img_height', $img_height);
445 $this->tmpl->assign('rows', $rows);
446 $this->tmpl->assign('columns', $this->cfg->thumbs_per_row);
447 $this->tmpl->show("photo_index.tpl");
450 } // showPhotoIndex()
452 public function showBubbleDetails($photo, $direction)
454 if($direction == "up")
455 $direction = "bubbleimg_up";
457 $direction = "bubbleimg_down";
459 $details = $this->get_photo_details($photo);
460 $orig_path = $this->translate_path($details['directory_path']) ."/". $details['name'];
462 $image_url = "phpfspot_img.php?idx=". $photo ."&width=". $this->cfg->bubble_width;
464 $filesize = filesize($orig_path);
465 $filesize = rand($filesize/1024, 2);
467 if(!file_exists($orig_path)) {
468 print "Photo ". $orig_path ." does not exist!<br />\n";
472 if(!is_readable($orig_path)) {
473 print "Photo ". $orig_path ." is not readable for user ". get_current_user() ."<br />\n";
477 $img = getimagesize($orig_path);
479 $this->tmpl->assign('file_size', $filesize);
480 $this->tmpl->assign('width', $img[0]);
481 $this->tmpl->assign('height', $img[1]);
482 $this->tmpl->assign('file_name', $details['name']);
483 $this->tmpl->assign('image_id', $direction);
484 $this->tmpl->assign('image_url', $image_url);
485 $this->tmpl->show("bubble_details.tpl");
487 } // showBubbleDetails()
489 public function showCredits()
491 $this->tmpl->assign('version', $this->cfg->version);
492 $this->tmpl->assign('product', $this->cfg->product);
493 $this->tmpl->show("credits.tpl");
497 public function create_thumbnail($orig_image, $thumb_image, $width)
499 if(!file_exists($orig_image))
502 $meta = $this->get_meta_informations($orig_image);
507 switch($meta['Orientation']) {
510 $rotate = 0; $flip = false; break;
512 $rotate = 0; $flip = true; break;
514 $rotate = 180; $flip = false; break;
516 $rotate = 180; $flip = true; break;
518 $rotate = 90; $flip = true; break;
520 $rotate = 90; $flip = false; break;
522 $rotate = 270; $flip = true; break;
524 $rotate = 270; $flip = false; break;
527 $src_img = @imagecreatefromjpeg($orig_image);
530 print "Can't load image from ". $orig_image ."\n";
534 /* grabs the height and width */
535 $new_w = imagesx($src_img);
536 $new_h = imagesy($src_img);
538 // If requested width is more then the actual image width,
539 // do not generate a thumbnail
541 if($width >= $new_w) {
542 imagedestroy($src_img);
546 /* calculates aspect ratio */
547 $aspect_ratio = $new_h / $new_w;
551 $new_h = abs($new_w * $aspect_ratio);
553 /* creates new image of that size */
554 $dst_img = imagecreatetruecolor($new_w, $new_h);
556 imagefill($dst_img, 0, 0, ImageColorAllocate($dst_img, 255, 255, 255));
558 /* copies resized portion of original image into new image */
559 imagecopyresampled($dst_img,$src_img,0,0,0,0,$new_w,$new_h,imagesx($src_img),imagesy($src_img));
561 /* needs the image to be flipped horizontal? */
565 for($x = 0; $x < $new_w; $x++) {
566 imagecopy($dst_img, $image, $x, 0, $w - $x - 1, 0, 1, $h);
572 $dst_img = $this->rotateImage($dst_img, $rotate);
575 /* write down new generated file */
576 $result = imagejpeg($dst_img, $thumb_image, 75);
579 imagedestroy($dst_img);
580 imagedestroy($src_img);
582 if($result === false) {
583 print "Can't write thumbnail ". $thumb_image ."\n";
589 } // create_thumbnail()
591 public function get_meta_informations($file)
593 return exif_read_data($file);
595 } // get_meta_informations()
597 public function check_config_table()
599 // if the config table doesn't exist yet, create it
600 if(!$this->cfg_db->db_check_table_exists("images")) {
601 $this->cfg_db->db_exec("
602 CREATE TABLE images (
603 img_idx int primary key,
609 } // check_config_table
612 * Generates a thumbnail from photo idx
614 * This function will generate JPEG thumbnails from provided F-Spot photo
617 * 1. Check if all thumbnail generations (width) are already in place and
619 * 2. Check if the md5sum of the original file has changed
620 * 3. Generate the thumbnails if needed
622 public function gen_thumb($idx = 0, $fromcmd = 0, $force = 0)
624 $resolutions = Array(
625 $this->cfg->thumb_width,
626 $this->cfg->bubble_width,
627 $this->cfg->photo_width,
630 $details = $this->get_photo_details($idx);
631 $full_path = $this->translate_path($details['directory_path']) ."/". $details['name'];
632 $file_md5 = md5_file($full_path);
634 if($fromcmd) print "Image [". $idx ."] ". $details['name'] ." Thumbnails:";
638 foreach($resolutions as $resolution) {
640 $thumb_path = $this->cfg->base_path ."/thumbs/". $resolution ."_". $file_md5;
642 /* if the thumbnail file doesn't exist, create it */
643 if(!file_exists($thumb_path)) {
645 if($fromcmd) print " ". $resolution ."px";
646 if(!$this->create_thumbnail($full_path, $thumb_path, $resolution))
651 /* if the file hasn't changed there is no need to regen the thumb */
652 elseif($file_md5 != $this->getMD5($idx) || $force) {
654 if($fromcmd) print " ". $resolution ."px";
655 if(!$this->create_thumbnail($full_path, $thumb_path, $resolution))
661 /* set the new/changed MD5 sum for the current photo */
663 $this->setMD5($idx, $file_md5);
665 if($fromcmd) print "\n";
669 public function getMD5($idx)
671 $result = $this->cfg_db->db_query("
674 WHERE img_idx='". $idx ."'
680 $img = $this->cfg_db->db_fetch_object($result);
681 return $img['img_md5'];
685 private function setMD5($idx, $md5)
687 $result = $this->cfg_db->db_exec("
688 REPLACE INTO images (img_idx, img_md5)
689 VALUES ('". $idx ."', '". $md5 ."')
694 public function setTagCondition($mode)
696 $_SESSION['tag_condition'] = $mode;
698 } // setTagCondition()
700 public function startTagSearch($searchfor)
702 $_SESSION['searchfor'] = $searchfor;
703 $_SESSION['selected_tags'] = Array();
705 foreach($this->avail_tags as $tag) {
706 if(preg_match('/'. $searchfor .'/i', $this->tags[$tag]))
707 array_push($_SESSION['selected_tags'], $tag);
710 } // startTagSearch()
712 private function rotateImage($img, $degrees)
714 if(function_exists("imagerotate"))
715 $img = imagerotate($img, $degrees, 0);
718 function imagerotate($src_img, $angle)
720 $src_x = imagesx($src_img);
721 $src_y = imagesy($src_img);
726 elseif ($src_x <= $src_y) {
730 elseif ($src_x >= $src_y) {
735 $rotate=imagecreatetruecolor($dest_x,$dest_y);
736 imagealphablending($rotate, false);
741 for ($y = 0; $y < ($src_y); $y++) {
742 for ($x = 0; $x < ($src_x); $x++) {
743 $color = imagecolorat($src_img, $x, $y);
744 imagesetpixel($rotate, $dest_x - $y - 1, $x, $color);
750 for ($y = 0; $y < ($src_y); $y++) {
751 for ($x = 0; $x < ($src_x); $x++) {
752 $color = imagecolorat($src_img, $x, $y);
753 imagesetpixel($rotate, $y, $dest_y - $x - 1, $color);
759 for ($y = 0; $y < ($src_y); $y++) {
760 for ($x = 0; $x < ($src_x); $x++) {
761 $color = imagecolorat($src_img, $x, $y);
762 imagesetpixel($rotate, $dest_x - $x - 1, $dest_y - $y - 1, $color);
767 default: $rotate = $src_img;
774 $img = imagerotate($img, $degrees);
782 private function get_photo_tags($idx)
784 $result = $this->db->db_query("
787 INNER JOIN photo_tags pt
789 WHERE pt.photo_id='". $idx ."'
794 while($row = $this->db->db_fetch_object($result))
795 $tags[$row['id']] = $row['name'];
799 } // get_photo_tags()
801 function showTextImage($txt, $color=000000, $space=4, $font=4, $w=300)
803 if (strlen($color) != 6)
806 $int = hexdec($color);
807 $h = imagefontheight($font);
808 $fw = imagefontwidth($font);
809 $txt = explode("\n", wordwrap($txt, ($w / $fw), "\n"));
810 $lines = count($txt);
811 $im = imagecreate($w, (($h * $lines) + ($lines * $space)));
812 $bg = imagecolorallocate($im, 255, 255, 255);
813 $color = imagecolorallocate($im, 0xFF & ($int >> 0x10), 0xFF & ($int >> 0x8), 0xFF & $int);
816 foreach ($txt as $text) {
817 $x = (($w - ($fw * strlen($text))) / 2);
818 imagestring($im, $font, $x, $y, $text, $color);
822 Header("Content-type: image/png");