Skip to content

Commit f411c27

Browse files
authored
Merge pull request #50 from FrivalszkyP/master
UTF-8 checking and encoding param support for exiftool
2 parents 002d07f + 2acac64 commit f411c27

File tree

3 files changed

+76
-3
lines changed

3 files changed

+76
-3
lines changed

lib/PHPExif/Adapter/Exiftool.php

+44-3
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,11 @@ class Exiftool extends AdapterAbstract
3939
*/
4040
protected $numeric = true;
4141

42+
/**
43+
* @var array
44+
*/
45+
protected $encoding = array();
46+
4247
/**
4348
* @var string
4449
*/
@@ -75,6 +80,26 @@ public function setNumeric($numeric)
7580
$this->numeric = $numeric;
7681
}
7782

83+
/**
84+
* @see http://www.sno.phy.queensu.ca/~phil/exiftool/faq.html#Q10
85+
* @param array $encoding encoding parameters in an array eg. ["exif" => "UTF-8"]
86+
*/
87+
public function setEncoding($encoding)
88+
{
89+
$possible_keys = array("exif", "iptc", "id3", "photoshop", "quicktime",);
90+
$possible_values = array("UTF8", "cp65001", "UTF-8", "Thai", "cp874", "Latin", "cp1252",
91+
"Latin1", "MacRoman", "cp10000", "Mac", "Roman", "Latin2", "cp1250", "MacLatin2",
92+
"cp10029", "Cyrillic", "cp1251", "Russian", "MacCyrillic", "cp10007", "Greek",
93+
"cp1253", "MacGreek", "cp10006", "Turkish", "cp1254", "MacTurkish", "cp10081",
94+
"Hebrew", "cp1255", "MacRomanian", "cp10010", "Arabic", "cp1256", "MacIceland",
95+
"cp10079", "Baltic", "cp1257", "MacCroatian", "cp10082", "Vietnam", "cp1258",);
96+
foreach ($encoding as $type => $encoding) {
97+
if (in_array($type, $possible_keys) && in_array($encoding, $possible_values)) {
98+
$this->encoding[$type] = $encoding;
99+
}
100+
}
101+
}
102+
78103
/**
79104
* Getter for the exiftool binary path
80105
* Lazy loads the "default" path
@@ -100,17 +125,33 @@ public function getToolPath()
100125
*/
101126
public function getExifFromFile($file)
102127
{
128+
$encoding = '';
129+
if (!empty($this->encoding)) {
130+
$encoding = '-charset ';
131+
foreach ($this->encoding as $key => $value) {
132+
$encoding .= escapeshellarg($key).'='.escapeshellarg($value);
133+
}
134+
}
103135
$result = $this->getCliOutput(
104136
sprintf(
105-
'%1$s%3$s -j -a -G1 -c %4$s %2$s',
137+
'%1$s%3$s -j -a -G1 %5$s -c %4$s %2$s',
106138
$this->getToolPath(),
107139
escapeshellarg($file),
108140
$this->numeric ? ' -n' : '',
109-
escapeshellarg('%d deg %d\' %.4f"')
141+
escapeshellarg('%d deg %d\' %.4f"'),
142+
$encoding
110143
)
111144
);
112145

113-
$data = json_decode(utf8_encode($result), true);
146+
if (!mb_check_encoding($result, "utf-8")) {
147+
$result = utf8_encode($result);
148+
}
149+
$data = json_decode($result, true);
150+
if (!is_array($data)) {
151+
throw new RuntimeException(
152+
'Could not decode exiftool output'
153+
);
154+
}
114155

115156
// map the data:
116157
$mapper = $this->getMapper();

tests/PHPExif/Adapter/ExiftoolTest.php

+32
Original file line numberDiff line numberDiff line change
@@ -78,13 +78,45 @@ public function testSetNumericInProperty()
7878
$this->assertEquals($expected, $reflProperty->getValue($this->adapter));
7979
}
8080

81+
/**
82+
* @see URI http://www.sno.phy.queensu.ca/~phil/exiftool/faq.html#Q10
83+
* @group exiftool
84+
* @covers \PHPExif\Adapter\Exiftool::setEncoding
85+
*/
86+
public function testSetEncodingInProperty()
87+
{
88+
$reflProperty = new \ReflectionProperty('\PHPExif\Adapter\Exiftool', 'encoding');
89+
$reflProperty->setAccessible(true);
90+
91+
$expected = array('iptc' => 'cp1250');
92+
$input = array('iptc' => 'cp1250', 'exif' => 'utf8', 'foo' => 'bar');
93+
$this->adapter->setEncoding($input);
94+
95+
$this->assertEquals($expected, $reflProperty->getValue($this->adapter));
96+
}
97+
8198
/**
8299
* @group exiftool
83100
* @covers \PHPExif\Adapter\Exiftool::getExifFromFile
84101
*/
85102
public function testGetExifFromFile()
86103
{
87104
$file = PHPEXIF_TEST_ROOT . '/files/morning_glory_pool_500.jpg';
105+
$this->adapter->setOptions(array('encoding' => array('iptc' => 'cp1252')));
106+
$result = $this->adapter->getExifFromFile($file);
107+
$this->assertInstanceOf('\PHPExif\Exif', $result);
108+
$this->assertInternalType('array', $result->getRawData());
109+
$this->assertNotEmpty($result->getRawData());
110+
}
111+
112+
/**
113+
* @group exiftool
114+
* @covers \PHPExif\Adapter\Exiftool::getExifFromFile
115+
*/
116+
public function testGetExifFromFileWithUtf8()
117+
{
118+
$file = PHPEXIF_TEST_ROOT . '/files/utf8.jpg';
119+
$this->adapter->setOptions(array('encoding' => array('iptc' => 'utf8')));
88120
$result = $this->adapter->getExifFromFile($file);
89121
$this->assertInstanceOf('\PHPExif\Exif', $result);
90122
$this->assertInternalType('array', $result->getRawData());

tests/files/utf8.jpg

349 KB
Loading

0 commit comments

Comments
 (0)