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 the thumbnail doesn't exist yet, try to create it */
125 if(!file_exists($thumb_path)) {
126 $this->gen_thumb($photo, 0, 1);
129 $meta = $this->get_meta_informations($orig_path);
131 if(file_exists($thumb_path)) {
133 $info = getimagesize($thumb_path);
135 $this->tmpl->assign('description', $details['description']);
136 $this->tmpl->assign('image_name', $details['name']);
138 $this->tmpl->assign('width', $info[0]);
139 $this->tmpl->assign('height', $info[1]);
140 $this->tmpl->assign('ExifMadeOn', strftime("%a %x %X", $meta['FileDateTime']));
141 $this->tmpl->assign('ExifMadeWith', $meta['Make'] ." ". $meta['Model']);
142 $this->tmpl->assign('ExifOrigResolution', $meta['ExifImageWidth'] ."x". $meta['ExifImageLength']);
143 $this->tmpl->assign('ExifFileSize', round($meta['FileSize']/1024, 1));
145 $this->tmpl->assign('image_url', 'phpfspot_img.php?idx='. $photo ."&width=". $this->cfg->photo_width);
146 $this->tmpl->assign('image_url_full', 'phpfspot_img.php?idx='. $photo);
148 $this->tmpl->assign('tags', $this->get_photo_tags($photo));
151 print "Can't open file ". $thumb_path ."\n";
155 $this->tmpl->assign('previous_url', "javascript:showImage(". $previous_img .");");
159 $this->tmpl->assign('next_url', "javascript:showImage(". $next_img .");");
162 $this->tmpl->show("single_photo.tpl");
166 public function getAvailableTags()
168 $result = $this->db->db_query("
169 SELECT tag_id as id, count(tag_id) as quantity
179 while($row = $this->db->db_fetch_object($result)) {
180 $tags[$row['id']] = $row['quantity'];
183 // change these font sizes if you will
184 $max_size = 125; // max font size in %
185 $min_size = 75; // min font size in %
187 // get the largest and smallest array values
188 $max_qty = max(array_values($tags));
189 $min_qty = min(array_values($tags));
191 // find the range of values
192 $spread = $max_qty - $min_qty;
193 if (0 == $spread) { // we don't want to divide by zero
197 // determine the font-size increment
198 // this is the increase per tag quantity (times used)
199 $step = ($max_size - $min_size)/($spread);
201 // loop through our tag array
202 foreach ($tags as $key => $value) {
204 if(isset($_SESSION['selected_tags']) && in_array($key, $_SESSION['selected_tags']))
207 // calculate CSS font-size
208 // find the $value in excess of $min_qty
209 // multiply by the font-size increment ($size)
210 // and add the $min_size set above
211 $size = $min_size + (($value - $min_qty) * $step);
212 // uncomment if you want sizes in whole %:
215 print "<a href=\"javascript:Tags('add', ". $key .");\" class=\"tag\" style=\"font-size: ". $size ."%;\">". $this->tags[$key] ."</a>, ";
219 } // getAvailableTags()
221 public function getSelectedTags()
223 foreach($this->avail_tags as $tag)
225 // return all selected tags
226 if(isset($_SESSION['selected_tags']) && in_array($tag, $_SESSION['selected_tags'])) {
227 print "<a href=\"javascript:Tags('del', ". $tag .");\" class=\"tag\">". $this->tags[$tag] ."</a> ";
232 } // getSelectedTags()
234 public function addTag($tag)
236 if(!isset($_SESSION['selected_tags']))
237 $_SESSION['selected_tags'] = Array();
239 if(!in_array($tag, $_SESSION['selected_tags']))
240 array_push($_SESSION['selected_tags'], $tag);
244 public function delTag($tag)
246 if(isset($_SESSION['selected_tags'])) {
247 $key = array_search($tag, $_SESSION['selected_tags']);
248 unset($_SESSION['selected_tags'][$key]);
249 sort($_SESSION['selected_tags']);
254 public function resetTags()
256 unset($_SESSION['selected_tags']);
260 public function getPhotoSelection()
262 $tagged_photos = Array();
264 /* return a search result */
265 if(isset($_SESSION['searchfor']) && $_SESSION['searchfor'] != '') {
266 $result = $this->db->db_query("
267 SELECT DISTINCT photo_id
273 WHERE t.name LIKE '%". $_SESSION['searchfor'] ."%'
276 while($row = $this->db->db_fetch_object($result)) {
277 array_push($tagged_photos, $row['photo_id']);
279 return $tagged_photos;
282 /* return according the selected tags */
283 if(isset($_SESSION['selected_tags']) && !empty($_SESSION['selected_tags'])) {
285 foreach($_SESSION['selected_tags'] as $tag)
286 $selected.= $tag .",";
287 $selected = substr($selected, 0, strlen($selected)-1);
289 if($_SESSION['tag_condition'] == 'or') {
290 $result = $this->db->db_query("
291 SELECT DISTINCT photo_id
295 WHERE pt.tag_id IN (". $selected .")
299 elseif($_SESSION['tag_condition'] == 'and') {
301 /* Join together a table looking like
303 pt1.photo_id pt1.tag_id pt2.photo_id pt2.tag_id ...
305 so the query can quickly return all images matching the
306 selected tags in an AND condition
311 SELECT DISTINCT pt1.photo_id
315 for($i = 0; $i < count($_SESSION['selected_tags']); $i++) {
317 INNER JOIN photo_tags pt". ($i+2) ."
318 ON pt1.photo_id=pt". ($i+2) .".photo_id
321 $query_str.= "WHERE pt1.tag_id=". $_SESSION['selected_tags'][0];
322 for($i = 1; $i < count($_SESSION['selected_tags']); $i++) {
324 AND pt". ($i+1) .".tag_id=". $_SESSION['selected_tags'][$i] ."
327 $result = $this->db->db_query($query_str);
330 while($row = $this->db->db_fetch_object($result)) {
331 array_push($tagged_photos, $row['photo_id']);
333 return $tagged_photos;
336 /* return all available photos */
337 $result = $this->db->db_query("
338 SELECT DISTINCT photo_id
344 while($row = $this->db->db_fetch_object($result)) {
345 array_push($tagged_photos, $row['photo_id']);
347 return $tagged_photos;
349 } // getPhotoSelection()
351 public function showPhotoIndex()
353 $photos = $this->getPhotoSelection();
355 $count = count($photos);
357 if(!$_SESSION['begin_with'] || $_SESSION['begin_with'] == 0)
360 $begin_with = $_SESSION['begin_with'];
362 if($this->cfg->rows_per_page == 0)
365 $end_with = $begin_with + ($this->cfg->rows_per_page * $this->cfg->thumbs_per_row);
370 $images[$rows] = Array();
371 $img_height[$rows] = Array();
372 $img_width[$rows] = Array();
374 for($i = $begin_with; $i < $end_with; $i++) {
376 $images[$rows][$cols] = $photos[$i];
378 $thumb_path = $this->cfg->base_path ."/thumbs/". $this->cfg->thumb_width ."_". $this->getMD5($photos[$i]);
380 if(file_exists($thumb_path)) {
381 $info = getimagesize($thumb_path);
382 $img_width[$rows][$cols] = $info[0];
383 $img_height[$rows][$cols] = $info[1];
386 if($cols == $this->cfg->thumbs_per_row-1) {
389 $images[$rows] = Array();
390 $img_width[$rows] = Array();
391 $img_height[$rows] = Array();
398 // +1 for for smarty's selection iteration
401 if(isset($_SESSION['searchfor']) && $_SESSION['searchfor'] != '')
402 $this->tmpl->assign('searchfor', $_SESSION['searchfor']);
404 if($this->cfg->rows_per_page != 0) {
405 $previous_start = $begin_with - ($this->cfg->rows_per_page * $this->cfg->thumbs_per_row);
406 $next_start = $begin_with + ($this->cfg->rows_per_page * $this->cfg->thumbs_per_row);
409 $this->tmpl->assign("previous_url", "javascript:showPhotoIndex(". $previous_start .");");
410 if($end_with < $count)
411 $this->tmpl->assign("next_url", "javascript:showPhotoIndex(". $next_start .");");
414 $this->tmpl->assign('count', $count);
415 $this->tmpl->assign('width', $this->cfg->thumb_width);
416 $this->tmpl->assign('images', $images);
417 $this->tmpl->assign('img_width', $img_width);
418 $this->tmpl->assign('img_height', $img_height);
419 $this->tmpl->assign('rows', $rows);
420 $this->tmpl->assign('columns', $this->cfg->thumbs_per_row);
421 $this->tmpl->show("photo_index.tpl");
424 } // showPhotoIndex()
426 public function showBubbleDetails($photo, $direction)
428 if($direction == "up")
429 $direction = "bubbleimg_up";
431 $direction = "bubbleimg_down";
433 $details = $this->get_photo_details($photo);
435 $image_url = "phpfspot_img.php?idx=". $photo ."&width=". $this->cfg->bubble_width;
437 $filesize = filesize($this->translate_path($details['directory_path']) ."/". $details['name']);
438 $filesize = rand($filesize/1024, 2);
440 $img = getimagesize($this->translate_path($details['directory_path']) ."/". $details['name']);
442 $this->tmpl->assign('file_size', $filesize);
443 $this->tmpl->assign('width', $img[0]);
444 $this->tmpl->assign('height', $img[1]);
445 $this->tmpl->assign('file_name', $details['name']);
446 $this->tmpl->assign('image_id', $direction);
447 $this->tmpl->assign('image_url', $image_url);
448 $this->tmpl->show("bubble_details.tpl");
450 } // showBubbleDetails()
452 public function showCredits()
454 $this->tmpl->assign('version', $this->cfg->version);
455 $this->tmpl->assign('product', $this->cfg->product);
456 $this->tmpl->show("credits.tpl");
460 public function create_thumbnail($orig_image, $thumb_image, $width)
462 if(!file_exists($orig_image))
465 $meta = $this->get_meta_informations($orig_image);
470 switch($meta['Orientation']) {
473 $rotate = 0; $flip = false; break;
475 $rotate = 0; $flip = true; break;
477 $rotate = 180; $flip = false; break;
479 $rotate = 180; $flip = true; break;
481 $rotate = 90; $flip = true; break;
483 $rotate = 90; $flip = false; break;
485 $rotate = 270; $flip = true; break;
487 $rotate = 270; $flip = false; break;
490 $src_img = @imagecreatefromjpeg($orig_image);
493 print "Can't load image from ". $orig_image ."\n";
497 /* grabs the height and width */
498 $new_w = imagesx($src_img);
499 $new_h = imagesy($src_img);
501 // If requested width is more then the actual image width,
502 // do not generate a thumbnail
504 if($width >= $new_w) {
505 imagedestroy($src_img);
509 /* calculates aspect ratio */
510 $aspect_ratio = $new_h / $new_w;
514 $new_h = abs($new_w * $aspect_ratio);
516 /* creates new image of that size */
517 $dst_img = imagecreatetruecolor($new_w, $new_h);
519 imagefill($dst_img, 0, 0, ImageColorAllocate($dst_img, 255, 255, 255));
521 /* copies resized portion of original image into new image */
522 imagecopyresampled($dst_img,$src_img,0,0,0,0,$new_w,$new_h,imagesx($src_img),imagesy($src_img));
524 /* needs the image to be flipped horizontal? */
528 for($x = 0; $x < $new_w; $x++) {
529 imagecopy($dst_img, $image, $x, 0, $w - $x - 1, 0, 1, $h);
535 $dst_img = $this->rotateImage($dst_img, $rotate);
538 /* write down new generated file */
539 $result = imagejpeg($dst_img, $thumb_image, 75);
542 imagedestroy($dst_img);
543 imagedestroy($src_img);
545 if($result === false) {
546 print "Can't write thumbnail ". $thumb_image ."\n";
552 } // create_thumbnail()
554 public function get_meta_informations($file)
556 return exif_read_data($file);
558 } // get_meta_informations()
560 public function check_config_table()
562 // if the config table doesn't exist yet, create it
563 if(!$this->cfg_db->db_check_table_exists("images")) {
564 $this->cfg_db->db_exec("
565 CREATE TABLE images (
566 img_idx int primary key,
572 } // check_config_table
575 * Generates a thumbnail from photo idx
577 * This function will generate JPEG thumbnails from provided F-Spot photo
580 * 1. Check if all thumbnail generations (width) are already in place and
582 * 2. Check if the md5sum of the original file has changed
583 * 3. Generate the thumbnails if needed
585 public function gen_thumb($idx = 0, $fromcmd = 0, $force = 0)
587 $resolutions = Array(
588 $this->cfg->thumb_width,
589 $this->cfg->bubble_width,
590 $this->cfg->photo_width,
593 $details = $this->get_photo_details($idx);
594 $full_path = $this->translate_path($details['directory_path']) ."/". $details['name'];
595 $file_md5 = md5_file($full_path);
597 if($fromcmd) print "Image [". $idx ."] ". $details['name'] ." Thumbnails:";
601 foreach($resolutions as $resolution) {
603 $thumb_path = $this->cfg->base_path ."/thumbs/". $resolution ."_". $file_md5;
605 /* if the thumbnail file doesn't exist, create it */
606 if(!file_exists($thumb_path)) {
608 if($fromcmd) print " ". $resolution ."px";
609 if(!$this->create_thumbnail($full_path, $thumb_path, $resolution))
614 /* if the file hasn't changed there is no need to regen the thumb */
615 elseif($file_md5 != $this->getMD5($idx) || $force) {
617 if($fromcmd) print " ". $resolution ."px";
618 if(!$this->create_thumbnail($full_path, $thumb_path, $resolution))
624 /* set the new/changed MD5 sum for the current photo */
626 $this->setMD5($idx, $file_md5);
628 if($fromcmd) print "\n";
632 public function getMD5($idx)
634 $result = $this->cfg_db->db_query("
637 WHERE img_idx='". $idx ."'
643 $img = $this->cfg_db->db_fetch_object($result);
644 return $img['img_md5'];
648 private function setMD5($idx, $md5)
650 $result = $this->cfg_db->db_exec("
651 REPLACE INTO images (img_idx, img_md5)
652 VALUES ('". $idx ."', '". $md5 ."')
657 public function setTagCondition($mode)
659 $_SESSION['tag_condition'] = $mode;
661 } // setTagCondition()
663 public function startSearch($searchfor)
665 $_SESSION['searchfor'] = $searchfor;
669 private function rotateImage($img, $degrees)
671 if(function_exists("imagerotate"))
672 $img = imagerotate($img, $degrees, 0);
675 function imagerotate($src_img, $angle)
677 $src_x = imagesx($src_img);
678 $src_y = imagesy($src_img);
683 elseif ($src_x <= $src_y) {
687 elseif ($src_x >= $src_y) {
692 $rotate=imagecreatetruecolor($dest_x,$dest_y);
693 imagealphablending($rotate, false);
698 for ($y = 0; $y < ($src_y); $y++) {
699 for ($x = 0; $x < ($src_x); $x++) {
700 $color = imagecolorat($src_img, $x, $y);
701 imagesetpixel($rotate, $dest_x - $y - 1, $x, $color);
707 for ($y = 0; $y < ($src_y); $y++) {
708 for ($x = 0; $x < ($src_x); $x++) {
709 $color = imagecolorat($src_img, $x, $y);
710 imagesetpixel($rotate, $y, $dest_y - $x - 1, $color);
716 for ($y = 0; $y < ($src_y); $y++) {
717 for ($x = 0; $x < ($src_x); $x++) {
718 $color = imagecolorat($src_img, $x, $y);
719 imagesetpixel($rotate, $dest_x - $x - 1, $dest_y - $y - 1, $color);
724 default: $rotate = $src_img;
731 $img = imagerotate($img, $degrees);
739 private function get_photo_tags($idx)
741 $result = $this->db->db_query("
744 INNER JOIN photo_tags pt
746 WHERE pt.photo_id='". $idx ."'
751 while($row = $this->db->db_fetch_object($result))
752 $tags[$row['id']] = $row['name'];
756 } // get_photo_tags()