Skip to content

Commit 4b8a44d

Browse files
committed
Merge pull request #4 from Miljar/issues/3
Issues/3
2 parents d7cbee5 + ce6e819 commit 4b8a44d

File tree

11 files changed

+479
-151
lines changed

11 files changed

+479
-151
lines changed

composer.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@
1414
"php": ">=5.3.0"
1515
},
1616
"require-dev": {
17-
"phpunit/phpunit": "3.7.*",
18-
"phing/phing": "2.4.*"
17+
"phpunit/phpunit": "3.7.*"
1918
},
2019
"autoload": {
2120
"psr-0":

lib/PHPExif/Reader.php

Lines changed: 54 additions & 120 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111

1212
namespace PHPExif;
1313

14+
use PHPExif\Reader\AdapterInterface;
15+
use PHPExif\Reader\NoAdapterException;
16+
1417
/**
1518
* PHP Exif Reader
1619
*
@@ -22,155 +25,86 @@
2225
*/
2326
class Reader
2427
{
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+
3130
/**
32-
* List of EXIF sections
31+
* The current adapter
3332
*
34-
* @var array
33+
* @var \PHPExif\Reader\AdapterInterface
3534
*/
36-
protected $sections = array();
37-
35+
protected $adapter;
36+
3837
/**
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
5641
*/
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)
7543
{
76-
return $this->sections;
44+
if (!is_null($adapter)) {
45+
$this->setAdapter($adapter);
46+
}
7747
}
78-
48+
7949
/**
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
8353
* @return \PHPExif\Reader Current instance for chaining
8454
*/
85-
public function setRequiredSections(array $sections)
55+
public function setAdapter(AdapterInterface $adapter)
8656
{
87-
$this->sections = $sections;
88-
57+
$this->adapter = $adapter;
58+
8959
return $this;
9060
}
91-
61+
9262
/**
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
9767
*/
98-
public function addRequiredSection($section)
68+
public function getAdapter()
9969
{
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');
10272
}
103-
104-
return $this;
73+
74+
return $this->adapter;
10575
}
106-
76+
10777
/**
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
11283
*/
113-
public function setIncludeThumbnail($value)
84+
public static function factory($type)
11485
{
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+
}
11898
}
119-
99+
120100
/**
121101
* Reads & parses the EXIF data from given file
122102
*
123103
* @param string $file
124104
* @return \PHPExif\Exif Instance of Exif object with data
125-
* @throws \RuntimeException If the EXIF data could not be read
126105
*/
127106
public function getExifFromFile($file)
128107
{
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);
175109
}
176110
}

0 commit comments

Comments
 (0)