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('width', $info[0]);
126 $this->tmpl->assign('height', $info[1]);
127 $this->tmpl->assign('c_date', $meta['DateTime']);
128 $this->tmpl->assign('madewith', $meta['Make'] ." ". $meta['Model']);
129 $this->tmpl->assign('image_name', $details['name']);
130 $this->tmpl->assign('image_url', 'phpfspot_img.php?idx='. $photo ."&width=". $this->cfg->photo_width);
131 $this->tmpl->assign('image_url_full', 'phpfspot_img.php?idx='. $photo);
134 $this->tmpl->assign('previous_url', "javascript:showImage(". $previous_img .");");
138 $this->tmpl->assign('next_url', "javascript:showImage(". $next_img .");");
141 $this->tmpl->show("single_photo.tpl");
145 public function getAvailableTags()
147 $result = $this->db->db_query("
148 SELECT tag_id as id, count(tag_id) as quantity
156 while($row = $this->db->db_fetch_object($result)) {
157 $tags[$row['id']] = $row['quantity'];
160 // change these font sizes if you will
161 $max_size = 125; // max font size in %
162 $min_size = 75; // min font size in %
164 // get the largest and smallest array values
165 $max_qty = max(array_values($tags));
166 $min_qty = min(array_values($tags));
168 // find the range of values
169 $spread = $max_qty - $min_qty;
170 if (0 == $spread) { // we don't want to divide by zero
174 // determine the font-size increment
175 // this is the increase per tag quantity (times used)
176 $step = ($max_size - $min_size)/($spread);
178 // loop through our tag array
179 foreach ($tags as $key => $value) {
181 if(isset($_SESSION['selected_tags']) && in_array($key, $_SESSION['selected_tags']))
184 // calculate CSS font-size
185 // find the $value in excess of $min_qty
186 // multiply by the font-size increment ($size)
187 // and add the $min_size set above
188 $size = $min_size + (($value - $min_qty) * $step);
189 // uncomment if you want sizes in whole %:
190 // $size = ceil($size);
192 print "<a href=\"javascript:Tags('add', ". $key .");\" class=\"tag\" style=\"font-size: ". $size ."%;\">". $this->tags[$key] ."</a> ";
196 } // getAvailableTags()
198 public function getSelectedTags()
200 foreach($this->avail_tags as $tag)
202 // return all selected tags
203 if(isset($_SESSION['selected_tags']) && in_array($tag, $_SESSION['selected_tags'])) {
204 print "<a href=\"javascript:Tags('del', ". $tag .");\" class=\"tag\">". $this->tags[$tag] ."</a> ";
209 } // getSelectedTags()
211 public function addTag($tag)
213 if(!isset($_SESSION['selected_tags']))
214 $_SESSION['selected_tags'] = Array();
216 array_push($_SESSION['selected_tags'], $tag);
220 public function delTag($tag)
222 if(isset($_SESSION['selected_tags'])) {
223 $key = array_search($tag, $_SESSION['selected_tags']);
224 unset($_SESSION['selected_tags'][$key]);
225 sort($_SESSION['selected_tags']);
230 public function resetTags()
232 unset($_SESSION['selected_tags']);
236 public function getPhotoSelection()
238 $tagged_photos = Array();
240 /* return a search result */
241 if(isset($_SESSION['searchfor']) && $_SESSION['searchfor'] != '') {
242 $result = $this->db->db_query("
243 SELECT DISTINCT photo_id
249 WHERE t.name LIKE '%". $_SESSION['searchfor'] ."%'
252 while($row = $this->db->db_fetch_object($result)) {
253 array_push($tagged_photos, $row['photo_id']);
255 return $tagged_photos;
258 /* return according the selected tags */
259 if(isset($_SESSION['selected_tags'])) {
261 foreach($_SESSION['selected_tags'] as $tag)
262 $selected.= $tag .",";
263 $selected = substr($selected, 0, strlen($selected)-1);
265 if($_SESSION['tag_condition'] == 'or') {
266 $result = $this->db->db_query("
267 SELECT DISTINCT photo_id
271 WHERE pt.tag_id IN (". $selected .")
275 elseif($_SESSION['tag_condition'] == 'and') {
277 /* Join together a table looking like
279 pt1.photo_id pt1.tag_id pt2.photo_id pt2.tag_id ...
281 so the query can quickly return all images matching the
282 selected tags in an AND condition
287 SELECT DISTINCT pt1.photo_id
291 for($i = 0; $i < count($_SESSION['selected_tags']); $i++) {
293 INNER JOIN photo_tags pt". ($i+2) ."
294 ON pt1.photo_id=pt". ($i+2) .".photo_id
297 $query_str.= "WHERE pt1.tag_id=". $_SESSION['selected_tags'][0];
298 for($i = 1; $i < count($_SESSION['selected_tags']); $i++) {
300 AND pt". ($i+1) .".tag_id=". $_SESSION['selected_tags'][$i] ."
303 $result = $this->db->db_query($query_str);
306 while($row = $this->db->db_fetch_object($result)) {
307 array_push($tagged_photos, $row['photo_id']);
309 return $tagged_photos;
312 /* return all available photos */
313 $result = $this->db->db_query("
314 SELECT DISTINCT photo_id
320 while($row = $this->db->db_fetch_object($result)) {
321 array_push($tagged_photos, $row['photo_id']);
323 return $tagged_photos;
325 } // getPhotoSelection()
327 public function showPhotoIndex()
329 $photos = $this->getPhotoSelection();
331 $count = count($photos);
335 $images[$rows] = Array();
337 for($i = 0; $i < $count; $i++) {
339 $images[$rows][$cols] = $photos[$i];
341 if($cols == $this->cfg->thumbs_per_row-1) {
344 $images[$rows] = Array();
351 // +1 for for smarty's selection iteration
354 if(isset($_SESSION['searchfor']) && $_SESSION['searchfor'] != '')
355 $this->tmpl->assign('searchfor', $_SESSION['searchfor']);
357 $this->tmpl->assign('count', $count);
358 $this->tmpl->assign('width', $this->cfg->thumb_width);
359 $this->tmpl->assign('images', $images);
360 $this->tmpl->assign('rows', $rows);
361 $this->tmpl->assign('columns', $this->cfg->thumbs_per_row);
362 $this->tmpl->show("photo_index.tpl");
365 } // showPhotoIndex()
367 public function showBubbleDetails($photo, $direction)
369 if($direction == "up")
370 $direction = "bubbleimg_up";
372 $direction = "bubbleimg_down";
374 $details = $this->get_photo_details($photo);
376 $image_url = "phpfspot_img.php?idx=". $photo ."&width=". $this->cfg->bubble_width;
378 $filesize = filesize($this->translate_path($details['directory_path']) ."/". $details['name']);
379 $filesize = rand($filesize/1024, 2);
381 $img = getimagesize($this->translate_path($details['directory_path']) ."/". $details['name']);
383 $this->tmpl->assign('file_size', $filesize);
384 $this->tmpl->assign('width', $img[0]);
385 $this->tmpl->assign('height', $img[1]);
386 $this->tmpl->assign('file_name', $details['name']);
387 $this->tmpl->assign('image_id', $direction);
388 $this->tmpl->assign('image_url', $image_url);
389 $this->tmpl->show("bubble_details.tpl");
391 } // showBubbleDetails()
393 public function showCredits()
395 $this->tmpl->assign('version', $this->cfg->version);
396 $this->tmpl->assign('product', $this->cfg->product);
397 $this->tmpl->show("credits.tpl");
401 public function create_thumbnail($image, $width)
403 $meta = $this->get_meta_informations($image);
408 switch($meta['Orientation']) {
451 $src_img = @imagecreatefromjpeg($image);
455 /* grabs the height and width */
456 $new_w = imagesx($src_img);
457 $new_h = imagesy($src_img);
459 // If requested width is more then the actual image width,
460 // do not generate a thumbnail
462 if($width >= $new_w) {
463 imagedestroy($src_img);
467 /* calculates aspect ratio */
468 $aspect_ratio = $new_h / $new_w;
472 $new_h = abs($new_w * $aspect_ratio);
474 /* creates new image of that size */
475 $dst_img = imagecreatetruecolor($new_w,$new_h);
477 imagefill($dst_img, 0, 0, ImageColorAllocate($dst_img, 255, 255, 255));
479 /* copies resized portion of original image into new image */
480 imagecopyresampled($dst_img,$src_img,0,0,0,0,$new_w,$new_h,imagesx($src_img),imagesy($src_img));
482 /* needs the image to be flipped horizontal? */
486 for($x = 0; $x < $new_w; $x++) {
487 imagecopy($dst_img, $image, $x, 0, $w - $x - 1, 0, 1, $h);
493 $dst_img = $this->rotateImage($dst_img, $rotate);
496 /* write down new generated file */
498 if(!file_exists(dirname($image) ."/thumbs"))
499 mkdir(dirname($image) ."/thumbs");
501 $newfile = dirname($image) ."/thumbs/". $width ."_". basename($image);
502 imagejpeg($dst_img, $newfile, 75);
505 imagedestroy($dst_img);
506 imagedestroy($src_img);
509 } // create_thumbnail()
511 public function get_meta_informations($file)
513 return exif_read_data($file);
515 } // get_meta_informations()
517 public function check_config_table()
519 // if the config table doesn't exist yet, create it
520 if(!$this->cfg_db->db_check_table_exists("images")) {
521 $this->cfg_db->db_exec("
522 CREATE TABLE images (
523 img_idx int primary key,
529 } // check_config_table
531 public function gen_thumbs($idx = 0, $fromcmd = 0)
534 /* get all available photos */
535 $all = $this->getPhotoSelection();
540 foreach($all as $photo) {
542 $details = $this->get_photo_details($photo);
544 $full_path = $this->translate_path($details['directory_path']) ."/". $details['name'];
545 $file_md5 = md5_file($full_path);
547 if($fromcmd) print "Image ". $details['name'] ." Thumbnails:";
549 /* if the file hasn't changed there is no need to regen the thumb */
550 if($file_md5 == $this->getMD5($photo)) {
551 if($fromcmd) print " file has not changed - skipping\n";
555 /* set the new/changed MD5 sum for the current photo */
556 $this->setMD5($photo, $file_md5);
558 $resolutions = Array(
559 $this->cfg->thumb_width,
560 $this->cfg->bubble_width,
561 $this->cfg->photo_width
564 /* create thumbnails for the requested resolutions */
565 foreach($resolutions as $resolution) {
566 if($fromcmd) print " ". $resolution ."px";
567 $this->create_thumbnail($full_path, $resolution);
570 if($fromcmd) print "\n";
576 private function getMD5($idx)
578 $result = $this->cfg_db->db_query("
581 WHERE img_idx='". $idx ."'
587 $img = $this->cfg_db->db_fetch_object($result);
588 return $img['img_md5'];
592 private function setMD5($idx, $md5)
594 $result = $this->cfg_db->db_exec("
595 REPLACE INTO images (img_idx, img_md5)
596 VALUES ('". $idx ."', '". $md5 ."')
601 public function setTagCondition($mode)
603 $_SESSION['tag_condition'] = $mode;
605 } // setTagCondition()
607 public function startSearch($searchfor)
609 $_SESSION['searchfor'] = $searchfor;
613 private function rotateImage($img, $degrees)
615 if(function_exists("imagerotate"))
616 $img = imagerotate($img, $degrees, 0);
619 function imagerotate($src_img, $angle)
621 $src_x = imagesx($src_img);
622 $src_y = imagesy($src_img);
627 elseif ($src_x <= $src_y) {
631 elseif ($src_x >= $src_y) {
636 $rotate=imagecreatetruecolor($dest_x,$dest_y);
637 imagealphablending($rotate, false);
642 for ($y = 0; $y < ($src_y); $y++) {
643 for ($x = 0; $x < ($src_x); $x++) {
644 $color = imagecolorat($src_img, $x, $y);
645 imagesetpixel($rotate, $dest_x - $y - 1, $x, $color);
651 for ($y = 0; $y < ($src_y); $y++) {
652 for ($x = 0; $x < ($src_x); $x++) {
653 $color = imagecolorat($src_img, $x, $y);
654 imagesetpixel($rotate, $y, $dest_y - $x - 1, $color);
660 for ($y = 0; $y < ($src_y); $y++) {
661 for ($x = 0; $x < ($src_x); $x++) {
662 $color = imagecolorat($src_img, $x, $y);
663 imagesetpixel($rotate, $dest_x - $x - 1, $dest_y - $y - 1, $color);
668 default: $rotate = $src_img;
675 $img = imagerotate($img, $degrees);