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 foreach($this->avail_tags as $tag)
149 if(isset($_SESSION['selected_tags']) && in_array($tag, $_SESSION['selected_tags']))
152 // return all available (= not selected) tags
153 print "<a href=\"javascript:Tags('add', ". $tag .");\" class=\"tag\">". $this->tags[$tag] ."</a> ";
157 } // getAvailableTags()
159 public function getSelectedTags()
161 foreach($this->avail_tags as $tag)
163 // return all selected tags
164 if(isset($_SESSION['selected_tags']) && in_array($tag, $_SESSION['selected_tags'])) {
165 print "<a href=\"javascript:Tags('del', ". $tag .");\" class=\"tag\">". $this->tags[$tag] ."</a> ";
170 } // getSelectedTags()
172 public function addTag($tag)
174 if(!isset($_SESSION['selected_tags']))
175 $_SESSION['selected_tags'] = Array();
177 array_push($_SESSION['selected_tags'], $tag);
181 public function delTag($tag)
183 if(isset($_SESSION['selected_tags'])) {
184 $key = array_search($tag, $_SESSION['selected_tags']);
185 unset($_SESSION['selected_tags'][$key]);
186 sort($_SESSION['selected_tags']);
191 public function resetTags()
193 unset($_SESSION['selected_tags']);
197 public function getPhotoSelection()
199 $tagged_photos = Array();
201 /* return a search result */
202 if(isset($_SESSION['searchfor']) && $_SESSION['searchfor'] != '') {
203 $result = $this->db->db_query("
204 SELECT DISTINCT photo_id
210 WHERE t.name LIKE '%". $_SESSION['searchfor'] ."%'
213 while($row = $this->db->db_fetch_object($result)) {
214 array_push($tagged_photos, $row['photo_id']);
216 return $tagged_photos;
219 /* return according the selected tags */
220 if(isset($_SESSION['selected_tags'])) {
222 foreach($_SESSION['selected_tags'] as $tag)
223 $selected.= $tag .",";
224 $selected = substr($selected, 0, strlen($selected)-1);
226 if($_SESSION['tag_condition'] == 'or') {
227 $result = $this->db->db_query("
228 SELECT DISTINCT photo_id
232 WHERE pt.tag_id IN (". $selected .")
235 while($row = $this->db->db_fetch_object($result)) {
236 array_push($tagged_photos, $row['photo_id']);
239 elseif($_SESSION['tag_condition'] == 'and') {
240 $result = $this->db->db_query("
241 SELECT DISTINCT photo_id, tag_id
245 WHERE pt.tag_id IN (". $selected .")
249 /* now we need to check if each object fulfills the condition
250 and has all the selected tags assigned
252 $match_object = Array();
253 $matches_needed = count($_SESSION['selected_tags']);
254 while($row = $this->db->db_fetch_object($result)) {
255 /* set the counter for this object */
256 if(!isset($match_object[$row['photo_id']]))
257 $match_object[$row['photo_id']] = $matches_needed;
259 /* we have a match? decrement the counter */
260 if(in_array($row['tag_id'], $_SESSION['selected_tags']))
261 $match_object[$row['photo_id']]--;
263 /* if the object has all necessary tags, add it to the result */
264 if($match_object[$row['photo_id']] == 0)
265 array_push($tagged_photos, $row['photo_id']);
268 return $tagged_photos;
271 /* return all available photos */
272 $result = $this->db->db_query("
273 SELECT DISTINCT photo_id
279 while($row = $this->db->db_fetch_object($result)) {
280 array_push($tagged_photos, $row['photo_id']);
282 return $tagged_photos;
284 } // getPhotoSelection()
286 public function showPhotoIndex()
288 $photos = $this->getPhotoSelection();
290 $count = count($photos);
294 $images[$rows] = Array();
296 for($i = 0; $i < $count; $i++) {
298 $images[$rows][$cols] = $photos[$i];
300 if($cols == $this->cfg->thumbs_per_row-1) {
303 $images[$rows] = Array();
310 // +1 for for smarty's selection iteration
313 if(isset($_SESSION['searchfor']) && $_SESSION['searchfor'] != '')
314 $this->tmpl->assign('searchfor', $_SESSION['searchfor']);
316 $this->tmpl->assign('count', $count);
317 $this->tmpl->assign('width', $this->cfg->thumb_width);
318 $this->tmpl->assign('images', $images);
319 $this->tmpl->assign('rows', $rows);
320 $this->tmpl->assign('columns', $this->cfg->thumbs_per_row);
321 $this->tmpl->show("photo_index.tpl");
324 } // showPhotoIndex()
326 public function showBubbleDetails($photo, $direction)
328 if($direction == "up")
329 $direction = "bubbleimg_up";
331 $direction = "bubbleimg_down";
333 $details = $this->get_photo_details($photo);
335 $image_url = "phpfspot_img.php?idx=". $photo ."&width=". $this->cfg->bubble_width;
337 $filesize = filesize($this->translate_path($details['directory_path']) ."/". $details['name']);
338 $filesize = rand($filesize/1024, 2);
340 $img = getimagesize($this->translate_path($details['directory_path']) ."/". $details['name']);
342 $this->tmpl->assign('file_size', $filesize);
343 $this->tmpl->assign('width', $img[0]);
344 $this->tmpl->assign('height', $img[1]);
345 $this->tmpl->assign('file_name', $details['name']);
346 $this->tmpl->assign('image_id', $direction);
347 $this->tmpl->assign('image_url', $image_url);
348 $this->tmpl->show("bubble_details.tpl");
350 } // showBubbleDetails()
352 public function showCredits()
354 $this->tmpl->assign('version', $this->cfg->version);
355 $this->tmpl->assign('product', $this->cfg->product);
356 $this->tmpl->show("credits.tpl");
360 public function create_thumbnail($image, $width)
362 // if thumbnail already exists, don't recreate it
363 if(file_exists(dirname($image) ."/thumbs/". $width ."_". basename($image)))
366 $meta = $this->get_meta_informations($image);
371 switch($meta['Orientation']) {
414 $src_img = @imagecreatefromjpeg($image);
418 /* grabs the height and width */
419 $new_w = imagesx($src_img);
420 $new_h = imagesy($src_img);
422 // If requested width is more then the actual image width,
423 // do not generate a thumbnail
425 if($width >= $new_w) {
426 imagedestroy($src_img);
430 /* calculates aspect ratio */
431 $aspect_ratio = $new_h / $new_w;
435 $new_h = abs($new_w * $aspect_ratio);
437 /* creates new image of that size */
438 $dst_img = imagecreatetruecolor($new_w,$new_h);
440 imagefill($dst_img, 0, 0, ImageColorAllocate($dst_img, 255, 255, 255));
442 /* copies resized portion of original image into new image */
443 imagecopyresampled($dst_img,$src_img,0,0,0,0,$new_w,$new_h,imagesx($src_img),imagesy($src_img));
445 /* needs the image to be flipped horizontal? */
449 for($x = 0; $x < $new_w; $x++) {
450 imagecopy($dst_img, $image, $x, 0, $w - $x - 1, 0, 1, $h);
456 $dst_img = $this->rotateImage($dst_img, $rotate);
459 /* write down new generated file */
461 if(!file_exists(dirname($image) ."/thumbs"))
462 mkdir(dirname($image) ."/thumbs");
464 $newfile = dirname($image) ."/thumbs/". $width ."_". basename($image);
465 imagejpeg($dst_img, $newfile, 75);
468 imagedestroy($dst_img);
469 imagedestroy($src_img);
472 } // create_thumbnail()
474 public function get_meta_informations($file)
476 return exif_read_data($file);
478 } // get_meta_informations()
480 public function check_config_table()
482 // if the config table doesn't exist yet, create it
483 if(!$this->cfg_db->db_check_table_exists("images")) {
484 $this->cfg_db->db_exec("
485 CREATE TABLE images (
486 img_idx int primary key,
492 } // check_config_table
494 public function gen_thumbs($idx = 0, $fromcmd = 0)
497 /* get all available photos */
498 $all = $this->getPhotoSelection();
503 foreach($all as $photo) {
505 $details = $this->get_photo_details($photo);
507 $full_path = $this->translate_path($details['directory_path']) ."/". $details['name'];
508 $file_md5 = md5_file($full_path);
510 if($fromcmd) print "Image ". $details['name'] ." Thumbnails:";
512 /* if the file hasn't changed there is no need to regen the thumb */
513 if($file_md5 == $this->getMD5($photo)) {
514 if($fromcmd) print " file has not changed - skipping\n";
518 /* set the new/changed MD5 sum for the current photo */
519 $this->setMD5($photo, $file_md5);
521 $resolutions = Array(
522 $this->cfg->thumb_width,
523 $this->cfg->bubble_width,
524 $this->cfg->photo_width
527 /* create thumbnails for the requested resolutions */
528 foreach($resolutions as $resolution) {
529 if($fromcmd) print " ". $resolution ."px";
530 $this->create_thumbnail($full_path, $resolution);
533 if($fromcmd) print "\n";
539 private function getMD5($idx)
541 $result = $this->cfg_db->db_query("
544 WHERE img_idx='". $idx ."'
550 $img = $this->cfg_db->db_fetch_object($result);
551 return $img['img_md5'];
555 private function setMD5($idx, $md5)
557 $result = $this->cfg_db->db_exec("
558 REPLACE INTO images (img_idx, img_md5)
559 VALUES ('". $idx ."', '". $md5 ."')
564 public function setTagCondition($mode)
566 $_SESSION['tag_condition'] = $mode;
568 } // setTagCondition()
570 public function startSearch($searchfor)
572 $_SESSION['searchfor'] = $searchfor;
576 private function rotateImage($img, $degrees)
578 if(function_exists("imagerotate"))
579 $img = imagerotate($img, $degrees, 0);
582 function imagerotate($src_img, $angle)
584 $src_x = imagesx($src_img);
585 $src_y = imagesy($src_img);
590 elseif ($src_x <= $src_y) {
594 elseif ($src_x >= $src_y) {
599 $rotate=imagecreatetruecolor($dest_x,$dest_y);
600 imagealphablending($rotate, false);
605 for ($y = 0; $y < ($src_y); $y++) {
606 for ($x = 0; $x < ($src_x); $x++) {
607 $color = imagecolorat($src_img, $x, $y);
608 imagesetpixel($rotate, $dest_x - $y - 1, $x, $color);
614 for ($y = 0; $y < ($src_y); $y++) {
615 for ($x = 0; $x < ($src_x); $x++) {
616 $color = imagecolorat($src_img, $x, $y);
617 imagesetpixel($rotate, $y, $dest_y - $x - 1, $color);
623 for ($y = 0; $y < ($src_y); $y++) {
624 for ($x = 0; $x < ($src_x); $x++) {
625 $color = imagecolorat($src_img, $x, $y);
626 imagesetpixel($rotate, $dest_x - $x - 1, $dest_y - $y - 1, $color);
631 default: $rotate = $src_img;
638 $img = imagerotate($img, $degrees);