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 public function __destruct()
37 public function show()
39 $this->tmpl->assign('page_title', $this->cfg->page_title);
40 $this->tmpl->show("index.tpl");
44 private function get_tags()
46 $this->avail_tags = Array();
49 $result = $this->db->db_query("
52 ORDER BY sort_priority ASC
55 while($row = $this->db->db_fetch_object($result)) {
58 $tag_name = $row['name'];
60 $this->tags[$tag_id] = $tag_name;
61 $this->avail_tags[$count] = $tag_id;
69 public function get_photo_details($idx)
71 $result = $this->db->db_query("
77 return $this->db->db_fetch_object($result);
79 } // get_photo_details
81 public function translate_path($path, $width = 0)
83 return str_replace($this->cfg->path_replace_from, $this->cfg->path_replace_to, $path);
87 public function showPhoto($photo)
89 $all_photos = $this->getAllTagPhotos();
91 foreach($all_photos as $all_photo) {
94 $next_img = $all_photo;
98 if($all_photo == $photo) {
102 $previous_img = $all_photo;
107 $details = $this->get_photo_details($photo);
108 $meta = $this->get_meta_informations($this->translate_path($details['directory_path']) ."/". $details['name']);
109 $info = getimagesize($this->translate_path($details['directory_path']) ."/thumbs/". $this->cfg->photo_width ."_". $details['name']);
111 $this->tmpl->assign('width', $info[0]);
112 $this->tmpl->assign('height', $info[1]);
113 $this->tmpl->assign('c_date', $meta['DateTime']);
114 $this->tmpl->assign('madewith', $meta['Make'] ." ". $meta['Model']);
115 $this->tmpl->assign('image_name', $details['name']);
116 $this->tmpl->assign('image_url', 'phpfspot_img.php?idx='. $photo ."&width=". $this->cfg->photo_width);
117 $this->tmpl->assign('image_url_full', 'phpfspot_img.php?idx='. $photo);
120 $this->tmpl->assign('previous_url', "javascript:showImage(". $previous_img .");");
124 $this->tmpl->assign('next_url', "javascript:showImage(". $next_img .");");
127 $this->tmpl->show("single_photo.tpl");
131 public function getAvailableTags()
133 foreach($this->avail_tags as $tag)
135 if(isset($_SESSION['selected_tags']) && in_array($tag, $_SESSION['selected_tags']))
138 // return all available (= not selected) tags
139 print "<a href=\"javascript:Tags('add', ". $tag .");\">". $this->tags[$tag] ."</a> ";
143 } // getAvailableTags()
145 public function getSelectedTags()
147 foreach($this->avail_tags as $tag)
149 // return all selected tags
150 if(isset($_SESSION['selected_tags']) && in_array($tag, $_SESSION['selected_tags'])) {
151 print "<a href=\"javascript:Tags('del', ". $tag .");\">". $this->tags[$tag] ."</a> ";
156 } // getSelectedTags()
158 public function addTag($tag)
160 if(!isset($_SESSION['selected_tags']))
161 $_SESSION['selected_tags'] = Array();
163 array_push($_SESSION['selected_tags'], $tag);
167 public function delTag($tag)
169 if(isset($_SESSION['selected_tags'])) {
170 $key = array_search($tag, $_SESSION['selected_tags']);
171 unset($_SESSION['selected_tags'][$key]);
176 public function resetTags()
178 unset($_SESSION['selected_tags']);
182 public function getAllTagPhotos()
184 $tagged_photos = Array();
186 if(isset($_SESSION['selected_tags'])) {
188 foreach($_SESSION['selected_tags'] as $tag)
189 $selected.= $tag .",";
190 $selected = substr($selected, 0, strlen($selected)-1);
191 $result = $this->db->db_query("
192 SELECT DISTINCT photo_id
196 WHERE pt.tag_id IN (". $selected .")
201 $result = $this->db->db_query("
202 SELECT DISTINCT photo_id
210 while($row = $this->db->db_fetch_object($result)) {
211 array_push($tagged_photos, $row['photo_id']);
214 return $tagged_photos;
216 } // getAllTagPhotos()
218 public function showPhotoIndex()
220 $photos = $this->getAllTagPhotos();
221 $count = count($photos);
225 $images[$rows] = Array();
227 for($i = 0; $i < $count; $i++) {
229 $images[$rows][$cols] = $photos[$i];
231 if($cols == $this->cfg->thumbs_per_row-1) {
234 $images[$rows] = Array();
241 // +1 for for smarty's selection iteration
244 //$images.= "<img src=\"phpfspot_img.php?idx=". $photo ."&width=". $this->cfg->thumb_width ."\" /><br />\n";
246 $this->tmpl->assign('count', $count);
247 $this->tmpl->assign('width', $this->cfg->thumb_width);
248 $this->tmpl->assign('images', $images);
249 $this->tmpl->assign('rows', $rows);
250 $this->tmpl->assign('columns', $this->cfg->thumbs_per_row);
251 $this->tmpl->show("photo_index.tpl");
254 } // showPhotoIndex()
256 public function showBubbleDetails($photo, $direction)
258 if($direction == "up")
259 $direction = "bubbleimg_up";
261 $direction = "bubbleimg_down";
263 $details = $this->get_photo_details($photo);
265 $image_url = "phpfspot_img.php?idx=". $photo ."&width=". $this->cfg->bubble_width;
267 $filesize = filesize($this->translate_path($details['directory_path']) ."/". $details['name']);
268 $filesize = rand($filesize/1024, 2);
270 $img = getimagesize($this->translate_path($details['directory_path']) ."/". $details['name']);
272 $this->tmpl->assign('file_size', $filesize);
273 $this->tmpl->assign('width', $img[0]);
274 $this->tmpl->assign('height', $img[1]);
275 $this->tmpl->assign('file_name', $details['name']);
276 $this->tmpl->assign('image_id', $direction);
277 $this->tmpl->assign('image_url', $image_url);
278 $this->tmpl->show("bubble_details.tpl");
280 } // showBubbleDetails()
282 public function showCredits()
284 $this->tmpl->assign('version', $this->cfg->version);
285 $this->tmpl->assign('product', $this->cfg->product);
286 $this->tmpl->show("credits.tpl");
290 public function create_thumbnail($image, $width)
292 // if thumbnail already exists, don't recreate it
293 if(file_exists(dirname($image) ."/thumbs/". $width ."_". basename($image)))
296 $src_img = @imagecreatefromjpeg($image);
300 /* grabs the height and width */
301 $new_w = imagesx($src_img);
302 $new_h = imagesy($src_img);
304 // If requested width is more then the actual image width,
305 // do not generate a thumbnail
307 if($width >= $new_w) {
308 imagedestroy($src_img);
312 /* calculates aspect ratio */
313 $aspect_ratio = $new_h / $new_w;
317 $new_h = abs($new_w * $aspect_ratio);
319 /* creates new image of that size */
320 $dst_img = imagecreatetruecolor($new_w,$new_h);
322 imagefill($dst_img, 0, 0, ImageColorAllocate($dst_img, 255, 255, 255));
324 /* copies resized portion of original image into new image */
325 imagecopyresampled($dst_img,$src_img,0,0,0,0,$new_w,$new_h,imagesx($src_img),imagesy($src_img));
327 /* write down new generated file */
329 if(!file_exists(dirname($image) ."/thumbs"))
330 mkdir(dirname($image) ."/thumbs");
332 $newfile = dirname($image) ."/thumbs/". $width ."_". basename($image);
333 imagejpeg($dst_img, $newfile, 75);
336 imagedestroy($dst_img);
337 imagedestroy($src_img);
340 } // create_thumbnail()
342 public function get_meta_informations($file)
345 return exif_read_data($file);
347 } // get_meta_informations()
349 public function check_config_table()
351 // if the config table doesn't exist yet, create it
352 if(!$this->cfg_db->db_check_table_exists("images")) {
353 $this->cfg_db->db_exec("
354 CREATE TABLE images (
355 img_idx int primary key,
361 } // check_config_table
363 public function gen_thumbs($idx = 0, $fromcmd = 0)
366 /* get all available photos */
367 $all = $this->getAllTagPhotos();
372 foreach($all as $photo) {
374 $details = $this->get_photo_details($photo);
376 $full_path = $this->translate_path($details['directory_path']) ."/". $details['name'];
377 $file_md5 = md5_file($full_path);
379 if($fromcmd) print "Image ". $details['name'] ." Thumbnails:";
381 /* if the file hasn't changed there is no need to regen the thumb */
382 if($file_md5 == $this->getMD5($photo)) {
383 if($fromcmd) print " file has not changed - skipping\n";
387 /* set the new/changed MD5 sum for the current photo */
388 $this->setMD5($photo, $file_md5);
390 $resolutions = Array(
391 $this->cfg->thumb_width,
392 $this->cfg->bubble_width,
393 $this->cfg->photo_width
396 /* create thumbnails for the requested resolutions */
397 foreach($resolutions as $resolution) {
398 if($fromcmd) print " ". $resolution ."px";
399 $this->create_thumbnail($full_path, $resolution);
402 if($fromcmd) print "\n";
408 private function getMD5($idx)
410 $result = $this->cfg_db->db_query("
413 WHERE img_idx='". $idx ."'
419 $img = $this->cfg_db->db_fetch_object($result);
420 return $img['img_md5'];
424 private function setMD5($idx, $md5)
426 $result = $this->cfg_db->db_exec("
427 REPLACE INTO images (img_idx, img_md5)
428 VALUES ('". $idx ."', '". $md5 ."')