|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Jackal\ImageMerge\Metadata\Parser; |
|
4
|
|
|
|
|
5
|
|
|
use DateTime; |
|
6
|
|
|
use Exception; |
|
7
|
|
|
use Jackal\ImageMerge\Model\File\FileObjectInterface; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Class IPTCParser |
|
11
|
|
|
* @package Jackal\ImageMerge\Metadata\Parser |
|
12
|
|
|
*/ |
|
13
|
|
|
class IPTCParser extends AbstractParser |
|
14
|
|
|
{ |
|
15
|
|
|
const TITLE = '2#005'; |
|
16
|
|
|
const URGENCY = '2#010'; |
|
17
|
|
|
const CATEGORY = '2#015'; |
|
18
|
|
|
const SUB_CATEGORY = '2#020'; |
|
19
|
|
|
const SPECIAL_INSTRUCTION = '2#040'; |
|
20
|
|
|
const CREATION_DATE = '2#055'; |
|
21
|
|
|
const CREATION_TIME = '2#060'; |
|
22
|
|
|
const DIGITAL_CREATION_DATE = '2#062'; |
|
23
|
|
|
const DIGITAL_CREATION_TIME = '2#063'; |
|
24
|
|
|
const BY_LINE = '2#080'; |
|
25
|
|
|
const BY_LINE_TITLE = '2#085'; |
|
26
|
|
|
const CITY = '2#090'; |
|
27
|
|
|
const LOCATION = '2#092'; |
|
28
|
|
|
const STATE = '2#095'; |
|
29
|
|
|
const COUNTRY_CODE = '2#100'; |
|
30
|
|
|
const COUNTRY_NAME = '2#101'; |
|
31
|
|
|
const OTR = '2#103'; |
|
32
|
|
|
const HEADLINE = '2#105'; |
|
33
|
|
|
const CREDIT = '2#110'; |
|
34
|
|
|
const SOURCE = '2#115'; |
|
35
|
|
|
const COPYRIGHT = '2#116'; |
|
36
|
|
|
const CONTACT = '2#118'; |
|
37
|
|
|
const CAPTION = '2#120'; |
|
38
|
|
|
const CAPTION_WRITER = '2#122'; |
|
39
|
|
|
const CHARSET = '1#090'; |
|
40
|
|
|
const KEYWORDS = '2#025'; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* IPTCParser constructor. |
|
44
|
|
|
* @param FileObjectInterface $file |
|
45
|
|
|
*/ |
|
46
|
|
|
public function __construct(FileObjectInterface $file) |
|
47
|
|
|
{ |
|
48
|
|
|
@iptcembed('', $file->getPathname(), 0); |
|
|
|
|
|
|
49
|
|
|
$info = null; |
|
50
|
|
|
getimagesize($file->getPathname(), $info); |
|
51
|
|
|
|
|
52
|
|
|
if (isset($info['APP13'])) { |
|
53
|
|
|
$this->data = iptcparse($info['APP13']); |
|
|
|
|
|
|
54
|
|
|
} |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @return array|null|string |
|
59
|
|
|
*/ |
|
60
|
|
|
public function getCategory() |
|
61
|
|
|
{ |
|
62
|
|
|
return $this->getValue(self::CATEGORY); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* @return DateTime|null |
|
67
|
|
|
* @throws Exception |
|
68
|
|
|
*/ |
|
69
|
|
|
public function getCreationDateTime() |
|
70
|
|
|
{ |
|
71
|
|
|
if ($this->getSingleValue(self::CREATION_DATE)) { |
|
72
|
|
|
$dt = trim($this->getSingleValue(self::CREATION_DATE) . ' ' . $this->getSingleValue(self::CREATION_TIME)); |
|
73
|
|
|
|
|
74
|
|
|
return new DateTime($dt); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
return null; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* @return array|null|string |
|
82
|
|
|
*/ |
|
83
|
|
|
public function getKeywords() |
|
84
|
|
|
{ |
|
85
|
|
|
return $this->getValue(self::KEYWORDS); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* @return bool |
|
90
|
|
|
*/ |
|
91
|
|
|
public function isUTF8() |
|
92
|
|
|
{ |
|
93
|
|
|
return $this->getSingleValue(self::CHARSET) == "\x1B%G"; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* @return null|string |
|
98
|
|
|
*/ |
|
99
|
|
|
public function getCreator() |
|
100
|
|
|
{ |
|
101
|
|
|
return $this->getSingleValue(self::CAPTION_WRITER); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* @return null|string |
|
106
|
|
|
*/ |
|
107
|
|
|
public function getDescription() |
|
108
|
|
|
{ |
|
109
|
|
|
return $this->getSingleValue(self::CAPTION); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
public function getCopyrights() |
|
113
|
|
|
{ |
|
114
|
|
|
return $this->getSingleValue(self::COPYRIGHT); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* @return array |
|
119
|
|
|
* @throws Exception |
|
120
|
|
|
*/ |
|
121
|
|
|
public function toArray() |
|
122
|
|
|
{ |
|
123
|
|
|
return [ |
|
124
|
|
|
'category' => $this->getCategory(), |
|
125
|
|
|
'created_at' => $this->getCreationDateTime(), |
|
126
|
|
|
'keywords' => $this->getKeywords(), |
|
127
|
|
|
'utf8' => $this->isUTF8(), |
|
128
|
|
|
'created_by' => $this->getCreator(), |
|
129
|
|
|
'description' => $this->getDescription(), |
|
130
|
|
|
'copyrights' => $this->getCopyrights(), |
|
131
|
|
|
]; |
|
132
|
|
|
} |
|
133
|
|
|
} |
|
134
|
|
|
|
If you suppress an error, we recommend checking for the error condition explicitly: