<?php
function evalu($data) {
        $tmp = split('/', $data, 2);
        return ($tmp[0] / $tmp[1]);
}

$diskbase = '/var/www/openstreetphoto.org/htdocs/data';
$diskthumbbase = '/var/www/openstreetphoto.org/htdocs/thumbs';
$urlbase  = 'http://photos.openstreetphoto.org';
$thumbbase  = 'http://thumbs.openstreetphoto.org';
$paths = array('/milo', '/floris-adam-centrum');

$i=0;

echo '<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://earth.google.com/kml/2.2">
 <Document>';


foreach ($paths as $path) {

if ($handle = opendir($diskbase.$path)) {
    while (false !== ($file = readdir($handle))) {
        if (substr(strtolower($file), -4) == '.jpg') {
                $pathfile = $path.'/'.$file;
                $photo = $diskbase.$pathfile;
                $exif=@exif_read_data($photo);

                if ($exif === false) {
                        echo $photo;
                } else {


                $lat = '';
                $lon = '';

                switch (count($exif['GPSLatitude'])) {
                        case 1:
                                $lat = $exif['GPSLatitude'];
                                break;
                        case 3:
                                $lat = (evalu($exif['GPSLatitude'][0]) + (evalu($exif['GPSLatitude'][1]) / 60) + (evalu($exif['GPSLatitude'][2]) / 3600)) * ($exif['GPSLatitudeRef'][0] == 'N' ? 1 : -1);
                                break;
                }

                switch (count($exif['GPSLongitude'])) {
                        case 1:
                                $lon = $exif['GPSLongitude'];
                                break;
                        case 3:
                                $lon = (evalu($exif['GPSLongitude'][0]) + (evalu($exif['GPSLongitude'][1]) / 60) + (evalu($exif['GPSLongitude'][2]) / 3600)) * ($exif['GPSLongitudeRef'][0] == 'E' ? 1 : -1);
                                break;
                }

                if ($lat != '' && $lon != '') {
                        $thumbfile = $diskthumbbase.$pathfile;
                        if (!file_exists( $thumbfile )) {
                        $thumbWidth = 150;
                        @mkdir($diskthumbbase.$path, 0755, true);
                        $img = imagecreatefromjpeg( $photo );
                        $width = imagesx( $img );
                        $height = imagesy( $img );

                        $new_width = $thumbWidth;
                        $new_height = floor( $height * ( $thumbWidth / $width ) );
                        $tmp_img = imagecreatetruecolor( $new_width, $new_height );
                        imagecopyresized( $tmp_img, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height );
                        imagejpeg( $tmp_img, $thumbfile );
                        }
                        
echo '
  <Placemark id="Photo'.$i.'">
    <description>
      <![CDATA[<a href="'.$urlbase.$pathfile.'"><img src="'.$thumbbase.$pathfile.'" alt="'.$file.'"/></a>]]>
    </description>
    <name>'.$file.'</name>
    <Icon>
      <href>'.$thumbbase.$pathfile.'</href>
    </Icon>
    <Point>
      <coordinates>'.$lon.','.$lat.'</coordinates>
    </Point>
  </Placemark>';
                $i++;
                }

                }

        }
    }
    closedir($handle);
}
}
echo '
 </Document>
</kml>';
?>