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;
121 $details = $this->get_photo_details($photo);
122 $meta = $this->get_meta_informations($this->translate_path($details['directory_path']) ."/". $details['name']);
123 $info = getimagesize($this->translate_path($details['directory_path']) ."/thumbs/". $this->cfg->photo_width ."_". $details['name']);
125 $this->tmpl->assign('description', $details['description']);
126 $this->tmpl->assign('image_name', $details['name']);
128 $this->tmpl->assign('width', $info[0]);
129 $this->tmpl->assign('height', $info[1]);
130 $this->tmpl->assign('ExifMadeOn', strftime("%a %x %X", $meta['FileDateTime']));
131 $this->tmpl->assign('ExifMadeWith', $meta['Make'] ." ". $meta['Model']);
132 $this->tmpl->assign('ExifOrigResolution', $meta['ExifImageWidth'] ."x". $meta['ExifImageLength']);
133 $this->tmpl->assign('ExifFileSize', round($meta['FileSize']/1024, 1));
135 $this->tmpl->assign('image_url', 'phpfspot_img.php?idx='. $photo ."&width=". $this->cfg->photo_width);
136 $this->tmpl->assign('image_url_full', 'phpfspot_img.php?idx='. $photo);
138 $this->tmpl->assign('tags', $this->get_photo_tags($photo));
141 $this->tmpl->assign('previous_url', "javascript:showImage(". $previous_img .");");
145 $this->tmpl->assign('next_url', "javascript:showImage(". $next_img .");");
148 $this->tmpl->show("single_photo.tpl");
152 public function getAvailableTags()
154 $result = $this->db->db_query("
155 SELECT tag_id as id, count(tag_id) as quantity
165 while($row = $this->db->db_fetch_object($result)) {
166 $tags[$row['id']] = $row['quantity'];
169 // change these font sizes if you will
170 $max_size = 125; // max font size in %
171 $min_size = 75; // min font size in %
173 // get the largest and smallest array values
174 $max_qty = max(array_values($tags));
175 $min_qty = min(array_values($tags));
177 // find the range of values
178 $spread = $max_qty - $min_qty;
179 if (0 == $spread) { // we don't want to divide by zero
183 // determine the font-size increment
184 // this is the increase per tag quantity (times used)
185 $step = ($max_size - $min_size)/($spread);
187 // loop through our tag array
188 foreach ($tags as $key => $value) {
190 if(isset($_SESSION['selected_tags']) && in_array($key, $_SESSION['selected_tags']))
193 // calculate CSS font-size
194 // find the $value in excess of $min_qty
195 // multiply by the font-size increment ($size)
196 // and add the $min_size set above
197 $size = $min_size + (($value - $min_qty) * $step);
198 // uncomment if you want sizes in whole %:
201 print "<a href=\"javascript:Tags('add', ". $key .");\" class=\"tag\" style=\"font-size: ". $size ."%;\">". $this->tags[$key] ."</a> ";
205 } // getAvailableTags()
207 public function getSelectedTags()
209 foreach($this->avail_tags as $tag)
211 // return all selected tags
212 if(isset($_SESSION['selected_tags']) && in_array($tag, $_SESSION['selected_tags'])) {
213 print "<a href=\"javascript:Tags('del', ". $tag .");\" class=\"tag\">". $this->tags[$tag] ."</a> ";
218 } // getSelectedTags()
220 public function addTag($tag)
222 if(!isset($_SESSION['selected_tags']))
223 $_SESSION['selected_tags'] = Array();
225 if(!in_array($tag, $_SESSION['selected_tags']))
226 array_push($_SESSION['selected_tags'], $tag);
230 public function delTag($tag)
232 if(isset($_SESSION['selected_tags'])) {
233 $key = array_search($tag, $_SESSION['selected_tags']);
234 unset($_SESSION['selected_tags'][$key]);
235 sort($_SESSION['selected_tags']);
240 public function resetTags()
242 unset($_SESSION['selected_tags']);
246 public function getPhotoSelection()
248 $tagged_photos = Array();
250 /* return a search result */
251 if(isset($_SESSION['searchfor']) && $_SESSION['searchfor'] != '') {
252 $result = $this->db->db_query("
253 SELECT DISTINCT photo_id
259 WHERE t.name LIKE '%". $_SESSION['searchfor'] ."%'
262 while($row = $this->db->db_fetch_object($result)) {
263 array_push($tagged_photos, $row['photo_id']);
265 return $tagged_photos;
268 /* return according the selected tags */
269 if(isset($_SESSION['selected_tags'])) {
271 foreach($_SESSION['selected_tags'] as $tag)
272 $selected.= $tag .",";
273 $selected = substr($selected, 0, strlen($selected)-1);
275 if($_SESSION['tag_condition'] == 'or') {
276 $result = $this->db->db_query("
277 SELECT DISTINCT photo_id
281 WHERE pt.tag_id IN (". $selected .")
285 elseif($_SESSION['tag_condition'] == 'and') {
287 /* Join together a table looking like
289 pt1.photo_id pt1.tag_id pt2.photo_id pt2.tag_id ...
291 so the query can quickly return all images matching the
292 selected tags in an AND condition
297 SELECT DISTINCT pt1.photo_id
301 for($i = 0; $i < count($_SESSION['selected_tags']); $i++) {
303 INNER JOIN photo_tags pt". ($i+2) ."
304 ON pt1.photo_id=pt". ($i+2) .".photo_id
307 $query_str.= "WHERE pt1.tag_id=". $_SESSION['selected_tags'][0];
308 for($i = 1; $i < count($_SESSION['selected_tags']); $i++) {
310 AND pt". ($i+1) .".tag_id=". $_SESSION['selected_tags'][$i] ."
313 $result = $this->db->db_query($query_str);
316 while($row = $this->db->db_fetch_object($result)) {
317 array_push($tagged_photos, $row['photo_id']);
319 return $tagged_photos;
322 /* return all available photos */
323 $result = $this->db->db_query("
324 SELECT DISTINCT photo_id
330 while($row = $this->db->db_fetch_object($result)) {
331 array_push($tagged_photos, $row['photo_id']);
333 return $tagged_photos;
335 } // getPhotoSelection()
337 public function showPhotoIndex()
339 $photos = $this->getPhotoSelection();
341 $count = count($photos);
345 $images[$rows] = Array();
347 for($i = 0; $i < $count; $i++) {
349 $images[$rows][$cols] = $photos[$i];
351 if($cols == $this->cfg->thumbs_per_row-1) {
354 $images[$rows] = Array();
361 // +1 for for smarty's selection iteration
364 if(isset($_SESSION['searchfor']) && $_SESSION['searchfor'] != '')
365 $this->tmpl->assign('searchfor', $_SESSION['searchfor']);
367 $this->tmpl->assign('count', $count);
368 $this->tmpl->assign('width', $this->cfg->thumb_width);
369 $this->tmpl->assign('images', $images);
370 $this->tmpl->assign('rows', $rows);
371 $this->tmpl->assign('columns', $this->cfg->thumbs_per_row);
372 $this->tmpl->show("photo_index.tpl");
375 } // showPhotoIndex()
377 public function showBubbleDetails($photo, $direction)
379 if($direction == "up")
380 $direction = "bubbleimg_up";
382 $direction = "bubbleimg_down";
384 $details = $this->get_photo_details($photo);
386 $image_url = "phpfspot_img.php?idx=". $photo ."&width=". $this->cfg->bubble_width;
388 $filesize = filesize($this->translate_path($details['directory_path']) ."/". $details['name']);
389 $filesize = rand($filesize/1024, 2);
391 $img = getimagesize($this->translate_path($details['directory_path']) ."/". $details['name']);
393 $this->tmpl->assign('file_size', $filesize);
394 $this->tmpl->assign('width', $img[0]);
395 $this->tmpl->assign('height', $img[1]);
396 $this->tmpl->assign('file_name', $details['name']);
397 $this->tmpl->assign('image_id', $direction);
398 $this->tmpl->assign('image_url', $image_url);
399 $this->tmpl->show("bubble_details.tpl");
401 } // showBubbleDetails()
403 public function showCredits()
405 $this->tmpl->assign('version', $this->cfg->version);
406 $this->tmpl->assign('product', $this->cfg->product);
407 $this->tmpl->show("credits.tpl");
411 public function create_thumbnail($image, $width)
413 $meta = $this->get_meta_informations($image);
418 switch($meta['Orientation']) {
461 $src_img = @imagecreatefromjpeg($image);
465 /* grabs the height and width */
466 $new_w = imagesx($src_img);
467 $new_h = imagesy($src_img);
469 // If requested width is more then the actual image width,
470 // do not generate a thumbnail
472 if($width >= $new_w) {
473 imagedestroy($src_img);
477 /* calculates aspect ratio */
478 $aspect_ratio = $new_h / $new_w;
482 $new_h = abs($new_w * $aspect_ratio);
484 /* creates new image of that size */
485 $dst_img = imagecreatetruecolor($new_w,$new_h);
487 imagefill($dst_img, 0, 0, ImageColorAllocate($dst_img, 255, 255, 255));
489 /* copies resized portion of original image into new image */
490 imagecopyresampled($dst_img,$src_img,0,0,0,0,$new_w,$new_h,imagesx($src_img),imagesy($src_img));
492 /* needs the image to be flipped horizontal? */
496 for($x = 0; $x < $new_w; $x++) {
497 imagecopy($dst_img, $image, $x, 0, $w - $x - 1, 0, 1, $h);
503 $dst_img = $this->rotateImage($dst_img, $rotate);
506 /* write down new generated file */
508 if(!file_exists(dirname($image) ."/thumbs"))
509 mkdir(dirname($image) ."/thumbs");
511 $newfile = dirname($image) ."/thumbs/". $width ."_". basename($image);
512 imagejpeg($dst_img, $newfile, 75);
515 imagedestroy($dst_img);
516 imagedestroy($src_img);
519 } // create_thumbnail()
521 public function get_meta_informations($file)
523 return exif_read_data($file);
525 } // get_meta_informations()
527 public function check_config_table()
529 // if the config table doesn't exist yet, create it
530 if(!$this->cfg_db->db_check_table_exists("images")) {
531 $this->cfg_db->db_exec("
532 CREATE TABLE images (
533 img_idx int primary key,
539 } // check_config_table
541 public function gen_thumb($idx = 0, $fromcmd = 0, $force = 0)
543 $details = $this->get_photo_details($idx);
545 $full_path = $this->translate_path($details['directory_path']) ."/". $details['name'];
546 $file_md5 = md5_file($full_path);
548 if($fromcmd) print "Image [". $idx ."] ". $details['name'] ." Thumbnails:";
550 /* if the file hasn't changed there is no need to regen the thumb */
551 if(!$force && $file_md5 == $this->getMD5($idx)) {
552 if($fromcmd) print " file has not changed - skipping\n";
556 /* set the new/changed MD5 sum for the current photo */
557 $this->setMD5($idx, $file_md5);
559 $resolutions = Array(
560 $this->cfg->thumb_width,
561 $this->cfg->bubble_width,
562 $this->cfg->photo_width
565 /* create thumbnails for the requested resolutions */
566 foreach($resolutions as $resolution) {
567 if($fromcmd) print " ". $resolution ."px";
568 $this->create_thumbnail($full_path, $resolution);
571 if($fromcmd) print "\n";
575 private function getMD5($idx)
577 $result = $this->cfg_db->db_query("
580 WHERE img_idx='". $idx ."'
586 $img = $this->cfg_db->db_fetch_object($result);
587 return $img['img_md5'];
591 private function setMD5($idx, $md5)
593 $result = $this->cfg_db->db_exec("
594 REPLACE INTO images (img_idx, img_md5)
595 VALUES ('". $idx ."', '". $md5 ."')
600 public function setTagCondition($mode)
602 $_SESSION['tag_condition'] = $mode;
604 } // setTagCondition()
606 public function startSearch($searchfor)
608 $_SESSION['searchfor'] = $searchfor;
612 private function rotateImage($img, $degrees)
614 if(function_exists("imagerotate"))
615 $img = imagerotate($img, $degrees, 0);
618 function imagerotate($src_img, $angle)
620 $src_x = imagesx($src_img);
621 $src_y = imagesy($src_img);
626 elseif ($src_x <= $src_y) {
630 elseif ($src_x >= $src_y) {
635 $rotate=imagecreatetruecolor($dest_x,$dest_y);
636 imagealphablending($rotate, false);
641 for ($y = 0; $y < ($src_y); $y++) {
642 for ($x = 0; $x < ($src_x); $x++) {
643 $color = imagecolorat($src_img, $x, $y);
644 imagesetpixel($rotate, $dest_x - $y - 1, $x, $color);
650 for ($y = 0; $y < ($src_y); $y++) {
651 for ($x = 0; $x < ($src_x); $x++) {
652 $color = imagecolorat($src_img, $x, $y);
653 imagesetpixel($rotate, $y, $dest_y - $x - 1, $color);
659 for ($y = 0; $y < ($src_y); $y++) {
660 for ($x = 0; $x < ($src_x); $x++) {
661 $color = imagecolorat($src_img, $x, $y);
662 imagesetpixel($rotate, $dest_x - $x - 1, $dest_y - $y - 1, $color);
667 default: $rotate = $src_img;
674 $img = imagerotate($img, $degrees);
682 private function get_photo_tags($idx)
684 $result = $this->db->db_query("
687 INNER JOIN photo_tags pt
689 WHERE pt.photo_id='". $idx ."'
694 while($row = $this->db->db_fetch_object($result))
695 $tags[$row['id']] = $row['name'];
699 } // get_photo_tags()