Files
sls/functions.php
Bruno21 b35196fdb8 Version 1.0
-déplacement des fichiers d’admin dans un dossier admin
-corrections dans la fonction data_for_lightbox($data). Celle-ci est maintenant toujours utilisé pour l’affichage des vignettes et des images dans la lightbox
-les chemins sont nettoyés avec 2 variables: $chemin pour les images, $base pour la base Sqlite
-corrections diverses
2025-02-13 10:07:21 +01:00

509 lines
16 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
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>';
}
/**
* Simple helper to debug to the console
*
* @param Array, Object, String $data
* @return String
*/
function debug_to_console( $data ) {
$output = '';
if ( is_array( $data ) ) {
$output .= "<script>console.warn( 'Debug Objects with Array.' ); console.log( '" . implode( ',', $data) . "' );</script>";
} else if ( is_object( $data ) ) {
$data = var_export( $data, TRUE );
$data = explode( "\n", $data );
foreach( $data as $line ) {
if ( trim( $line ) ) {
$line = addslashes( $line );
$output .= "console.log( '{$line}' );";
}
}
$output = "<script>console.warn( 'Debug Objects with Object.' ); $output</script>";
} else {
$output .= "<script>console.log( 'Debug Objects: {$data}' );</script>";
}
echo $output;
}
/* Fonction month(): convertit le mois (nb) en mois (texte) francais
photo-du-mois.php
*/
function convert_bytes($val , $type_val , $type_wanted){
$tab_val = array("o", "ko", "Mo", "Go", "To", "Po", "Eo");
if (!(in_array($type_val, $tab_val) && in_array($type_wanted, $tab_val)))
return 0;
$tab = array_flip($tab_val);
$diff = $tab[$type_val] - $tab[$type_wanted];
if ($diff > 0)
return ($val * pow(1024, $diff));
if ($diff < 0)
return ($val / pow(1024, -$diff));
return ($val);
}
function taille_fichier($octets) {
$resultat = $octets;
for ($i=0; $i < 8 && $resultat >= 1024; $i++) {
$resultat = $resultat / 1024;
}
if ($i > 0) {
return preg_replace('/,00$/', '', number_format($resultat, 2, ',', ''))
. ' ' . substr('KMGTPEZY',$i-1,1) . 'o';
} else {
return $resultat . ' o';
}
}
function formatBytes($bytes, $precision = 2) {
$units = array('o', 'Ko', 'Mo', 'Go', 'To');
$bytes = max($bytes, 0);
$pow = floor(($bytes ? log($bytes) : 0) / log(1024));
$pow = min($pow, count($units) - 1);
// Uncomment one of the following alternatives
// $bytes /= pow(1024, $pow);
$bytes /= (1 << (10 * $pow));
return round($bytes, $precision) . " " .$units[$pow];
}
function month($w) {
$m = date('m', strtotime($w)); // month
$y = date('Y', strtotime($w)); // year
$locale = getenv('LC_ALL=');
$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]);
$my = ucfirst($months_locale[(int)$m]) . " " . $y;
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'] : '';
/*
echo $GPSLatitude;
echo $GPSLongitude;
echo $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();
}
}
/* 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'] == "sur-le-sentier.airbook.local") {
//$host = $pv_URIprotocol . $_SERVER['HTTP_HOST'] . "/sls/";
$host = $pv_URIprotocol . $_SERVER['HTTP_HOST'] . "/";
}
return $host;
}
class AdvancedFilesystemIterator extends ArrayIterator
{
public function __construct(string $path, int $flags = FilesystemIterator::KEY_AS_PATHNAME | FilesystemIterator::CURRENT_AS_FILEINFO | FilesystemIterator::SKIP_DOTS)
{
parent::__construct(iterator_to_array(new FilesystemIterator($path, $flags)));
}
public function __call(string $name, array $arguments)
{
if (preg_match('/^sortBy(.*)/', $name, $m)) return $this->sort('get' . $m[1]);
throw new MemberAccessException('Method ' . $methodName . ' not exists');
}
public function sort($method)
{
if (!method_exists('SplFileInfo', $method)) throw new InvalidArgumentException(sprintf('Method "%s" does not exist in SplFileInfo', $method));
$this->uasort(function(SplFileInfo $a, SplFileInfo $b) use ($method) { return (is_string($a->$method()) ? strnatcmp($a->$method(), $b->$method()) : $b->$method() - $a->$method()); });
return $this;
}
public function limit(int $offset = 0, int $limit = -1)
{
return parent::__construct(iterator_to_array(new LimitIterator($this, $offset, $limit))) ?? $this;
}
public function match(string $regex, int $mode = RegexIterator::MATCH, int $flags = 0, int $preg_flags = 0)
{
return parent::__construct(iterator_to_array(new RegexIterator($this, $regex, $mode, $flags, $preg_flags))) ?? $this;
}
}
/*
Suppress keywords like _vert_, _violet_...
*/
function clean_keywords($keywords) {
$v = preg_replace( "/_\w+_/", "", $keywords );
$v = str_replace(",,", ",", $v);
//$v= (substr($v, strlen($v)-1) == ",") ? substr($v, 0, -1) : $v;
$v= rtrim($v, ",");
$v= ltrim($v, ",");
return $v;
}
function conv_date($dateoriginal, $format) {
$locale = getenv('LC_ALL=');
if ($format == 1) {
$formatter = new IntlDateFormatter($locale, IntlDateFormatter::FULL, IntlDateFormatter::SHORT, "Europe/Paris");
// vendredi 16 septembre 2022 à 19:17
}
elseif ($format == 2) {
$formatter = new IntlDateFormatter($locale, IntlDateFormatter::LONG, IntlDateFormatter::LONG, "Europe/Paris");
// 16 septembre 2022 à 19:17:22 UTC+2
}
elseif ($format == 3) {
$formatter = new IntlDateFormatter($locale, IntlDateFormatter::MEDIUM, IntlDateFormatter::MEDIUM, "Europe/Paris");
// 16 sept. 2022, 19:17:22
}
elseif ($format == 4) {
$formatter = new IntlDateFormatter($locale, IntlDateFormatter::MEDIUM, IntlDateFormatter::SHORT, "Europe/Paris");
// 16 sept. 2022, 19:17
}
$date = new DateTime($dateoriginal);
return $formatter->format($date);
}
function data_for_lightbox($data) {
global $chemin;
//$query4 = "INSERT OR IGNORE INTO photos (filename, filesize, dateoriginal, lens, speed, correctexpo, iso, usercomment, comment, model, metering, flash, focal, program, wb, mode, width, height, html, aperture, software, lat, long, alt, keywords, title, creator, city, department, code, country, copyright, legende);
$filename = $data['filename'];
$title_thumb = pathinfo($filename, PATHINFO_FILENAME);
$aperture = $data['aperture'];
$model = $data['model'];
$objectif = $data['lens'];
$speed = $data['speed'];
$iso = $data['iso'];
$exif = $model . " \u{30FB} " . $objectif . " \u{30FB} " . $speed . " \u{30FB} " . $aperture . " \u{30FB} " . $iso . "ISO";
$correctexpo = $data['correctexpo'];
$metering = $data['metering'];
$program = $data['program'];
$wb = $data['wb'];
$software = $data['software'];
$dateoriginal = conv_date($data['dateoriginal'], 4);
$focal = $data['focal'];
$flash = $data['flash'];
$latitude = $data['lat'];
$longitude = $data['long'];
$altitude = $data['alt'];
$gps = (!empty($longitude) && !empty($latitude)) ? $latitude . "," . $longitude : "";
//$gps = $longitude . "," . $latitude;
$title = $data['title'];
$legende = $data['legende'];
$file = basename($filename);
if (!empty($title)) {
$x = $title;
$y = ($legende != "") ? $legende : "";
}
elseif (!empty($legende)) {
$x= $legende;
$y = "";
}
else {
$x = $file;
$y = "";
}
if (!empty($gps)) {
$map = '<a href = "https://maps.google.com/maps?q=' . $gps . '&t=&z=9&ie=UTF8&iwloc=&output=embed" title="' . $title . '" data-lcl-txt="' . $legende . '">' . " \u{30FB} \u{2693} " . '</a>';
}
else {
$map = '';
}
$creator = $data['creator'];
$big = host() . $chemin . $data['filename'];
$thumb = host() . str_replace("img", "thumb", $chemin) . $data['filename'];
// origine
//$big = host() . $data['filename'];
//$thumb = host() . str_replace("../photos/img", "../photos/thumb", $data['filename']);
$keywords = str_replace(',', " \u{30FB} ", clean_keywords($data['keywords']));
$copyright = $data['copyright'];
$city = $data['city'];
$department = $data['department'];
$code = $data['code'];
$country = $data['country'];
$comment = $data['comment'];
$usercomment = $data['usercomment'];
$description = "<table>";
$description .= "<tr><td>" . gettext("Date") . "</td><td>" . $dateoriginal . "</td></tr>";
$description .= "<tr><td>" . gettext("Speed") . "</td><td>" . $speed . "</td></tr>";
$description .= "<tr><td>" . gettext("Aperture") . "</td><td>" . $aperture . "</td></tr>";
$description .= "<tr><td>" . gettext("Iso") . "</td><td>" . $iso . "</td></tr>";
$description .= "<tr><td>" . gettext("Model") . "</td><td>". $model . "</td></tr>";
$description .= "<tr><td>" . gettext("Lens") . "</td><td>" . $objectif . "</td></tr>";
$description .= "<tr><td>" . gettext("Focal") . "</td><td>" . $focal . "</td></tr>";
$description .= "<tr><td>" . gettext("Exposure Correction") . "</td><td>" . $correctexpo . "</td></tr>";
$description .= "<tr><td>" . gettext("Metering") . "</td><td>". $metering . "</td></tr>";
$description .= "<tr><td>" . gettext("Program") . "</td><td>" . $program . "</td></tr>";
$description .= "<tr><td>" . gettext("White balance") . "</td><td>" . $wb . "</td></tr>";
if (! strcasecmp($flash, "Off") == 0) {
$description .= "<tr><td>" . gettext("Flash") . "</td><td>" . $flash . "</td></tr>";
}
$description .= "<tr><td>" . gettext("Software") . "</td><td>" . $software . "</td></tr>";
$description .= "<tr><td>" . gettext("Keywords") . "</td><td>" . $keywords . "</td></tr>";
(!empty($copyright)) && $description .= "<tr><td>" . gettext("Copyright") . "</td><td>" . $copyright . "</td></tr>";
(!empty($city)) && $description .= "<tr><td>" . gettext("City") . "</td><td>" . $city . "</td></tr>";
(!empty($department)) && $description .= "<tr><td>" . gettext("Department") . "</td><td>" . $department . "</td></tr>";
$code = (!empty($code)) ? " (" . $code . ")" : "";
(!empty($country)) && $description .= "<tr><td>" . gettext("Country") . "</td><td>" . $country . $code . "</td></tr>";
(!empty($map)) && $description .= "<tr><td>GPS</td><td>" . $map . "</td></tr>";
((!empty($comment)) || (!empty($usercomment))) && $description .= "<tr><td>&nbsp;</td><td>&nbsp;</td></tr>";
(!empty($comment)) && $description .= "<tr><td>" . gettext("Comment") . "</td><td>" . $comment . "</td></tr>";
(!empty($usercomment)) && $description .= "<tr><td>" . gettext("User comment") . "</td><td>" . $usercomment . "</td></tr>";
$description .= "</table>";
$lightbox = array();
$lightbox['title_thumb'] = $title_thumb;
$lightbox['exif'] = $exif;
$lightbox['title'] = $x;
$lightbox['legende'] = $y;
$lightbox['thumb'] = $thumb;
$lightbox['big'] = $big;
$lightbox['keywords'] = $keywords;
$lightbox['creator'] = $creator;
$lightbox['gps'] = $gps;
$lightbox['description'] = $description;
/*
Array
(
[title_thumb] => 12_2021
[exif] => Canon EOS 90D ・ EF500mm f/4L IS USM III ・ 1/3200 ・ f/5.6 ・ 1600ISO
[title] => 12_2021.jpg
[legende] =>
[thumb] => https://airbook.local/sls/photos/thumb/12_2021.jpg
[big] => https://airbook.local/sls/photos/img/12_2021.jpg
[keywords] => Carduelis carduelis ・ chardonneret élégant
[creator] => @Bruno Pesenti
[description] => <table>...</table>
)
*/
return $lightbox;
}
?>