Completed
Pull Request — develop (#2)
by Tom Van
03:06
created

Reader::getMetadataFromFile()   B

Complexity

Conditions 3
Paths 2

Size

Total Lines 28
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

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