<?php

/* Fonction month(): convertit le mois (nb) en mois (texte) francais

	photo-du-mois.php
*/

function month($w) {    
    //setlocale(LC_TIME, 'fr_FR');
    $m = date('m', strtotime($w));	// month
    $y = date('Y', strtotime($w));	// year
    //echo $w . "- month: " . $m . "- year: " . $y;
        
    // or any other locales like pl_PL, cs_CZ, fr_FR, zh, zh_Hans, ...
	$locale = 'fr_FR';
	$dateFormatter = new IntlDateFormatter(
    	$locale,
    	IntlDateFormatter::LONG, // date type
    	IntlDateFormatter::NONE  // time type
		);
	$dateFormatter->setPattern('LLLL'); // full month name with NO DECLENSION ;-)
	$months_locale = [];
	for ($month_number = 1; $month_number <= 12; ++$month_number) {
    	$months_locale[] = $dateFormatter->format(
        	// 'n' => month number with no leading zeros
        	DateTime::createFromFormat('n', (string)$month_number)
    		);
		}
	
	array_unshift($months_locale,"");
	unset($months_locale[0]);
	//_pr($months_locale);
	$my = ucfirst($months_locale[(int)$m]) . " " . $y;
	//echo $my;
	return $my;
	}	


/* Fonctions get_gps() et gps2Num(): extrait les coord GPS depuis les exifs

	insert_bdd.php
*/

function gps2Num($coordPart){
	/*
	Array
	(
		[0] => 46/1
		[1] => 408587/10000
		[2] => 0/0
	)
	Array
	(
		[0] => 5/1
		[1] => 562596/10000
		[2] => 0/0
	)
	*/
	
    $parts = explode('/', $coordPart);
	//echo $parts[0].'-'.$parts[1].'<br>';
    if(count($parts) <= 0)
    return 0;
    if(count($parts) == 1)
    return $parts[0];
	if($parts[1] != 0)
    	return floatval($parts[0]) / floatval($parts[1]);
	else return 0;
}

function get_gps($exif) {
	
	if($exif && isset($exif['GPS'])){
		/*
		echo $exif['FILE']['FileName'];
		_pr($exif['GPS']['GPSLatitude']);
		_pr($exif['GPS']['GPSLongitude']);
		*/
		$GPSLatitudeRef = isset($exif['GPS']['GPSLatitudeRef']) ? $exif['GPS']['GPSLatitudeRef'] : '';
		$GPSLatitude = isset($exif['GPS']['GPSLatitude']) ? $exif['GPS']['GPSLatitude'] : '';
		$GPSLongitudeRef = isset($exif['GPS']['GPSLongitudeRef']) ? $exif['GPS']['GPSLongitudeRef'] : '';
		$GPSLongitude = isset($exif['GPS']['GPSLongitude']) ? $exif['GPS']['GPSLongitude'] : '';
		$GPSAltitude = isset($exif['GPS']['GPSAltitude']) ? $exif['GPS']['GPSAltitude'] : '';
    
    	$lat_degrees = count($GPSLatitude) > 0 ? gps2Num($GPSLatitude[0]) : 0;
    	$lat_minutes = count($GPSLatitude) > 1 ? gps2Num($GPSLatitude[1]) : 0;
    	$lat_seconds = count($GPSLatitude) > 2 ? gps2Num($GPSLatitude[2]) : 0;
 
     	$lon_degrees = count($GPSLongitude) > 0 ? gps2Num($GPSLongitude[0]) : 0;
    	$lon_minutes = count($GPSLongitude) > 1 ? gps2Num($GPSLongitude[1]) : 0;
    	$lon_seconds = count($GPSLongitude) > 2 ? gps2Num($GPSLongitude[2]) : 0;
		
    	$lat_direction = ($GPSLatitudeRef == 'W' or $GPSLatitudeRef == 'S') ? -1 : 1;
    	$lon_direction = ($GPSLongitudeRef == 'W' or $GPSLongitudeRef == 'S') ? -1 : 1;
        
    	$latitude = $lat_direction * ($lat_degrees + ($lat_minutes / 60) + ($lat_seconds / (60*60)));
    	$longitude = $lon_direction * ($lon_degrees + ($lon_minutes / 60) + ($lon_seconds / (60*60)));

		$alt      = explode('/', $GPSAltitude);
		$altitude = (isset($alt[1])) ? ($alt[0] / $alt[1]) : $alt[0];
		}
	else {
		$latitude = '';
		$longitude = '';
		$altitude = '';
		}	

		//echo $latitude . " - " . $longitude . " - " . $altitude;

    return array('latitude'=>$latitude, 'longitude'=>$longitude, 'altitude'=>$altitude);
	}


