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 /* check if config requests to ignore this tag */
61 if(in_array($row['name'], $this->cfg->hide_tags))
64 $this->tags[$tag_id] = $tag_name;
65 $this->avail_tags[$count] = $tag_id;
73 public function get_photo_details($idx)
75 $result = $this->db->db_query("
81 return $this->db->db_fetch_object($result);
83 } // get_photo_details
85 public function translate_path($path, $width = 0)
87 return str_replace($this->cfg->path_replace_from, $this->cfg->path_replace_to, $path);
91 public function showPhoto($photo)
93 $all_photos = $this->getAllTagPhotos();
95 foreach($all_photos as $all_photo) {
98 $next_img = $all_photo;
102 if($all_photo == $photo) {
106 $previous_img = $all_photo;
111 $details = $this->get_photo_details($photo);
112 $meta = $this->get_meta_informations($this->translate_path($details['directory_path']) ."/". $details['name']);
113 $info = getimagesize($this->translate_path($details['directory_path']) ."/thumbs/". $this->cfg->photo_width ."_". $details['name']);
115 $this->tmpl->assign('width', $info[0]);
116 $this->tmpl->assign('height', $info[1]);
117 $this->tmpl->assign('c_date', $meta['DateTime']);
118 $this->tmpl->assign('madewith', $meta['Make'] ." ". $meta['Model']);
119 $this->tmpl->assign('image_name', $details['name']);
120 $this->tmpl->assign('image_url', 'phpfspot_img.php?idx='. $photo ."&width=". $this->cfg->photo_width);
121 $this->tmpl->assign('image_url_full', 'phpfspot_img.php?idx='. $photo);
124 $this->tmpl->assign('previous_url', "javascript:showImage(". $previous_img .");");
128 $this->tmpl->assign('next_url', "javascript:showImage(". $next_img .");");
131 $this->tmpl->show("single_photo.tpl");
135 public function getAvailableTags()
137 foreach($this->avail_tags as $tag)
139 if(isset($_SESSION['selected_tags']) && in_array($tag, $_SESSION['selected_tags']))
142 // return all available (= not selected) tags
143 print "<a href=\"javascript:Tags('add', ". $tag .");\">". $this->tags[$tag] ."</a> ";
147 } // getAvailableTags()
149 public function getSelectedTags()
151 foreach($this->avail_tags as $tag)
153 // return all selected tags
154 if(isset($_SESSION['selected_tags']) && in_array($tag, $_SESSION['selected_tags'])) {
155 print "<a href=\"javascript:Tags('del', ". $tag .");\">". $this->tags[$tag] ."</a> ";
160 } // getSelectedTags()
162 public function addTag($tag)
164 if(!isset($_SESSION['selected_tags']))
165 $_SESSION['selected_tags'] = Array();
167 array_push($_SESSION['selected_tags'], $tag);
171 public function delTag($tag)
173 if(isset($_SESSION['selected_tags'])) {
174 $key = array_search($tag, $_SESSION['selected_tags']);
175 unset($_SESSION['selected_tags'][$key]);
180 public function resetTags()
182 unset($_SESSION['selected_tags']);
186 public function getAllTagPhotos()
188 $tagged_photos = Array();
190 if(isset($_SESSION['selected_tags'])) {
192 foreach($_SESSION['selected_tags'] as $tag)
193 $selected.= $tag .",";
194 $selected = substr($selected, 0, strlen($selected)-1);
195 $result = $this->db->db_query("
196 SELECT DISTINCT photo_id
200 WHERE pt.tag_id IN (". $selected .")
205 $result = $this->db->db_query("
206 SELECT DISTINCT photo_id
214 while($row = $this->db->db_fetch_object($result)) {
215 array_push($tagged_photos, $row['photo_id']);
218 return $tagged_photos;
220 } // getAllTagPhotos()
222 public function showPhotoIndex()
224 $photos = $this->getAllTagPhotos();
225 $count = count($photos);
229 $images[$rows] = Array();
231 for($i = 0; $i < $count; $i++) {
233 $images[$rows][$cols] = $photos[$i];
235 if($cols == $this->cfg->thumbs_per_row-1) {
238 $images[$rows] = Array();
245 // +1 for for smarty's selection iteration
248 //$images.= "<img src=\"phpfspot_img.php?idx=". $photo ."&width=". $this->cfg->thumb_width ."\" /><br />\n";
250 $this->tmpl->assign('count', $count);
251 $this->tmpl->assign('width', $this->cfg->thumb_width);
252 $this->tmpl->assign('images', $images);
253 $this->tmpl->assign('rows', $rows);
254 $this->tmpl->assign('columns', $this->cfg->thumbs_per_row);
255 $this->tmpl->show("photo_index.tpl");
258 } // showPhotoIndex()
260 public function showBubbleDetails($photo, $direction)
262 if($direction == "up")
263 $direction = "bubbleimg_up";
265 $direction = "bubbleimg_down";
267 $details = $this->get_photo_details($photo);
269 $image_url = "phpfspot_img.php?idx=". $photo ."&width=". $this->cfg->bubble_width;
271 $filesize = filesize($this->translate_path($details['directory_path']) ."/". $details['name']);
272 $filesize = rand($filesize/1024, 2);
274 $img = getimagesize($this->translate_path($details['directory_path']) ."/". $details['name']);
276 $this->tmpl->assign('file_size', $filesize);
277 $this->tmpl->assign('width', $img[0]);
278 $this->tmpl->assign('height', $img[1]);
279 $this->tmpl->assign('file_name', $details['name']);
280 $this->tmpl->assign('image_id', $direction);
281 $this->tmpl->assign('image_url', $image_url);
282 $this->tmpl->show("bubble_details.tpl");
284 } // showBubbleDetails()
286 public function showCredits()
288 $this->tmpl->assign('version', $this->cfg->version);
289 $this->tmpl->assign('product', $this->cfg->product);
290 $this->tmpl->show("credits.tpl");
294 public function create_thumbnail($image, $width)
296 // if thumbnail already exists, don't recreate it
297 if(file_exists(dirname($image) ."/thumbs/". $width ."_". basename($image)))
300 $src_img = @imagecreatefromjpeg($image);
304 /* grabs the height and width */
305 $new_w = imagesx($src_img);
306 $new_h = imagesy($src_img);
308 // If requested width is more then the actual image width,
309 // do not generate a thumbnail
311 if($width >= $new_w) {
312 imagedestroy($src_img);
316 /* calculates aspect ratio */
317 $aspect_ratio = $new_h / $new_w;
321 $new_h = abs($new_w * $aspect_ratio);
323 /* creates new image of that size */
324 $dst_img = imagecreatetruecolor($new_w,$new_h);
326 imagefill($dst_img, 0, 0, ImageColorAllocate($dst_img, 255, 255, 255));
328 /* copies resized portion of original image into new image */
329 imagecopyresampled($dst_img,$src_img,0,0,0,0,$new_w,$new_h,imagesx($src_img),imagesy($src_img));
331 /* write down new generated file */
333 if(!file_exists(dirname($image) ."/thumbs"))
334 mkdir(dirname($image) ."/thumbs");
336 $newfile = dirname($image) ."/thumbs/". $width ."_". basename($image);
337 imagejpeg($dst_img, $newfile, 75);
340 imagedestroy($dst_img);
341 imagedestroy($src_img);
344 } // create_thumbnail()
346 public function get_meta_informations($file)
349 return exif_read_data($file);
351 } // get_meta_informations()
353 public function check_config_table()
355 // if the config table doesn't exist yet, create it
356 if(!$this->cfg_db->db_check_table_exists("images")) {
357 $this->cfg_db->db_exec("
358 CREATE TABLE images (
359 img_idx int primary key,
365 } // check_config_table
367 public function gen_thumbs($idx = 0, $fromcmd = 0)
370 /* get all available photos */
371 $all = $this->getAllTagPhotos();
376 foreach($all as $photo) {
378 $details = $this->get_photo_details($photo);
380 $full_path = $this->translate_path($details['directory_path']) ."/". $details['name'];
381 $file_md5 = md5_file($full_path);
383 if($fromcmd) print "Image ". $details['name'] ." Thumbnails:";
385 /* if the file hasn't changed there is no need to regen the thumb */
386 if($file_md5 == $this->getMD5($photo)) {
387 if($fromcmd) print " file has not changed - skipping\n";
391 /* set the new/changed MD5 sum for the current photo */
392 $this->setMD5($photo, $file_md5);
394 $resolutions = Array(
395 $this->cfg->thumb_width,
396 $this->cfg->bubble_width,
397 $this->cfg->photo_width
400 /* create thumbnails for the requested resolutions */
401 foreach($resolutions as $resolution) {
402 if($fromcmd) print " ". $resolution ."px";
403 $this->create_thumbnail($full_path, $resolution);
406 if($fromcmd) print "\n";
412 private function getMD5($idx)
414 $result = $this->cfg_db->db_query("
417 WHERE img_idx='". $idx ."'
423 $img = $this->cfg_db->db_fetch_object($result);
424 return $img['img_md5'];
428 private function setMD5($idx, $md5)
430 $result = $this->cfg_db->db_exec("
431 REPLACE INTO images (img_idx, img_md5)
432 VALUES ('". $idx ."', '". $md5 ."')