|
11 | 11 |
|
12 | 12 | namespace PHPExif; |
13 | 13 |
|
| 14 | +use PHPExif\Reader\AdapterInterface; |
| 15 | +use PHPExif\Reader\NoAdapterException; |
| 16 | + |
14 | 17 | /** |
15 | 18 | * PHP Exif Reader |
16 | 19 | * |
|
22 | 25 | */ |
23 | 26 | class Reader |
24 | 27 | { |
25 | | - const INCLUDE_THUMBNAIL = true; |
26 | | - const NO_THUMBNAIL = false; |
27 | | - |
28 | | - const SECTIONS_AS_ARRAYS = true; |
29 | | - const SECTIONS_FLAT = false; |
30 | | - |
| 28 | + const TYPE_NATIVE = 'native'; |
| 29 | + |
31 | 30 | /** |
32 | | - * List of EXIF sections |
| 31 | + * The current adapter |
33 | 32 | * |
34 | | - * @var array |
| 33 | + * @var \PHPExif\Reader\AdapterInterface |
35 | 34 | */ |
36 | | - protected $sections = array(); |
37 | | - |
| 35 | + protected $adapter; |
| 36 | + |
38 | 37 | /** |
39 | | - * Include the thumbnail in the EXIF data? |
40 | | - * |
41 | | - * @var boolean |
42 | | - */ |
43 | | - protected $includeThumbnail = self::NO_THUMBNAIL; |
44 | | - |
45 | | - /** |
46 | | - * Parse the sections as arrays? |
47 | | - * |
48 | | - * @var boolean |
49 | | - */ |
50 | | - protected $sectionsAsArrays = self::SECTIONS_FLAT; |
51 | | - |
52 | | - /** |
53 | | - * Contains the mapping of names to IPTC field numbers |
54 | | - * |
55 | | - * @var array |
| 38 | + * Reader constructor |
| 39 | + * |
| 40 | + * @param \PHPExif\Reader\AdapterInterface $adapter |
56 | 41 | */ |
57 | | - protected $iptcMapping = array( |
58 | | - 'title' => '2#005', |
59 | | - 'keywords' => '2#025', |
60 | | - 'copyright' => '2#116', |
61 | | - 'caption' => '2#120', |
62 | | - 'headline' => '2#105', |
63 | | - 'credit' => '2#110', |
64 | | - 'source' => '2#115', |
65 | | - 'jobtitle' => '2#085' |
66 | | - ); |
67 | | - |
68 | | - |
69 | | - /** |
70 | | - * Getter for the EXIF sections |
71 | | - * |
72 | | - * @return array |
73 | | - */ |
74 | | - public function getRequiredSections() |
| 42 | + public function __construct(AdapterInterface $adapter = null) |
75 | 43 | { |
76 | | - return $this->sections; |
| 44 | + if (!is_null($adapter)) { |
| 45 | + $this->setAdapter($adapter); |
| 46 | + } |
77 | 47 | } |
78 | | - |
| 48 | + |
79 | 49 | /** |
80 | | - * Setter for the EXIF sections |
81 | | - * |
82 | | - * @param array $sections List of EXIF sections |
| 50 | + * Setter for the reader adapter |
| 51 | + * |
| 52 | + * @param \PHPExif\Reader\AdapterInterface $adapter |
83 | 53 | * @return \PHPExif\Reader Current instance for chaining |
84 | 54 | */ |
85 | | - public function setRequiredSections(array $sections) |
| 55 | + public function setAdapter(AdapterInterface $adapter) |
86 | 56 | { |
87 | | - $this->sections = $sections; |
88 | | - |
| 57 | + $this->adapter = $adapter; |
| 58 | + |
89 | 59 | return $this; |
90 | 60 | } |
91 | | - |
| 61 | + |
92 | 62 | /** |
93 | | - * Adds an EXIF section to the list |
94 | | - * |
95 | | - * @param string $section |
96 | | - * @return \PHPExif\Reader Current instance for chaining |
| 63 | + * Getter for the reader adapter |
| 64 | + * |
| 65 | + * @return \PHPExif\Reader\AdapterInterface |
| 66 | + * @throws NoAdapterException When no adapter is set |
97 | 67 | */ |
98 | | - public function addRequiredSection($section) |
| 68 | + public function getAdapter() |
99 | 69 | { |
100 | | - if (!in_array($section, $this->sections)) { |
101 | | - array_push($this->sections, $section); |
| 70 | + if (empty($this->adapter)) { |
| 71 | + throw new NoAdapterException('No adapter set in the reader'); |
102 | 72 | } |
103 | | - |
104 | | - return $this; |
| 73 | + |
| 74 | + return $this->adapter; |
105 | 75 | } |
106 | | - |
| 76 | + |
107 | 77 | /** |
108 | | - * Define if the thumbnail should be included into the EXIF data or not |
109 | | - * |
110 | | - * @param boolean $value |
111 | | - * @return \PHPExif\Reader Current instance for chaining |
| 78 | + * Factory for the reader |
| 79 | + * |
| 80 | + * @param string $type |
| 81 | + * @return \PHPExif\Reader |
| 82 | + * @throws \InvalidArgumentException When given type is invalid |
112 | 83 | */ |
113 | | - public function setIncludeThumbnail($value) |
| 84 | + public static function factory($type) |
114 | 85 | { |
115 | | - $this->includeThumbnail = $value; |
116 | | - |
117 | | - return $this; |
| 86 | + $classname = get_called_class(); |
| 87 | + |
| 88 | + switch ($type) { |
| 89 | + case self::TYPE_NATIVE: |
| 90 | + $adapter = new Reader\Adapter\Native(); |
| 91 | + return new $classname($adapter); |
| 92 | + default: |
| 93 | + throw new \InvalidArgumentException( |
| 94 | + sprintf('Unknown type "%1$s"', $type) |
| 95 | + ); |
| 96 | + break; |
| 97 | + } |
118 | 98 | } |
119 | | - |
| 99 | + |
120 | 100 | /** |
121 | 101 | * Reads & parses the EXIF data from given file |
122 | 102 | * |
123 | 103 | * @param string $file |
124 | 104 | * @return \PHPExif\Exif Instance of Exif object with data |
125 | | - * @throws \RuntimeException If the EXIF data could not be read |
126 | 105 | */ |
127 | 106 | public function getExifFromFile($file) |
128 | 107 | { |
129 | | - $sections = $this->getRequiredSections(); |
130 | | - $sections = implode(',', $sections); |
131 | | - $sections = (empty($sections)) ? null : $sections; |
132 | | - |
133 | | - $data = @exif_read_data($file, $sections, $this->sectionsAsArrays, $this->includeThumbnail); |
134 | | - |
135 | | - if (false === $data) { |
136 | | - throw new \RuntimeException( |
137 | | - sprintf('Could not read EXIF data from file %1$s', $file) |
138 | | - ); |
139 | | - } |
140 | | - |
141 | | - $xmpData = $this->getIptcData($file); |
142 | | - $data = array_merge($data, array(Exif::SECTION_IPTC => $xmpData)); |
143 | | - $exif = new Exif($data); |
144 | | - |
145 | | - return $exif; |
146 | | - } |
147 | | - |
148 | | - /** |
149 | | - * Returns an array of IPTC data |
150 | | - * |
151 | | - * @param string $file The file to read the IPTC data from |
152 | | - * @return array |
153 | | - */ |
154 | | - public function getIptcData($file) |
155 | | - { |
156 | | - $size = getimagesize($file, $info); |
157 | | - $arrData = array(); |
158 | | - if(isset($info['APP13'])) { |
159 | | - $iptc = iptcparse($info['APP13']); |
160 | | - |
161 | | - foreach ($this->iptcMapping as $name => $field) { |
162 | | - if (!isset($iptc[$field])) { |
163 | | - continue; |
164 | | - } |
165 | | - |
166 | | - if (count($iptc[$field]) === 1) { |
167 | | - $arrData[$name] = reset($iptc[$field]); |
168 | | - } else { |
169 | | - $arrData[$name] = $iptc[$field]; |
170 | | - } |
171 | | - } |
172 | | - } |
173 | | - |
174 | | - return $arrData; |
| 108 | + return $this->getAdapter()->getExifFromFile($file); |
175 | 109 | } |
176 | 110 | } |
0 commit comments