/* Fonction create_thumb(): Création des vignettes 

	insert_bdd.php
*/

function create_thumb($thumb_w, $thumb_h, $image) {

	list($origin_w, $origin_h) = getimagesize($image);
	$origin_ratio = round($origin_w / $origin_h, 1);
	$outFile = str_replace("photos/img", "photos/thumb", $image);

	if ($origin_w != null && $origin_h != null) {
		if ($thumb_w / $thumb_h > $origin_ratio) {
    		$thumb_w = $thumb_h * $origin_ratio;
			} else {
    		$thumb_h = $thumb_w / $origin_ratio;
			}

		if ($origin_w >= 400 && $origin_h >= 400) {
			$image = new Imagick($image);				// !!!
			$image->thumbnailImage($thumb_w, $thumb_h);
			$image->writeImage($outFile);
			$image->destroy();
			}
		}
	}

	
/* Fonction in_bdd(): test si la photo est déjà dans la bdd 

	insert_bdd.php
*/

function in_bdd($image) {
	try {
		$conn3 = new PDO('sqlite:db_photo.sqlite3');
		#$query3 = "SELECT filename FROM photos WHERE instr(filename, '". $file . "') > 0;";
		$query3 = "SELECT filename FROM photos WHERE filename = :filename";
		$stmt = $conn3->prepare($query3);
		$stmt->bindParam(':filename', $image, PDO::PARAM_STR);

		$stmt->execute();
		$result3 = $stmt->fetchAll(PDO::FETCH_ASSOC);
		if (count($result3) > 0) {
			return true;
			} else {
			return false;
			}
		$conn3 = null;	
   		}
    catch(PDOException $e) {
        echo $e->getMessage();
        }
	}


//

function _pr($d) {
    echo "<div style='border: 1px solid#ccc; padding: 10px;'>";
    echo '<strong>' . debug_backtrace()[0]['file'] . ' ' . debug_backtrace()[0]['line'] . '</strong>';
    echo "</div>";
    echo '<pre>';
    if(is_array($d)) {
    	print_r($d);
    	} else if(is_object($d)) {
    	var_dump($d);
    	}
    echo '</pre>';
	}



/* return geo exif in a nice form

*/

function geo_frac2dec($str) {
	@list( $n, $d ) = explode( '/', $str );
	if ( !empty($d) )
		return $n / $d;
	return $str;
}


/*
33° 52.37′ 0″ S 151° 9.06′ 0″ E
*/


function geo_pretty_fracs2dec($fracs) {
	return	geo_frac2dec($fracs[0]) . '&deg; ' .
		geo_frac2dec($fracs[1]) . '&prime; ' .
		geo_frac2dec($fracs[2]) . '&Prime; ';
}


/* 
to GoogleMaps 
*/

function geo_single_fracs2dec($fracs) {
	return	geo_frac2dec($fracs[0]) +
		geo_frac2dec($fracs[1]) / 60 +
		geo_frac2dec($fracs[2]) / 3600;
}

/**/
function host() {
	$pv_sslport=443;
	//$pv_serverport=80; 
	//$pv_servername="sur-le-sentier.fr";
		
	$pv_URIprotocol = isset($_SERVER["HTTPS"]) ? (($_SERVER["HTTPS"]==="on" || $_SERVER["HTTPS"]===1 || $_SERVER["SERVER_PORT"]===$pv_sslport) ? "https://" : "http://") :  (($_SERVER["SERVER_PORT"]===$pv_sslport) ? "https://" : "http://");
	if ($_SERVER['HTTP_HOST'] == "sur-le-sentier.fr") {
		$host = $pv_URIprotocol . $_SERVER['HTTP_HOST'] . "/";
		}
	elseif ($_SERVER['HTTP_HOST'] == "airbook.local") {
		$host = $pv_URIprotocol . $_SERVER['HTTP_HOST'] . "/sls/";
		}
	return $host;
	}

?>