-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathReader.php
More file actions
172 lines (149 loc) · 3.79 KB
/
Copy pathReader.php
File metadata and controls
172 lines (149 loc) · 3.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
<?php
/**
* Reader which uses exiftool to read EXIF data
*
* @category PHPExif
* @copyright Copyright (c) 2016 Tom Van Herreweghe <tom@theanalogguy.be>
* @license http://github.com/PHPExif/php-exif-exiftool/blob/master/LICENSE MIT License
* @link http://github.com/PHPExif/php-exif-exiftool for the canonical source repository
* @package Exiftool
*/
namespace PHPExif\Adapter\Exiftool;
use PHPExif\Common\Adapter\MapperInterface;
use PHPExif\Common\Adapter\ReaderInterface;
use PHPExif\Common\Data\Exif;
use PHPExif\Common\Data\Iptc;
use PHPExif\Common\Data\Metadata;
use PHPExif\Common\Exception\Reader\NoExifDataException;
use \RuntimeException;
/**
* Reader
*
* Reads EXIF data
*
* @category PHPExif
* @package Exiftool
*/
final class Reader implements ReaderInterface
{
const PATH = 'path';
const BIN = 'binary';
const NUMERIC = 'numeric';
/**
* @var MapperInterface
*/
private $mapper;
/**
* @var string
*/
private $binary;
/**
* @var bool
*/
private $numeric;
/**
* @var string
*/
private $path;
/**
* @param MapperInterface $mapper
*/
public function __construct(
MapperInterface $mapper,
array $config = []
) {
$defaults = [
self::BIN => 'exiftool',
self::PATH => '/usr/bin/env',
self::NUMERIC => true,
];
$config = array_replace($defaults, $config);
$this->binary = $config[self::BIN];
$this->path = $config[self::PATH];
$this->numeric = $config[self::NUMERIC];
$this->mapper = $mapper;
}
/**
* {@inheritDoc}
*/
public function getMapper()
{
return $this->mapper;
}
/**
* {@inheritDoc}
*/
public function getMetadataFromFile($filePath)
{
$result = $this->getCliOutput(
sprintf(
'%1$s%3$s -j -a -G1 -c %4$s %2$s',
$this->path,
escapeshellarg($filePath),
$this->numeric ? ' -n' : '',
escapeshellarg('%d deg %d\' %.4f"')
)
);
if (false === $result) {
throw NoExifDataException::fromFile($filePath);
}
$data = json_decode($result, true)[0];
$data = $this->normalizeArrayKeys($data);
// map the data:
$mapper = $this->getMapper();
$metadata = new Metadata(
new Exif,
new Iptc
);
$mapper->map($data, $metadata);
return $metadata;
}
/**
* Lowercases the keys for given array
*
* @param array $data
*
* @return array
*/
private function normalizeArrayKeys(array $data)
{
$keys = array_keys($data);
$keys = array_map('strtolower', $keys);
$values = array_values($data);
$values = array_map(function ($value) {
if (!is_array($value)) {
return $value;
}
return $this->normalizeArrayKeys($value);
}, $values);
return array_combine(
$keys,
$values
);
}
/**
* Returns the output from given cli command
*
* @param string $command
*
* @return string|boolean
*/
protected function getCliOutput($command)
{
$descriptorspec = array(
0 => array('pipe', 'r'),
1 => array('pipe', 'w'),
2 => array('pipe', 'a')
);
$process = proc_open($command, $descriptorspec, $pipes);
if (!is_resource($process)) {
return false;
}
$result = stream_get_contents($pipes[1]);
fclose($pipes[0]);
fclose($pipes[1]);
fclose($pipes[2]);
proc_close($process);
return $result;
}
}