Fix two calls to exiftool

pull/37/head
Varun Patil 2022-08-19 21:38:18 +00:00
parent 0aa3d485c1
commit ebab5225e0
1 changed files with 40 additions and 25 deletions

View File

@ -14,36 +14,48 @@ class Util {
$this->connection = $connection; $this->connection = $connection;
} }
private static function getExif($data, $field) { private static function getExif(File $file) {
$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 // Attempt to read exif data
// Assume it exists in the first 256 kb of the file // Assume it exists in the first 256 kb of the file
$handle = $file->fopen('rb'); try {
$data = stream_get_contents($handle, 256 * 1024); $handle = $file->fopen('rb');
fclose($handle); $data = stream_get_contents($handle, 256 * 1024);
fclose($handle);
// Try different formats if (!$data) {
$dt = self::getExif($data, 'DateTimeOriginal'); throw new \Exception('Could not read file');
if (empty($dt)) { }
$dt = self::getExif($data, 'CreateDate');
$pipes = [];
$proc = proc_open('exiftool -json -', [
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);
$json = json_decode($stdout, true);
if (empty($json)) {
throw new \Exception('Could not read exif data');
}
return $json[0];
} catch (\Exception $e) {
return [];
}
}
public static function getDateTaken(File $file, array $exif) {
$dt = $exif['DateTimeOriginal'];
if (!isset($dt) || empty($dt)) {
$dt = $exif['CreateDate'];
} }
// Check if found something // Check if found something
if (!empty($dt)) { if (isset($dt) && !empty($dt)) {
$dt = \DateTime::createFromFormat('Y:m:d H:i:s', $dt); $dt = \DateTime::createFromFormat('Y:m:d H:i:s', $dt);
if ($dt && $dt->getTimestamp() > -5364662400) { // 1800 A.D. if ($dt && $dt->getTimestamp() > -5364662400) { // 1800 A.D.
return $dt->getTimestamp(); return $dt->getTimestamp();
@ -91,8 +103,11 @@ class Util {
return; return;
} }
// Get exif data
$exif = self::getExif($file);
// Get more parameters // Get more parameters
$dateTaken = $this->getDateTaken($file); $dateTaken = $this->getDateTaken($file, $exif);
$dayId = floor($dateTaken / 86400); $dayId = floor($dateTaken / 86400);
$dateTaken = gmdate('Y-m-d H:i:s', $dateTaken); $dateTaken = gmdate('Y-m-d H:i:s', $dateTaken);