<?php
require('header.htm');
function cache_shrunken_image ( $f, $aw, $ah, $name ) {
  if( ( $fp = fopen( $f, 'rb' ) ) || ($fp = fopen($_SERVER['DOCUMENT_ROOT'] . $f, 'rb')) ) {
    $data = fread( $fp, ( filesize( $f ) ? filesize( $f ) : IMG_MAXSIZE ) );
    if( $image = imagecreatefromstring( $data ) ) {
      $iw = imageSX( $image );
      $ih = imageSY( $image );
      if ($iw > $ih) {
        $ox = ($iw - $ih) / 2;
        $oy = 0;
        $iw = $ih;
      } else {
        $oy = ($ih - $iw) / 2;
        $ox = 0;
        $ih = $iw;
      }
      $new = imageCreatetruecolor( $aw, $ah );
      // $new = imageCreate( $aw, $ah );
      imageCopyResized( $new, $image, 0, 0, $ox, $oy, $aw, $ah, $iw, $ih );
      imagejpeg($new, $name, 100);
      imagedestroy($new);
    }
  } 
}
function read_pics ($dir) {
  $list  = array();
  $newfs = array();
  $files = opendir($dir);
  while ($file = readdir($files)) {
    if (preg_match('/^IMG/', $file) || preg_match('/^P/', $file)) {
      $is_avi   = strpos($file, "A.");
      $big      = 'pics/' . $file;
      $exif     = read_exif_data($big);
      $datetime = $exif['DateTimeOriginal'];
      $datetime = str_replace(':', '-', $datetime);
      $datetime = str_replace(' ', '_', $datetime);
      if ($is_avi) { // IMG_*A.JPG => MVI_*.AVI
	$vid    = str_replace('IMG', 'MVI', $file);
	$vid    = str_replace("A",   '',    $vid);
	$vid    = str_replace('JPG', 'AVI', $vid);
	print $vid . "\n";
	rename('pics/' . $vid, 'pics/' . $datetime . ".AVI");
        $datetime = $datetime . "_A";
      }
      $file     = $datetime . ".JPG";
      rename($big, 'pics/' . $file);
      $newfs[]  = $file;
    } else if (preg_match("/.JPG/", $file)) {
      $list[] = $file;
    }
  }
  closedir($files);
  return $list;
}
function sort_pics ($files) {
  return $files;
} 
function date_time ($date) {
  $elts = explode("-", $date);
  return mktime(0, 0, 0, $elts[1], $elts[2], $elts[0]);
}
function days_since ($now, $epoch) {
  $now_time   = date_time($now);
  $epoch_time = date_time($epoch);
  return round(($now_time - $epoch_time) / 60 / 60 / 24);
}
$i = 0;
$n_cols = 8;
print "<center>\n";
print '<font size="1" face="Arial, Helvetica, sans-serif">';
print "<table border=0>\n";
print "<tr>";
$files = read_pics('pics');
rsort($files);
$last_days = -100;
foreach ($files as $file) {
  $datetime = str_replace('.JPG', '', $file);
  $is_avi   = strpos($file, "_A.");
  $big      = 'pics/' . $file;
  $small    = 'pics/cache/' . $file . ".cache";
  if (file_exists($small) == FALSE)
    cache_shrunken_image($big, 100, 100, $small);
  if ($datetime) {
  $days = days_since(substr($datetime, 0, 11), "2005-11-11");
  if ($days != $last_days) {
    if ($i > 0 && ($i % $n_cols) == 0)
       print "</tr>\n<tr>";
    print "<td align=center><font size=+6><b>" . $days . "</b></font></td>";
    $i += 1;
  }
  } else
    $days = $last_days;
  if ($i > 0 && ($i % $n_cols) == 0)
     print "</tr>\n<tr>";
  print "<td align=center>";
  if ($is_avi) {  // *_A.JPG => *.AVI
    $big = str_replace('_A.', '.', $big);
    $big = str_replace('JPG', 'AVI', $big);
  }
  print '<a href="' . $big . '"><img src="' . $small . "\"></a>\n";
  print '</td>';
  $i += 1;
  $last_days = $days;
}
print "</tr>\n";
print "</table>\n";
print "</font>\n";
print "</center>\n";
require('footer.htm');
?>
