1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Meta Reader package. |
5
|
|
|
* |
6
|
|
|
* (c) Stephan Wentz <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Temp\MetaReader; |
13
|
|
|
|
14
|
|
|
use PHPExiftool\Driver\Metadata\Metadata; |
15
|
|
|
use PHPExiftool\Driver\Value\ValueInterface; |
16
|
|
|
use PHPExiftool\FileEntity; |
17
|
|
|
use PHPExiftool\Reader; |
18
|
|
|
use Temp\MetaReader\Value\MetaValue; |
19
|
|
|
use Temp\MetaReader\Value\ValueBag; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Exiftool reader. |
23
|
|
|
* |
24
|
|
|
* @author Stephan Wentz <[email protected]> |
25
|
|
|
*/ |
26
|
|
|
class ExiftoolReader implements ReaderInterface |
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* @var Reader |
30
|
|
|
*/ |
31
|
|
|
private $reader; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @param Reader $reader |
35
|
|
|
*/ |
36
|
5 |
|
public function __construct(Reader $reader) |
37
|
|
|
{ |
38
|
5 |
|
$this->reader = $reader; |
39
|
5 |
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* {@inheritdoc} |
43
|
|
|
*/ |
44
|
1 |
|
public function available() |
45
|
|
|
{ |
46
|
1 |
|
return true; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* {@inheritdoc} |
51
|
|
|
*/ |
52
|
2 |
|
public function supports($filename) |
53
|
|
|
{ |
54
|
|
|
try { |
55
|
2 |
|
$fileEntity = $this->reader->reset()->files($filename)->first(); |
56
|
2 |
|
} catch (\Exception $e) { |
57
|
1 |
|
return false; |
58
|
|
|
} |
59
|
|
|
|
60
|
1 |
|
return $fileEntity instanceof FileEntity; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* {@inheritdoc} |
65
|
|
|
*/ |
66
|
2 |
|
public function read($filename) |
67
|
|
|
{ |
68
|
2 |
|
$meta = new ValueBag(); |
69
|
|
|
|
70
|
|
|
try { |
71
|
2 |
|
$fileEntity = $this->reader->reset()->files($filename)->first(); |
72
|
2 |
|
} catch (\Exception $e) { |
73
|
1 |
|
return $meta; |
74
|
|
|
} |
75
|
|
|
|
76
|
1 |
View Code Duplication |
if ($fileEntity->getMetadatas()->containsKey('File:ImageWidth')) { |
|
|
|
|
77
|
1 |
|
$meta->set( |
78
|
1 |
|
'image.width', |
79
|
1 |
|
new MetaValue((int) $fileEntity->getMetadatas()->get('File:ImageWidth')->getValue()->asString()) |
80
|
1 |
|
); |
81
|
1 |
|
} elseif ($fileEntity->getMetadatas()->containsKey('PNG:ImageWidth')) { |
82
|
|
|
$meta->set( |
83
|
|
|
'image.width', |
84
|
|
|
new MetaValue((int) $fileEntity->getMetadatas()->get('PNG:ImageWidth')->getValue()->asString()) |
85
|
|
|
); |
86
|
|
|
} |
87
|
|
|
|
88
|
1 |
View Code Duplication |
if ($fileEntity->getMetadatas()->containsKey('File:ImageHeight')) { |
|
|
|
|
89
|
1 |
|
$meta->set( |
90
|
1 |
|
'image.height', |
91
|
1 |
|
new MetaValue((int) $fileEntity->getMetadatas()->get('File:ImageHeight')->getValue()->asString()) |
92
|
1 |
|
); |
93
|
1 |
|
} elseif ($fileEntity->getMetadatas()->containsKey('PNG:ImageHeight')) { |
94
|
|
|
$meta->set( |
95
|
|
|
'image.height', |
96
|
|
|
new MetaValue((int) $fileEntity->getMetadatas()->get('PNG:ImageHeight')->getValue()->asString()) |
97
|
|
|
); |
98
|
|
|
} |
99
|
|
|
|
100
|
1 |
|
foreach ($fileEntity->getMetadatas() as $metadata) { |
101
|
|
|
/* @var $metadata Metadata */ |
102
|
|
|
|
103
|
1 |
|
if (ValueInterface::TYPE_BINARY === $metadata->getValue()->getType()) { |
104
|
|
|
continue; |
105
|
|
|
} |
106
|
|
|
|
107
|
1 |
|
$groupName = ($metadata->getTag()->getGroupName()); |
108
|
1 |
|
$name = ($metadata->getTag()->getName()); |
109
|
1 |
|
$value = (string) $metadata->getValue(); |
110
|
1 |
|
if ($groupName === 'System' || !ctype_print($value)) { |
111
|
1 |
|
continue; |
112
|
|
|
} |
113
|
|
|
|
114
|
1 |
|
$path = "$groupName.$name"; |
115
|
1 |
|
$meta->set($path, new MetaValue($value)); |
116
|
1 |
|
} |
117
|
|
|
|
118
|
1 |
|
return $meta; |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.