Skip to content

Commit 2295875

Browse files
committed
Merge pull request #16 from wasinger/master
add support for getting Orientation tag
2 parents afa6d42 + ee4791d commit 2295875

File tree

4 files changed

+81
-0
lines changed

4 files changed

+81
-0
lines changed

lib/PHPExif/Exif.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ class Exif
4141
const JOB_TITLE = 'jobTitle';
4242
const KEYWORDS = 'keywords';
4343
const MIMETYPE = 'MimeType';
44+
const ORIENTATION = 'Orientation';
4445
const SOFTWARE = 'software';
4546
const SOURCE = 'source';
4647
const TITLE = 'title';
@@ -438,4 +439,40 @@ public function getFileSize()
438439

439440
return $this->data[self::FILESIZE];
440441
}
442+
443+
/**
444+
* Returns the orientation, if it exists
445+
*
446+
* @return integer
447+
*/
448+
public function getOrientation()
449+
{
450+
if (!isset($this->data[self::ORIENTATION])) {
451+
return false;
452+
}
453+
454+
$value = $this->data[self::ORIENTATION];
455+
456+
// normalize exiftool strings to raw integer values
457+
switch ($value) {
458+
case 'Horizontal (normal)':
459+
return 1;
460+
case 'Mirror horizontal':
461+
return 2;
462+
case 'Rotate 180':
463+
return 3;
464+
case 'Mirror vertical':
465+
return 4;
466+
case 'Mirror horizontal and rotate 270 CW':
467+
return 5;
468+
case 'Rotate 90 CW':
469+
return 6;
470+
case 'Mirror horizontal and rotate 90 CW':
471+
return 7;
472+
case 'Rotate 270 CW':
473+
return 8;
474+
default:
475+
return $value;
476+
}
477+
}
441478
}

lib/PHPExif/Reader/Adapter/Exiftool.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,7 @@ public function mapData(array $source)
166166
Exif::JOB_TITLE => false,
167167
Exif::KEYWORDS => (!isset($source['Keywords'])) ? false : $source['Keywords'],
168168
Exif::MIMETYPE => false,
169+
Exif::ORIENTATION => (!isset($source['Orientation'])) ? false : $source['Orientation'],
169170
Exif::SOFTWARE => (!isset($source['Software'])) ? false : $source['Software'],
170171
Exif::SOURCE => false,
171172
Exif::TITLE => (!isset($source['Title'])) ? false : $source['Title'],

lib/PHPExif/Reader/Adapter/Native.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,7 @@ public function mapData(array $source)
271271
Exif::JOB_TITLE => (!isset($source[self::SECTION_IPTC]['jobtitle'])) ? false : $source[self::SECTION_IPTC]['jobtitle'],
272272
Exif::KEYWORDS => (!isset($source[self::SECTION_IPTC]['keywords'])) ? false : $source[self::SECTION_IPTC]['keywords'],
273273
Exif::MIMETYPE => (!isset($source[Exif::MIMETYPE]) ? false : $source[Exif::MIMETYPE]),
274+
Exif::ORIENTATION => (!isset($source[Exif::ORIENTATION]) ? false : $source[Exif::ORIENTATION]),
274275
Exif::SOFTWARE => (!isset($source['Software'])) ? false : $source['Software'],
275276
Exif::SOURCE => (!isset($source[self::SECTION_IPTC]['source'])) ? false : $source[self::SECTION_IPTC]['source'],
276277
Exif::TITLE => (!isset($source[self::SECTION_IPTC]['title'])) ? false : $source[self::SECTION_IPTC]['title'],

tests/PHPExif/ExifTest.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -385,4 +385,46 @@ public function testGetFileSize()
385385
$this->exif->setRawData($data);
386386
$this->assertEquals($expected, $this->exif->getFileSize());
387387
}
388+
389+
public function testGetOrientation()
390+
{
391+
$expected = 1;
392+
$data[\PHPExif\Exif::ORIENTATION] = $expected;
393+
$this->exif->setRawData($data);
394+
$this->assertEquals($expected, $this->exif->getOrientation());
395+
396+
// test normalization of exiftool strings to raw integer values
397+
$expected = 1;
398+
$data[\PHPExif\Exif::ORIENTATION] = 'Horizontal (normal)';
399+
$this->exif->setRawData($data);
400+
$this->assertEquals($expected, $this->exif->getOrientation());
401+
$expected = 2;
402+
$data[\PHPExif\Exif::ORIENTATION] = 'Mirror horizontal';
403+
$this->exif->setRawData($data);
404+
$this->assertEquals($expected, $this->exif->getOrientation());
405+
$expected = 3;
406+
$data[\PHPExif\Exif::ORIENTATION] = 'Rotate 180';
407+
$this->exif->setRawData($data);
408+
$this->assertEquals($expected, $this->exif->getOrientation());
409+
$expected = 4;
410+
$data[\PHPExif\Exif::ORIENTATION] = 'Mirror vertical';
411+
$this->exif->setRawData($data);
412+
$this->assertEquals($expected, $this->exif->getOrientation());
413+
$expected = 5;
414+
$data[\PHPExif\Exif::ORIENTATION] = 'Mirror horizontal and rotate 270 CW';
415+
$this->exif->setRawData($data);
416+
$this->assertEquals($expected, $this->exif->getOrientation());
417+
$expected = 6;
418+
$data[\PHPExif\Exif::ORIENTATION] = 'Rotate 90 CW';
419+
$this->exif->setRawData($data);
420+
$this->assertEquals($expected, $this->exif->getOrientation());
421+
$expected = 7;
422+
$data[\PHPExif\Exif::ORIENTATION] = 'Mirror horizontal and rotate 90 CW';
423+
$this->exif->setRawData($data);
424+
$this->assertEquals($expected, $this->exif->getOrientation());
425+
$expected = 8;
426+
$data[\PHPExif\Exif::ORIENTATION] = 'Rotate 270 CW';
427+
$this->exif->setRawData($data);
428+
$this->assertEquals($expected, $this->exif->getOrientation());
429+
}
388430
}

0 commit comments

Comments
 (0)