1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Reader which uses native PHP functionality 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-native/blob/master/LICENSE MIT License |
8
|
|
|
* @link http://github.com/PHPExif/php-exif-native for the canonical source repository |
9
|
|
|
* @package Native |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace PHPExif\Adapter\Native; |
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
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Reader |
23
|
|
|
* |
24
|
|
|
* Reads EXIF data |
25
|
|
|
* |
26
|
|
|
* @category PHPExif |
27
|
|
|
* @package Native |
28
|
|
|
*/ |
29
|
|
|
final class Reader implements ReaderInterface |
30
|
|
|
{ |
31
|
|
|
const SECTIONS = 'sections'; |
32
|
|
|
const ARRAYS = 'asArrays'; |
33
|
|
|
const THUMBNAILINFO = 'thumbnailInfo'; |
34
|
|
|
const WITHIPTC = 'withIptc'; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var MapperInterface |
38
|
|
|
*/ |
39
|
|
|
private $mapper; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var string |
43
|
|
|
*/ |
44
|
|
|
private $sections = null; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @var boolean |
48
|
|
|
*/ |
49
|
|
|
private $asArrays = false; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @var boolean |
53
|
|
|
*/ |
54
|
|
|
private $thumbnailInfo = false; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @var boolean |
58
|
|
|
*/ |
59
|
|
|
private $withIptc = true; |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @param MapperInterface $mapper |
63
|
|
|
* @param array $config |
64
|
|
|
*/ |
65
|
|
|
public function __construct( |
66
|
|
|
MapperInterface $mapper, |
67
|
|
|
array $config = [] |
68
|
|
|
) { |
69
|
|
|
$defaults = [ |
70
|
|
|
self::SECTIONS => null, |
71
|
|
|
self::ARRAYS => false, |
72
|
|
|
self::THUMBNAILINFO => false, |
73
|
|
|
self::WITHIPTC => true, |
74
|
|
|
]; |
75
|
|
|
|
76
|
|
|
$config = array_merge($defaults, $config); |
77
|
|
|
|
78
|
|
|
$this->sections = $config[self::SECTIONS]; |
79
|
|
|
$this->asArrays = $config[self::ARRAYS]; |
80
|
|
|
$this->thumbnailInfo = $config[self::THUMBNAILINFO]; |
81
|
|
|
$this->withIptc = $config[self::WITHIPTC]; |
82
|
|
|
|
83
|
|
|
$this->mapper = $mapper; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* {@inheritDoc} |
88
|
|
|
*/ |
89
|
|
|
public function getMapper() |
90
|
|
|
{ |
91
|
|
|
return $this->mapper; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* {@inheritDoc} |
96
|
|
|
*/ |
97
|
|
|
public function getMetadataFromFile($filePath) |
98
|
|
|
{ |
99
|
|
|
$data = @exif_read_data( |
100
|
|
|
$filePath, |
101
|
|
|
$this->sections, |
102
|
|
|
$this->asArrays, // flat array |
103
|
|
|
$this->thumbnailInfo // no thumbnail |
104
|
|
|
); |
105
|
|
|
|
106
|
|
|
if (false === $data) { |
107
|
|
|
throw NoExifDataException::fromFile($filePath); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
$this->augmentDataWithIptcRawData($filePath, $data); |
111
|
|
|
|
112
|
|
|
// map the data: |
113
|
|
|
$mapper = $this->getMapper(); |
114
|
|
|
$metadata = new Metadata( |
115
|
|
|
new Exif, |
116
|
|
|
new Iptc |
117
|
|
|
); |
118
|
|
|
$mapper->map($data, $metadata); |
119
|
|
|
|
120
|
|
|
return $metadata; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Adds data from iptcparse to the original raw EXIF data |
125
|
|
|
* |
126
|
|
|
* @param string $filePath |
127
|
|
|
* @param array $data |
128
|
|
|
* |
129
|
|
|
* @return void |
130
|
|
|
*/ |
131
|
|
|
private function augmentDataWithIptcRawData($filePath, array &$data) |
132
|
|
|
{ |
133
|
|
|
if (!$this->withIptc) { |
134
|
|
|
return; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
getimagesize($filePath, $info); |
138
|
|
|
|
139
|
|
|
if (!array_key_exists('APP13', $info)) { |
140
|
|
|
return; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
$iptcRawData = iptcparse($info['APP13']); |
144
|
|
|
|
145
|
|
|
// UTF8 |
146
|
|
|
if (isset($iptc["1#090"]) && $iptc["1#090"][0] == "\x1B%G") { |
|
|
|
|
147
|
|
|
$iptcRawData = array_map('utf8_encode', $iptcRawData); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
// Merge with original raw Exif data |
151
|
|
|
$data = array_merge( |
152
|
|
|
$data, |
153
|
|
|
$iptcRawData |
154
|
|
|
); |
155
|
|
|
} |
156
|
|
|
} |
157
|
|
|
|
This check looks for calls to
isset(...)
orempty()
on variables that are yet undefined. These calls will always produce the same result and can be removed.This is most likely caused by the renaming of a variable or the removal of a function/method parameter.