Skip to content

Commit 3790c81

Browse files
committed
Merge pull request #17 from wasinger/master
unify output from Native and Exiftool adapters
2 parents 2295875 + 330dcdd commit 3790c81

File tree

2 files changed

+68
-13
lines changed

2 files changed

+68
-13
lines changed

lib/PHPExif/Reader/Adapter/Exiftool.php

Lines changed: 39 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,19 @@
2828
class Exiftool extends AdapterAbstract
2929
{
3030
const TOOL_NAME = 'exiftool';
31-
31+
3232
/**
3333
* Path to the exiftool binary
3434
*
3535
* @var string
3636
*/
3737
protected $toolPath;
3838

39+
/**
40+
* @var boolean
41+
*/
42+
protected $numeric = true;
43+
3944
/**
4045
* Setter for the exiftool binary path
4146
*
@@ -58,6 +63,14 @@ public function setToolPath($path)
5863

5964
return $this;
6065
}
66+
67+
/**
68+
* @param boolean $numeric
69+
*/
70+
public function setNumeric($numeric)
71+
{
72+
$this->numeric = $numeric;
73+
}
6174

6275
/**
6376
* Getter for the exiftool binary path
@@ -86,9 +99,10 @@ public function getExifFromFile($file)
8699
{
87100
$result = $this->getCliOutput(
88101
sprintf(
89-
'%1$s -j %2$s',
102+
'%1$s%3$s -j %2$s',
90103
$this->getToolPath(),
91-
$file
104+
$file,
105+
$this->numeric ? ' -n' : ''
92106
)
93107
);
94108

@@ -145,30 +159,42 @@ public function mapData(array $source)
145159
$focalLengthParts = explode(' ', $source['FocalLength']);
146160
$focalLength = (int) reset($focalLengthParts);
147161
}
162+
163+
$exposureTime = false;
164+
if (isset($source['ExposureTime'])) {
165+
$exposureTime = '1/' . round(1 / $source['ExposureTime']);
166+
}
167+
168+
$caption = false;
169+
if (isset($source['Caption'])) {
170+
$caption = $source['Caption'];
171+
} elseif (isset($source['Caption-Abstract'])) {
172+
$caption = $source['Caption-Abstract'];
173+
}
148174

149175
return array(
150176
Exif::APERTURE => (!isset($source['Aperture'])) ? false : sprintf('f/%01.1f', $source['Aperture']),
151-
Exif::AUTHOR => false,
177+
Exif::AUTHOR => (!isset($source['Artist'])) ? false : $source['Artist'],
152178
Exif::CAMERA => (!isset($source['Model'])) ? false : $source['Model'],
153-
Exif::CAPTION => false,
179+
Exif::CAPTION => $caption,
154180
Exif::COLORSPACE => (!isset($source[Exif::COLORSPACE]) ? false : $source[Exif::COLORSPACE]),
155-
Exif::COPYRIGHT => false,
181+
Exif::COPYRIGHT => (!isset($source['Copyright'])) ? false : $source['Copyright'],
156182
Exif::CREATION_DATE => (!isset($source['CreateDate'])) ? false : DateTime::createFromFormat('Y:m:d H:i:s', $source['CreateDate']),
157-
Exif::CREDIT => false,
158-
Exif::EXPOSURE => (!isset($source['ShutterSpeed'])) ? false : $source['ShutterSpeed'],
159-
Exif::FILESIZE => false,
183+
Exif::CREDIT => (!isset($source['Credit'])) ? false : $source['Credit'],
184+
Exif::EXPOSURE => $exposureTime,
185+
Exif::FILESIZE => (!isset($source[Exif::FILESIZE]) ? false : $source[Exif::FILESIZE]),
160186
Exif::FOCAL_LENGTH => $focalLength,
161187
Exif::FOCAL_DISTANCE => (!isset($source['ApproximateFocusDistance'])) ? false : sprintf('%1$sm', $source['ApproximateFocusDistance']),
162-
Exif::HEADLINE => false,
188+
Exif::HEADLINE => (!isset($source['Headline'])) ? false : $source['Headline'],
163189
Exif::HEIGHT => (!isset($source['ImageHeight'])) ? false : $source['ImageHeight'],
164190
Exif::HORIZONTAL_RESOLUTION => (!isset($source['XResolution'])) ? false : $source['XResolution'],
165191
Exif::ISO => (!isset($source['ISO'])) ? false : $source['ISO'],
166-
Exif::JOB_TITLE => false,
192+
Exif::JOB_TITLE => (!isset($source['JobTitle'])) ? false : $source['JobTitle'],
167193
Exif::KEYWORDS => (!isset($source['Keywords'])) ? false : $source['Keywords'],
168-
Exif::MIMETYPE => false,
194+
Exif::MIMETYPE => (!isset($source['MIMEType'])) ? false : $source['MIMEType'],
169195
Exif::ORIENTATION => (!isset($source['Orientation'])) ? false : $source['Orientation'],
170196
Exif::SOFTWARE => (!isset($source['Software'])) ? false : $source['Software'],
171-
Exif::SOURCE => false,
197+
Exif::SOURCE => (!isset($source['Source'])) ? false : $source['Source'],
172198
Exif::TITLE => (!isset($source['Title'])) ? false : $source['Title'],
173199
Exif::VERTICAL_RESOLUTION => (!isset($source['YResolution'])) ? false : $source['YResolution'],
174200
Exif::WIDTH => (!isset($source['ImageWidth'])) ? false : $source['ImageWidth'],

tests/PHPExif/ExifTest.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -427,4 +427,33 @@ public function testGetOrientation()
427427
$this->exif->setRawData($data);
428428
$this->assertEquals($expected, $this->exif->getOrientation());
429429
}
430+
431+
/**
432+
* Test that the values returned by both adapters are equal
433+
*/
434+
public function testAdapterConsistency()
435+
{
436+
$reflClass = new \ReflectionClass('\PHPExif\Exif');
437+
$methods = $reflClass->getMethods(ReflectionMethod::IS_PUBLIC);
438+
$file = PHPEXIF_TEST_ROOT . '/files/morning_glory_pool_500.jpg';
439+
440+
$adapter_exiftool = new \PHPExif\Reader\Adapter\Exiftool();
441+
$adapter_native = new \PHPExif\Reader\Adapter\Native();
442+
443+
$result_exiftool = $adapter_exiftool->getExifFromFile($file);
444+
$result_native = $adapter_native->getExifFromFile($file);
445+
446+
// find all Getter methods on the results and compare its output
447+
foreach ($methods as $method) {
448+
$name = $method->getName();
449+
if (strpos($name, 'get') !== 0 || $name == 'getRawData') {
450+
continue;
451+
}
452+
$this->assertEquals(
453+
call_user_func(array($result_native, $name)),
454+
call_user_func(array($result_exiftool, $name)),
455+
'Adapter difference in method ' . $name
456+
);
457+
}
458+
}
430459
}

0 commit comments

Comments
 (0)