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
163 while($row = $this->db->db_fetch_object($result)) {
164 $tags[$row['id']] = $row['quantity'];
167 // change these font sizes if you will
168 $max_size = 125; // max font size in %
169 $min_size = 75; // min font size in %
171 // get the largest and smallest array values
172 $max_qty = max(array_values($tags));
173 $min_qty = min(array_values($tags));
175 // find the range of values
176 $spread = $max_qty - $min_qty;
177 if (0 == $spread) { // we don't want to divide by zero
181 // determine the font-size increment
182 // this is the increase per tag quantity (times used)
183 $step = ($max_size - $min_size)/($spread);
185 // loop through our tag array
186 foreach ($tags as $key => $value) {
188 if(isset($_SESSION['selected_tags']) && in_array($key, $_SESSION['selected_tags']))
191 // calculate CSS font-size
192 // find the $value in excess of $min_qty
193 // multiply by the font-size increment ($size)
194 // and add the $min_size set above
195 $size = $min_size + (($value - $min_qty) * $step);
196 // uncomment if you want sizes in whole %:
197 // $size = ceil($size);
199 print "<a href=\"javascript:Tags('add', ". $key .");\" class=\"tag\" style=\"font-size: ". $size ."%;\">". $this->tags[$key] ."</a> ";
203 } // getAvailableTags()
205 public function getSelectedTags()
207 foreach($this->avail_tags as $tag)
209 // return all selected tags
210 if(isset($_SESSION['selected_tags']) && in_array($tag, $_SESSION['selected_tags'])) {
211 print "<a href=\"javascript:Tags('del', ". $tag .");\" class=\"tag\">". $this->tags[$tag] ."</a> ";
216 } // getSelectedTags()
218 public function addTag($tag)
220 if(!isset($_SESSION['selected_tags']))
221 $_SESSION['selected_tags'] = Array();
223 if(!in_array($tag, $_SESSION['selected_tags']))
224 array_push($_SESSION['selected_tags'], $tag);
228 public function delTag($tag)
230 if(isset($_SESSION['selected_tags'])) {
231 $key = array_search($tag, $_SESSION['selected_tags']);
232 unset($_SESSION['selected_tags'][$key]);
233 sort($_SESSION['selected_tags']);
238 public function resetTags()
240 unset($_SESSION['selected_tags']);
244 public function getPhotoSelection()
246 $tagged_photos = Array();
248 /* return a search result */
249 if(isset($_SESSION['searchfor']) && $_SESSION['searchfor'] != '') {
250 $result = $this->db->db_query("
251 SELECT DISTINCT photo_id
257 WHERE t.name LIKE '%". $_SESSION['searchfor'] ."%'
260 while($row = $this->db->db_fetch_object($result)) {
261 array_push($tagged_photos, $row['photo_id']);
263 return $tagged_photos;
266 /* return according the selected tags */
267 if(isset($_SESSION['selected_tags'])) {
269 foreach($_SESSION['selected_tags'] as $tag)
270 $selected.= $tag .",";
271 $selected = substr($selected, 0, strlen($selected)-1);
273 if($_SESSION['tag_condition'] == 'or') {
274 $result = $this->db->db_query("
275 SELECT DISTINCT photo_id
279 WHERE pt.tag_id IN (". $selected .")
283 elseif($_SESSION['tag_condition'] == 'and') {
285 /* Join together a table looking like
287 pt1.photo_id pt1.tag_id pt2.photo_id pt2.tag_id ...
289 so the query can quickly return all images matching the
290 selected tags in an AND condition
295 SELECT DISTINCT pt1.photo_id
299 for($i = 0; $i < count($_SESSION['selected_tags']); $i++) {
301 INNER JOIN photo_tags pt". ($i+2) ."
302 ON pt1.photo_id=pt". ($i+2) .".photo_id
305 $query_str.= "WHERE pt1.tag_id=". $_SESSION['selected_tags'][0];
306 for($i = 1; $i < count($_SESSION['selected_tags']); $i++) {
308 AND pt". ($i+1) .".tag_id=". $_SESSION['selected_tags'][$i] ."
311 $result = $this->db->db_query($query_str);
314 while($row = $this->db->db_fetch_object($result)) {
315 array_push($tagged_photos, $row['photo_id']);
317 return $tagged_photos;
320 /* return all available photos */
321 $result = $this->db->db_query("
322 SELECT DISTINCT photo_id
328 while($row = $this->db->db_fetch_object($result)) {
329 array_push($tagged_photos, $row['photo_id']);
331 return $tagged_photos;
333 } // getPhotoSelection()
335 public function showPhotoIndex()
337 $photos = $this->getPhotoSelection();
339 $count = count($photos);
343 $images[$rows] = Array();
345 for($i = 0; $i < $count; $i++) {
347 $images[$rows][$cols] = $photos[$i];
349 if($cols == $this->cfg->thumbs_per_row-1) {
352 $images[$rows] = Array();
359 // +1 for for smarty's selection iteration
362 if(isset($_SESSION['searchfor']) && $_SESSION['searchfor'] != '')
363 $this->tmpl->assign('searchfor', $_SESSION['searchfor']);
365 $this->tmpl->assign('count', $count);
366 $this->tmpl->assign('width', $this->cfg->thumb_width);
367 $this->tmpl->assign('images', $images);
368 $this->tmpl->assign('rows', $rows);
369 $this->tmpl->assign('columns', $this->cfg->thumbs_per_row);
370 $this->tmpl->show("photo_index.tpl");
373 } // showPhotoIndex()
375 public function showBubbleDetails($photo, $direction)
377 if($direction == "up")
378 $direction = "bubbleimg_up";
380 $direction = "bubbleimg_down";
382 $details = $this->get_photo_details($photo);
384 $image_url = "phpfspot_img.php?idx=". $photo ."&width=". $this->cfg->bubble_width;
386 $filesize = filesize($this->translate_path($details['directory_path']) ."/". $details['name']);
387 $filesize = rand($filesize/1024, 2);
389 $img = getimagesize($this->translate_path($details['directory_path']) ."/". $details['name']);
391 $this->tmpl->assign('file_size', $filesize);
392 $this->tmpl->assign('width', $img[0]);
393 $this->tmpl->assign('height', $img[1]);
394 $this->tmpl->assign('file_name', $details['name']);
395 $this->tmpl->assign('image_id', $direction);
396 $this->tmpl->assign('image_url', $image_url);
397 $this->tmpl->show("bubble_details.tpl");
399 } // showBubbleDetails()
401 public function showCredits()
403 $this->tmpl->assign('version', $this->cfg->version);
404 $this->tmpl->assign('product', $this->cfg->product);
405 $this->tmpl->show("credits.tpl");
409 public function create_thumbnail($image, $width)
411 $meta = $this->get_meta_informations($image);
416 switch($meta['Orientation']) {
459 $src_img = @imagecreatefromjpeg($image);
463 /* grabs the height and width */
464 $new_w = imagesx($src_img);
465 $new_h = imagesy($src_img);
467 // If requested width is more then the actual image width,
468 // do not generate a thumbnail
470 if($width >= $new_w) {
471 imagedestroy($src_img);
475 /* calculates aspect ratio */
476 $aspect_ratio = $new_h / $new_w;
480 $new_h = abs($new_w * $aspect_ratio);
482 /* creates new image of that size */
483 $dst_img = imagecreatetruecolor($new_w,$new_h);
485 imagefill($dst_img, 0, 0, ImageColorAllocate($dst_img, 255, 255, 255));
487 /* copies resized portion of original image into new image */
488 imagecopyresampled($dst_img,$src_img,0,0,0,0,$new_w,$new_h,imagesx($src_img),imagesy($src_img));
490 /* needs the image to be flipped horizontal? */
494 for($x = 0; $x < $new_w; $x++) {
495 imagecopy($dst_img, $image, $x, 0, $w - $x - 1, 0, 1, $h);
501 $dst_img = $this->rotateImage($dst_img, $rotate);
504 /* write down new generated file */
506 if(!file_exists(dirname($image) ."/thumbs"))
507 mkdir(dirname($image) ."/thumbs");
509 $newfile = dirname($image) ."/thumbs/". $width ."_". basename($image);
510 imagejpeg($dst_img, $newfile, 75);
513 imagedestroy($dst_img);
514 imagedestroy($src_img);
517 } // create_thumbnail()
519 public function get_meta_informations($file)
521 return exif_read_data($file);
523 } // get_meta_informations()
525 public function check_config_table()
527 // if the config table doesn't exist yet, create it
528 if(!$this->cfg_db->db_check_table_exists("images")) {
529 $this->cfg_db->db_exec("
530 CREATE TABLE images (
531 img_idx int primary key,
537 } // check_config_table
539 public function gen_thumbs($idx = 0, $fromcmd = 0)
542 /* get all available photos */
543 $all = $this->getPhotoSelection();
548 foreach($all as $photo) {
550 $details = $this->get_photo_details($photo);
552 $full_path = $this->translate_path($details['directory_path']) ."/". $details['name'];
553 $file_md5 = md5_file($full_path);
555 if($fromcmd) print "Image ". $details['name'] ." Thumbnails:";
557 /* if the file hasn't changed there is no need to regen the thumb */
558 if($file_md5 == $this->getMD5($photo)) {
559 if($fromcmd) print " file has not changed - skipping\n";
563 /* set the new/changed MD5 sum for the current photo */
564 $this->setMD5($photo, $file_md5);
566 $resolutions = Array(
567 $this->cfg->thumb_width,
568 $this->cfg->bubble_width,
569 $this->cfg->photo_width
572 /* create thumbnails for the requested resolutions */
573 foreach($resolutions as $resolution) {
574 if($fromcmd) print " ". $resolution ."px";
575 $this->create_thumbnail($full_path, $resolution);
578 if($fromcmd) print "\n";
584 private function getMD5($idx)
586 $result = $this->cfg_db->db_query("
589 WHERE img_idx='". $idx ."'
595 $img = $this->cfg_db->db_fetch_object($result);
596 return $img['img_md5'];
600 private function setMD5($idx, $md5)
602 $result = $this->cfg_db->db_exec("
603 REPLACE INTO images (img_idx, img_md5)
604 VALUES ('". $idx ."', '". $md5 ."')
609 public function setTagCondition($mode)
611 $_SESSION['tag_condition'] = $mode;
613 } // setTagCondition()
615 public function startSearch($searchfor)
617 $_SESSION['searchfor'] = $searchfor;
621 private function rotateImage($img, $degrees)
623 if(function_exists("imagerotate"))
624 $img = imagerotate($img, $degrees, 0);
627 function imagerotate($src_img, $angle)
629 $src_x = imagesx($src_img);
630 $src_y = imagesy($src_img);
635 elseif ($src_x <= $src_y) {
639 elseif ($src_x >= $src_y) {
644 $rotate=imagecreatetruecolor($dest_x,$dest_y);
645 imagealphablending($rotate, false);
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, $dest_x - $y - 1, $x, $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, $y, $dest_y - $x - 1, $color);
668 for ($y = 0; $y < ($src_y); $y++) {
669 for ($x = 0; $x < ($src_x); $x++) {
670 $color = imagecolorat($src_img, $x, $y);
671 imagesetpixel($rotate, $dest_x - $x - 1, $dest_y - $y - 1, $color);
676 default: $rotate = $src_img;
683 $img = imagerotate($img, $degrees);
691 private function get_photo_tags($idx)
693 $result = $this->db->db_query("
696 INNER JOIN photo_tags pt
698 WHERE pt.photo_id='". $idx ."'
703 while($row = $this->db->db_fetch_object($result))
704 $tags[$row['id']] = $row['name'];
708 } // get_photo_tags()