From 0e99d887d8d676d332918877d23ab8a9103e6a43 Mon Sep 17 00:00:00 2001 From: Varun Patil Date: Wed, 17 Aug 2022 21:45:01 +0000 Subject: [PATCH] Update to use exiftool --- README.md | 5 ++++- lib/Db/Util.php | 37 ++++++++++++++++++++++++++++++------- 2 files changed, 34 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 1f03816e..cf21ec86 100644 --- a/README.md +++ b/README.md @@ -4,12 +4,15 @@ ## How is this different? -* **📸 Photo and Video Timeline**: Sorts photos by date taken. +* **📸 Photo and Video Timeline**: Sorts photos by date taken, parsed from Exif data. * **🤔 Quick Recap**: Jump to anywhere in the timeline instantly. +* **🖼️ Albums**: Browse your and shared folders with a similar, efficient timeline. +* **🎦 Slideshow**: View photos from your timeline and albums easily. ## 🚀 Installation 1. ☁ Clone this into your `apps` folder of your Nextcloud. +1. ⚒️ Install `exiftool` (`sudo apt install exiftool`). 1. Run `php ./occ polaroid:index` to generate metadata indices for existing photos. 1. Consider installing the [preview generator](https://github.com/rullzer/previewgenerator) for pre-generating thumbnails. diff --git a/lib/Db/Util.php b/lib/Db/Util.php index a7d3dcdb..b56eb15e 100644 --- a/lib/Db/Util.php +++ b/lib/Db/Util.php @@ -14,16 +14,39 @@ class Util { $this->connection = $connection; } + private static function getExif($data, $field) { + $pipes = []; + $proc = proc_open('exiftool -b -' . $field . ' -', [ + 0 => array('pipe', 'rb'), + 1 => array('pipe', 'w'), + 2 => array('pipe', 'w'), + ], $pipes); + + fwrite($pipes[0], $data); + fclose($pipes[0]); + $stdout = stream_get_contents($pipes[1]); + proc_close($proc); + return $stdout; + } + public static function getDateTaken(File $file) { // Attempt to read exif data - if (in_array($file->getMimeType(), Application::IMAGE_MIMES)) { - $exif = exif_read_data($file->fopen('rb')); - $dt = $exif['DateTimeOriginal']; + // Assume it exists in the first 256 kb of the file + $handle = $file->fopen('rb'); + $data = stream_get_contents($handle, 256 * 1024); + fclose($handle); + + // Try different formats + $dt = self::getExif($data, 'DateTimeOriginal'); + if (empty($dt)) { + $dt = self::getExif($data, 'CreateDate'); + } + + // Check if found something + if (!empty($dt)) { + $dt = \DateTime::createFromFormat('Y:m:d H:i:s', $dt); if ($dt) { - $dt = \DateTime::createFromFormat('Y:m:d H:i:s', $dt); - if ($dt) { - return $dt->getTimestamp(); - } + return $dt->getTimestamp(); } }