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']);
50 $this->tmpl->show("index.tpl");
54 private function get_tags()
56 $this->avail_tags = Array();
59 $result = $this->db->db_query("
62 ORDER BY sort_priority ASC
65 while($row = $this->db->db_fetch_object($result)) {
68 $tag_name = $row['name'];
70 /* check if config requests to ignore this tag */
71 if(in_array($row['name'], $this->cfg->hide_tags))
74 $this->tags[$tag_id] = $tag_name;
75 $this->avail_tags[$count] = $tag_id;
83 public function get_photo_details($idx)
85 $result = $this->db->db_query("
91 return $this->db->db_fetch_object($result);
93 } // get_photo_details
95 public function translate_path($path, $width = 0)
97 return str_replace($this->cfg->path_replace_from, $this->cfg->path_replace_to, $path);
101 public function showPhoto($photo)
103 $all_photos = $this->getPhotoSelection();
105 foreach($all_photos as $all_photo) {
108 $next_img = $all_photo;
112 if($all_photo == $photo) {
116 $previous_img = $all_photo;
120 $details = $this->get_photo_details($photo);
121 $orig_path = $this->translate_path($details['directory_path']) ."/". $details['name'];
122 $thumb_path = $this->cfg->base_path ."/thumbs/". $this->cfg->photo_width ."_". $this->getMD5($photo);
124 if(!file_exists($orig_path)) {
125 print "Photo ". $orig_path ." does not exist!<br />\n";
128 if(!is_readable($orig_path)) {
129 print "Photo ". $orig_path ." is not readable for user ". get_current_user() ."<br />\n";
132 /* If the thumbnail doesn't exist yet, try to create it */
133 if(!file_exists($thumb_path)) {
134 $this->gen_thumb($photo, 0, 1);
137 $meta = $this->get_meta_informations($orig_path);
139 if(file_exists($thumb_path)) {
141 $info = getimagesize($thumb_path);
143 $this->tmpl->assign('description', $details['description']);
144 $this->tmpl->assign('image_name', $details['name']);
146 $this->tmpl->assign('width', $info[0]);
147 $this->tmpl->assign('height', $info[1]);
148 $this->tmpl->assign('ExifMadeOn', strftime("%a %x %X", $meta['FileDateTime']));
149 $this->tmpl->assign('ExifMadeWith', $meta['Make'] ." ". $meta['Model']);
150 $this->tmpl->assign('ExifOrigResolution', $meta['ExifImageWidth'] ."x". $meta['ExifImageLength']);
151 $this->tmpl->assign('ExifFileSize', round($meta['FileSize']/1024, 1));
153 $this->tmpl->assign('image_url', 'phpfspot_img.php?idx='. $photo ."&width=". $this->cfg->photo_width);
154 $this->tmpl->assign('image_url_full', 'phpfspot_img.php?idx='. $photo);
156 $this->tmpl->assign('tags', $this->get_photo_tags($photo));
159 print "Can't open file ". $thumb_path ."\n";
163 $this->tmpl->assign('previous_url', "javascript:showImage(". $previous_img .");");
167 $this->tmpl->assign('next_url', "javascript:showImage(". $next_img .");");
170 $this->tmpl->show("single_photo.tpl");
174 public function getAvailableTags()
176 $result = $this->db->db_query("
177 SELECT tag_id as id, count(tag_id) as quantity
187 while($row = $this->db->db_fetch_object($result)) {
188 $tags[$row['id']] = $row['quantity'];
191 // change these font sizes if you will
192 $max_size = 125; // max font size in %
193 $min_size = 75; // min font size in %
195 // get the largest and smallest array values
196 $max_qty = max(array_values($tags));
197 $min_qty = min(array_values($tags));
199 // find the range of values
200 $spread = $max_qty - $min_qty;
201 if (0 == $spread) { // we don't want to divide by zero
205 // determine the font-size increment
206 // this is the increase per tag quantity (times used)
207 $step = ($max_size - $min_size)/($spread);
209 // loop through our tag array
210 foreach ($tags as $key => $value) {
212 if(isset($_SESSION['selected_tags']) && in_array($key, $_SESSION['selected_tags']))
215 // calculate CSS font-size
216 // find the $value in excess of $min_qty
217 // multiply by the font-size increment ($size)
218 // and add the $min_size set above
219 $size = $min_size + (($value - $min_qty) * $step);
220 // uncomment if you want sizes in whole %:
223 print "<a href=\"javascript:Tags('add', ". $key .");\" class=\"tag\" style=\"font-size: ". $size ."%;\">". $this->tags[$key] ."</a>, ";
227 } // getAvailableTags()
229 public function getSelectedTags()
231 foreach($this->avail_tags as $tag)
233 // return all selected tags
234 if(isset($_SESSION['selected_tags']) && in_array($tag, $_SESSION['selected_tags'])) {
235 print "<a href=\"javascript:Tags('del', ". $tag .");\" class=\"tag\">". $this->tags[$tag] ."</a> ";
240 } // getSelectedTags()
242 public function addTag($tag)
244 if(!isset($_SESSION['selected_tags']))
245 $_SESSION['selected_tags'] = Array();
247 if(!in_array($tag, $_SESSION['selected_tags']))
248 array_push($_SESSION['selected_tags'], $tag);
252 public function delTag($tag)
254 if(isset($_SESSION['selected_tags'])) {
255 $key = array_search($tag, $_SESSION['selected_tags']);
256 unset($_SESSION['selected_tags'][$key]);
257 sort($_SESSION['selected_tags']);
262 public function resetTags()
264 unset($_SESSION['selected_tags']);
268 public function getPhotoSelection()
270 $tagged_photos = Array();
272 /* return a search result */
273 if(isset($_SESSION['searchfor']) && $_SESSION['searchfor'] != '') {
274 $result = $this->db->db_query("
275 SELECT DISTINCT photo_id
281 WHERE t.name LIKE '%". $_SESSION['searchfor'] ."%'
284 while($row = $this->db->db_fetch_object($result)) {
285 array_push($tagged_photos, $row['photo_id']);
287 return $tagged_photos;
290 /* return according the selected tags */
291 if(isset($_SESSION['selected_tags']) && !empty($_SESSION['selected_tags'])) {
293 foreach($_SESSION['selected_tags'] as $tag)
294 $selected.= $tag .",";
295 $selected = substr($selected, 0, strlen($selected)-1);
297 if($_SESSION['tag_condition'] == 'or') {
298 $result = $this->db->db_query("
299 SELECT DISTINCT photo_id
303 WHERE pt.tag_id IN (". $selected .")
307 elseif($_SESSION['tag_condition'] == 'and') {
309 /* Join together a table looking like
311 pt1.photo_id pt1.tag_id pt2.photo_id pt2.tag_id ...
313 so the query can quickly return all images matching the
314 selected tags in an AND condition
319 SELECT DISTINCT pt1.photo_id
323 for($i = 0; $i < count($_SESSION['selected_tags']); $i++) {
325 INNER JOIN photo_tags pt". ($i+2) ."
326 ON pt1.photo_id=pt". ($i+2) .".photo_id
329 $query_str.= "WHERE pt1.tag_id=". $_SESSION['selected_tags'][0];
330 for($i = 1; $i < count($_SESSION['selected_tags']); $i++) {
332 AND pt". ($i+1) .".tag_id=". $_SESSION['selected_tags'][$i] ."
335 $result = $this->db->db_query($query_str);
338 while($row = $this->db->db_fetch_object($result)) {
339 array_push($tagged_photos, $row['photo_id']);
341 return $tagged_photos;
344 /* return all available photos */
345 $result = $this->db->db_query("
346 SELECT DISTINCT photo_id
352 while($row = $this->db->db_fetch_object($result)) {
353 array_push($tagged_photos, $row['photo_id']);
355 return $tagged_photos;
357 } // getPhotoSelection()
359 public function showPhotoIndex()
361 $photos = $this->getPhotoSelection();
363 $count = count($photos);
365 if(!$_SESSION['begin_with'] || $_SESSION['begin_with'] == 0)
368 $begin_with = $_SESSION['begin_with'];
370 if($this->cfg->rows_per_page == 0)
373 $end_with = $begin_with + ($this->cfg->rows_per_page * $this->cfg->thumbs_per_row);
378 $images[$rows] = Array();
379 $img_height[$rows] = Array();
380 $img_width[$rows] = Array();
382 for($i = $begin_with; $i < $end_with; $i++) {
384 $images[$rows][$cols] = $photos[$i];
386 $thumb_path = $this->cfg->base_path ."/thumbs/". $this->cfg->thumb_width ."_". $this->getMD5($photos[$i]);
388 if(file_exists($thumb_path)) {
389 $info = getimagesize($thumb_path);
390 $img_width[$rows][$cols] = $info[0];
391 $img_height[$rows][$cols] = $info[1];
394 if($cols == $this->cfg->thumbs_per_row-1) {
397 $images[$rows] = Array();
398 $img_width[$rows] = Array();
399 $img_height[$rows] = Array();
406 // +1 for for smarty's selection iteration
409 if(isset($_SESSION['searchfor']) && $_SESSION['searchfor'] != '')
410 $this->tmpl->assign('searchfor', $_SESSION['searchfor']);
412 if($this->cfg->rows_per_page != 0) {
413 $previous_start = $begin_with - ($this->cfg->rows_per_page * $this->cfg->thumbs_per_row);
414 $next_start = $begin_with + ($this->cfg->rows_per_page * $this->cfg->thumbs_per_row);
417 $this->tmpl->assign("previous_url", "javascript:showPhotoIndex(". $previous_start .");");
418 if($end_with < $count)
419 $this->tmpl->assign("next_url", "javascript:showPhotoIndex(". $next_start .");");
422 $this->tmpl->assign('count', $count);
423 $this->tmpl->assign('width', $this->cfg->thumb_width);
424 $this->tmpl->assign('images', $images);
425 $this->tmpl->assign('img_width', $img_width);
426 $this->tmpl->assign('img_height', $img_height);
427 $this->tmpl->assign('rows', $rows);
428 $this->tmpl->assign('columns', $this->cfg->thumbs_per_row);
429 $this->tmpl->show("photo_index.tpl");
432 } // showPhotoIndex()
434 public function showBubbleDetails($photo, $direction)
436 if($direction == "up")
437 $direction = "bubbleimg_up";
439 $direction = "bubbleimg_down";
441 $details = $this->get_photo_details($photo);
442 $orig_path = $this->translate_path($details['directory_path']) ."/". $details['name'];
444 $image_url = "phpfspot_img.php?idx=". $photo ."&width=". $this->cfg->bubble_width;
446 $filesize = filesize($orig_path);
447 $filesize = rand($filesize/1024, 2);
449 if(!file_exists($orig_path)) {
450 print "Photo ". $orig_path ." does not exist!<br />\n";
454 if(!is_readable($orig_path)) {
455 print "Photo ". $orig_path ." is not readable for user ". get_current_user() ."<br />\n";
459 $img = getimagesize($orig_path);
461 $this->tmpl->assign('file_size', $filesize);
462 $this->tmpl->assign('width', $img[0]);
463 $this->tmpl->assign('height', $img[1]);
464 $this->tmpl->assign('file_name', $details['name']);
465 $this->tmpl->assign('image_id', $direction);
466 $this->tmpl->assign('image_url', $image_url);
467 $this->tmpl->show("bubble_details.tpl");
469 } // showBubbleDetails()
471 public function showCredits()
473 $this->tmpl->assign('version', $this->cfg->version);
474 $this->tmpl->assign('product', $this->cfg->product);
475 $this->tmpl->show("credits.tpl");
479 public function create_thumbnail($orig_image, $thumb_image, $width)
481 if(!file_exists($orig_image))
484 $meta = $this->get_meta_informations($orig_image);
489 switch($meta['Orientation']) {
492 $rotate = 0; $flip = false; break;
494 $rotate = 0; $flip = true; break;
496 $rotate = 180; $flip = false; break;
498 $rotate = 180; $flip = true; break;
500 $rotate = 90; $flip = true; break;
502 $rotate = 90; $flip = false; break;
504 $rotate = 270; $flip = true; break;
506 $rotate = 270; $flip = false; break;
509 $src_img = @imagecreatefromjpeg($orig_image);
512 print "Can't load image from ". $orig_image ."\n";
516 /* grabs the height and width */
517 $new_w = imagesx($src_img);
518 $new_h = imagesy($src_img);
520 // If requested width is more then the actual image width,
521 // do not generate a thumbnail
523 if($width >= $new_w) {
524 imagedestroy($src_img);
528 /* calculates aspect ratio */
529 $aspect_ratio = $new_h / $new_w;
533 $new_h = abs($new_w * $aspect_ratio);
535 /* creates new image of that size */
536 $dst_img = imagecreatetruecolor($new_w, $new_h);
538 imagefill($dst_img, 0, 0, ImageColorAllocate($dst_img, 255, 255, 255));
540 /* copies resized portion of original image into new image */
541 imagecopyresampled($dst_img,$src_img,0,0,0,0,$new_w,$new_h,imagesx($src_img),imagesy($src_img));
543 /* needs the image to be flipped horizontal? */
547 for($x = 0; $x < $new_w; $x++) {
548 imagecopy($dst_img, $image, $x, 0, $w - $x - 1, 0, 1, $h);
554 $dst_img = $this->rotateImage($dst_img, $rotate);
557 /* write down new generated file */
558 $result = imagejpeg($dst_img, $thumb_image, 75);
561 imagedestroy($dst_img);
562 imagedestroy($src_img);
564 if($result === false) {
565 print "Can't write thumbnail ". $thumb_image ."\n";
571 } // create_thumbnail()
573 public function get_meta_informations($file)
575 return exif_read_data($file);
577 } // get_meta_informations()
579 public function check_config_table()
581 // if the config table doesn't exist yet, create it
582 if(!$this->cfg_db->db_check_table_exists("images")) {
583 $this->cfg_db->db_exec("
584 CREATE TABLE images (
585 img_idx int primary key,
591 } // check_config_table
594 * Generates a thumbnail from photo idx
596 * This function will generate JPEG thumbnails from provided F-Spot photo
599 * 1. Check if all thumbnail generations (width) are already in place and
601 * 2. Check if the md5sum of the original file has changed
602 * 3. Generate the thumbnails if needed
604 public function gen_thumb($idx = 0, $fromcmd = 0, $force = 0)
606 $resolutions = Array(
607 $this->cfg->thumb_width,
608 $this->cfg->bubble_width,
609 $this->cfg->photo_width,
612 $details = $this->get_photo_details($idx);
613 $full_path = $this->translate_path($details['directory_path']) ."/". $details['name'];
614 $file_md5 = md5_file($full_path);
616 if($fromcmd) print "Image [". $idx ."] ". $details['name'] ." Thumbnails:";
620 foreach($resolutions as $resolution) {
622 $thumb_path = $this->cfg->base_path ."/thumbs/". $resolution ."_". $file_md5;
624 /* if the thumbnail file doesn't exist, create it */
625 if(!file_exists($thumb_path)) {
627 if($fromcmd) print " ". $resolution ."px";
628 if(!$this->create_thumbnail($full_path, $thumb_path, $resolution))
633 /* if the file hasn't changed there is no need to regen the thumb */
634 elseif($file_md5 != $this->getMD5($idx) || $force) {
636 if($fromcmd) print " ". $resolution ."px";
637 if(!$this->create_thumbnail($full_path, $thumb_path, $resolution))
643 /* set the new/changed MD5 sum for the current photo */
645 $this->setMD5($idx, $file_md5);
647 if($fromcmd) print "\n";
651 public function getMD5($idx)
653 $result = $this->cfg_db->db_query("
656 WHERE img_idx='". $idx ."'
662 $img = $this->cfg_db->db_fetch_object($result);
663 return $img['img_md5'];
667 private function setMD5($idx, $md5)
669 $result = $this->cfg_db->db_exec("
670 REPLACE INTO images (img_idx, img_md5)
671 VALUES ('". $idx ."', '". $md5 ."')
676 public function setTagCondition($mode)
678 $_SESSION['tag_condition'] = $mode;
680 } // setTagCondition()
682 public function startSearch($searchfor)
684 $_SESSION['searchfor'] = $searchfor;
688 private function rotateImage($img, $degrees)
690 if(function_exists("imagerotate"))
691 $img = imagerotate($img, $degrees, 0);
694 function imagerotate($src_img, $angle)
696 $src_x = imagesx($src_img);
697 $src_y = imagesy($src_img);
702 elseif ($src_x <= $src_y) {
706 elseif ($src_x >= $src_y) {
711 $rotate=imagecreatetruecolor($dest_x,$dest_y);
712 imagealphablending($rotate, false);
717 for ($y = 0; $y < ($src_y); $y++) {
718 for ($x = 0; $x < ($src_x); $x++) {
719 $color = imagecolorat($src_img, $x, $y);
720 imagesetpixel($rotate, $dest_x - $y - 1, $x, $color);
726 for ($y = 0; $y < ($src_y); $y++) {
727 for ($x = 0; $x < ($src_x); $x++) {
728 $color = imagecolorat($src_img, $x, $y);
729 imagesetpixel($rotate, $y, $dest_y - $x - 1, $color);
735 for ($y = 0; $y < ($src_y); $y++) {
736 for ($x = 0; $x < ($src_x); $x++) {
737 $color = imagecolorat($src_img, $x, $y);
738 imagesetpixel($rotate, $dest_x - $x - 1, $dest_y - $y - 1, $color);
743 default: $rotate = $src_img;
750 $img = imagerotate($img, $degrees);
758 private function get_photo_tags($idx)
760 $result = $this->db->db_query("
763 INNER JOIN photo_tags pt
765 WHERE pt.photo_id='". $idx ."'
770 while($row = $this->db->db_fetch_object($result))
771 $tags[$row['id']] = $row['name'];
775 } // get_photo_tags()
777 function showTextImage($txt, $color=000000, $space=4, $font=4, $w=300)
779 if (strlen($color) != 6)
782 $int = hexdec($color);
783 $h = imagefontheight($font);
784 $fw = imagefontwidth($font);
785 $txt = explode("\n", wordwrap($txt, ($w / $fw), "\n"));
786 $lines = count($txt);
787 $im = imagecreate($w, (($h * $lines) + ($lines * $space)));
788 $bg = imagecolorallocate($im, 255, 255, 255);
789 $color = imagecolorallocate($im, 0xFF & ($int >> 0x10), 0xFF & ($int >> 0x8), 0xFF & $int);
792 foreach ($txt as $text) {
793 $x = (($w - ($fw * strlen($text))) / 2);
794 imagestring($im, $font, $x, $y, $text, $color);
798 Header("Content-type: image/png");