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';
37 public function __destruct()
42 public function show()
44 $this->tmpl->assign('page_title', $this->cfg->page_title);
45 $this->tmpl->assign('current_condition', $_SESSION['tag_condition']);
46 $this->tmpl->show("index.tpl");
50 private function get_tags()
52 $this->avail_tags = Array();
55 $result = $this->db->db_query("
58 ORDER BY sort_priority ASC
61 while($row = $this->db->db_fetch_object($result)) {
64 $tag_name = $row['name'];
66 /* check if config requests to ignore this tag */
67 if(in_array($row['name'], $this->cfg->hide_tags))
70 $this->tags[$tag_id] = $tag_name;
71 $this->avail_tags[$count] = $tag_id;
79 public function get_photo_details($idx)
81 $result = $this->db->db_query("
87 return $this->db->db_fetch_object($result);
89 } // get_photo_details
91 public function translate_path($path, $width = 0)
93 return str_replace($this->cfg->path_replace_from, $this->cfg->path_replace_to, $path);
97 public function showPhoto($photo)
99 $all_photos = $this->getAllTagPhotos();
101 foreach($all_photos as $all_photo) {
104 $next_img = $all_photo;
108 if($all_photo == $photo) {
112 $previous_img = $all_photo;
117 $details = $this->get_photo_details($photo);
118 $meta = $this->get_meta_informations($this->translate_path($details['directory_path']) ."/". $details['name']);
119 $info = getimagesize($this->translate_path($details['directory_path']) ."/thumbs/". $this->cfg->photo_width ."_". $details['name']);
121 $this->tmpl->assign('width', $info[0]);
122 $this->tmpl->assign('height', $info[1]);
123 $this->tmpl->assign('c_date', $meta['DateTime']);
124 $this->tmpl->assign('madewith', $meta['Make'] ." ". $meta['Model']);
125 $this->tmpl->assign('image_name', $details['name']);
126 $this->tmpl->assign('image_url', 'phpfspot_img.php?idx='. $photo ."&width=". $this->cfg->photo_width);
127 $this->tmpl->assign('image_url_full', 'phpfspot_img.php?idx='. $photo);
130 $this->tmpl->assign('previous_url', "javascript:showImage(". $previous_img .");");
134 $this->tmpl->assign('next_url', "javascript:showImage(". $next_img .");");
137 $this->tmpl->show("single_photo.tpl");
141 public function getAvailableTags()
143 foreach($this->avail_tags as $tag)
145 if(isset($_SESSION['selected_tags']) && in_array($tag, $_SESSION['selected_tags']))
148 // return all available (= not selected) tags
149 print "<a href=\"javascript:Tags('add', ". $tag .");\">". $this->tags[$tag] ."</a> ";
153 } // getAvailableTags()
155 public function getSelectedTags()
157 foreach($this->avail_tags as $tag)
159 // return all selected tags
160 if(isset($_SESSION['selected_tags']) && in_array($tag, $_SESSION['selected_tags'])) {
161 print "<a href=\"javascript:Tags('del', ". $tag .");\">". $this->tags[$tag] ."</a> ";
166 } // getSelectedTags()
168 public function addTag($tag)
170 if(!isset($_SESSION['selected_tags']))
171 $_SESSION['selected_tags'] = Array();
173 array_push($_SESSION['selected_tags'], $tag);
177 public function delTag($tag)
179 if(isset($_SESSION['selected_tags'])) {
180 $key = array_search($tag, $_SESSION['selected_tags']);
181 unset($_SESSION['selected_tags'][$key]);
186 public function resetTags()
188 unset($_SESSION['selected_tags']);
192 public function getAllTagPhotos()
194 $tagged_photos = Array();
196 if(isset($_SESSION['selected_tags'])) {
198 foreach($_SESSION['selected_tags'] as $tag)
199 $selected.= $tag .",";
200 $selected = substr($selected, 0, strlen($selected)-1);
202 if($_SESSION['tag_condition'] == 'or') {
203 $result = $this->db->db_query("
204 SELECT DISTINCT photo_id
208 WHERE pt.tag_id IN (". $selected .")
211 while($row = $this->db->db_fetch_object($result)) {
212 array_push($tagged_photos, $row['photo_id']);
215 elseif($_SESSION['tag_condition'] == 'and') {
216 $result = $this->db->db_query("
217 SELECT DISTINCT photo_id, tag_id
221 WHERE pt.tag_id IN (". $selected .")
225 /* now we need to check if each object fulfills the condition
226 and has all the selected tags assigned
228 $match_object = Array();
229 $matches_needed = count($_SESSION['selected_tags']);
230 while($row = $this->db->db_fetch_object($result)) {
231 /* set the counter for this object */
232 if(!isset($match_object[$row['photo_id']]))
233 $match_object[$row['photo_id']] = $matches_needed;
235 /* we have a match? decrement the counter */
236 if(in_array($row['tag_id'], $_SESSION['selected_tags']))
237 $match_object[$row['photo_id']]--;
239 /* if the object has all necessary tags, add it to the result */
240 if($match_object[$row['photo_id']] == 0)
241 array_push($tagged_photos, $row['photo_id']);
246 $result = $this->db->db_query("
247 SELECT DISTINCT photo_id
253 while($row = $this->db->db_fetch_object($result)) {
254 array_push($tagged_photos, $row['photo_id']);
258 return $tagged_photos;
260 } // getAllTagPhotos()
262 public function showPhotoIndex()
264 $photos = $this->getAllTagPhotos();
265 $count = count($photos);
269 $images[$rows] = Array();
271 for($i = 0; $i < $count; $i++) {
273 $images[$rows][$cols] = $photos[$i];
275 if($cols == $this->cfg->thumbs_per_row-1) {
278 $images[$rows] = Array();
285 // +1 for for smarty's selection iteration
288 //$images.= "<img src=\"phpfspot_img.php?idx=". $photo ."&width=". $this->cfg->thumb_width ."\" /><br />\n";
290 $this->tmpl->assign('count', $count);
291 $this->tmpl->assign('width', $this->cfg->thumb_width);
292 $this->tmpl->assign('images', $images);
293 $this->tmpl->assign('rows', $rows);
294 $this->tmpl->assign('columns', $this->cfg->thumbs_per_row);
295 $this->tmpl->show("photo_index.tpl");
298 } // showPhotoIndex()
300 public function showBubbleDetails($photo, $direction)
302 if($direction == "up")
303 $direction = "bubbleimg_up";
305 $direction = "bubbleimg_down";
307 $details = $this->get_photo_details($photo);
309 $image_url = "phpfspot_img.php?idx=". $photo ."&width=". $this->cfg->bubble_width;
311 $filesize = filesize($this->translate_path($details['directory_path']) ."/". $details['name']);
312 $filesize = rand($filesize/1024, 2);
314 $img = getimagesize($this->translate_path($details['directory_path']) ."/". $details['name']);
316 $this->tmpl->assign('file_size', $filesize);
317 $this->tmpl->assign('width', $img[0]);
318 $this->tmpl->assign('height', $img[1]);
319 $this->tmpl->assign('file_name', $details['name']);
320 $this->tmpl->assign('image_id', $direction);
321 $this->tmpl->assign('image_url', $image_url);
322 $this->tmpl->show("bubble_details.tpl");
324 } // showBubbleDetails()
326 public function showCredits()
328 $this->tmpl->assign('version', $this->cfg->version);
329 $this->tmpl->assign('product', $this->cfg->product);
330 $this->tmpl->show("credits.tpl");
334 public function create_thumbnail($image, $width)
336 // if thumbnail already exists, don't recreate it
337 if(file_exists(dirname($image) ."/thumbs/". $width ."_". basename($image)))
340 $src_img = @imagecreatefromjpeg($image);
344 /* grabs the height and width */
345 $new_w = imagesx($src_img);
346 $new_h = imagesy($src_img);
348 // If requested width is more then the actual image width,
349 // do not generate a thumbnail
351 if($width >= $new_w) {
352 imagedestroy($src_img);
356 /* calculates aspect ratio */
357 $aspect_ratio = $new_h / $new_w;
361 $new_h = abs($new_w * $aspect_ratio);
363 /* creates new image of that size */
364 $dst_img = imagecreatetruecolor($new_w,$new_h);
366 imagefill($dst_img, 0, 0, ImageColorAllocate($dst_img, 255, 255, 255));
368 /* copies resized portion of original image into new image */
369 imagecopyresampled($dst_img,$src_img,0,0,0,0,$new_w,$new_h,imagesx($src_img),imagesy($src_img));
371 /* write down new generated file */
373 if(!file_exists(dirname($image) ."/thumbs"))
374 mkdir(dirname($image) ."/thumbs");
376 $newfile = dirname($image) ."/thumbs/". $width ."_". basename($image);
377 imagejpeg($dst_img, $newfile, 75);
380 imagedestroy($dst_img);
381 imagedestroy($src_img);
384 } // create_thumbnail()
386 public function get_meta_informations($file)
389 return exif_read_data($file);
391 } // get_meta_informations()
393 public function check_config_table()
395 // if the config table doesn't exist yet, create it
396 if(!$this->cfg_db->db_check_table_exists("images")) {
397 $this->cfg_db->db_exec("
398 CREATE TABLE images (
399 img_idx int primary key,
405 } // check_config_table
407 public function gen_thumbs($idx = 0, $fromcmd = 0)
410 /* get all available photos */
411 $all = $this->getAllTagPhotos();
416 foreach($all as $photo) {
418 $details = $this->get_photo_details($photo);
420 $full_path = $this->translate_path($details['directory_path']) ."/". $details['name'];
421 $file_md5 = md5_file($full_path);
423 if($fromcmd) print "Image ". $details['name'] ." Thumbnails:";
425 /* if the file hasn't changed there is no need to regen the thumb */
426 if($file_md5 == $this->getMD5($photo)) {
427 if($fromcmd) print " file has not changed - skipping\n";
431 /* set the new/changed MD5 sum for the current photo */
432 $this->setMD5($photo, $file_md5);
434 $resolutions = Array(
435 $this->cfg->thumb_width,
436 $this->cfg->bubble_width,
437 $this->cfg->photo_width
440 /* create thumbnails for the requested resolutions */
441 foreach($resolutions as $resolution) {
442 if($fromcmd) print " ". $resolution ."px";
443 $this->create_thumbnail($full_path, $resolution);
446 if($fromcmd) print "\n";
452 private function getMD5($idx)
454 $result = $this->cfg_db->db_query("
457 WHERE img_idx='". $idx ."'
463 $img = $this->cfg_db->db_fetch_object($result);
464 return $img['img_md5'];
468 private function setMD5($idx, $md5)
470 $result = $this->cfg_db->db_exec("
471 REPLACE INTO images (img_idx, img_md5)
472 VALUES ('". $idx ."', '". $md5 ."')
477 public function setTagCondition($mode)
479 $_SESSION['tag_condition'] = $mode;
481 } // setTagCondition()