Extract exif data using php

I am planning to add a list of useful exif data to my gallery. I searched for some tools and came across phpexiftool.

This library is very advanced, but is missing some simplicity. This is why I created an exif helper.

composer.json

{
    "require": {
        "phpexiftool/phpexiftool": "0.3.0",
        "symfony/console": "2.3.*@dev"
    }
}

ExifHelper.php

<?php
class ExifHelper
{
    protected $exifData = array();

    public function __construct(PHPExiftool\Driver\Metadata\MetadataBag $metaData)
    {
        $regex = array(
            'File:',
            'ExifIFD:',
            'IFD0:',
        );
        foreach ($regex as $reg) {
            foreach ($metaData->filterKeysByRegExp('/' . $reg . '/') as $exif) {
                if ($values = $exif->getTag()->getValues()) {
                    $this->exifData[$exif->getTag()->getName()] = $values[$exif->getValue()->asString()]['Label'];
                }
                else {
                    $this->exifData[$exif->getTag()->getName()] = $exif->getValue()->asString();
                }
            }
        }
    }

    public function __call($name, $args)
    {
        $name = substr($name, 3);
        if (isset($this->exifData[$name])) {
            if (strpos($name, 'Date') !== false) {
                //return new \DateTime($this->exifData[$name]);
            }
            return $this->exifData[$name];
        }
        return null;
    }

    public function getExposureTime()
    {
        return '1/' . (1 / floatval($this->exifData['ExposureTime']));
    }

    public function getShutterSpeedValue()
    {
        return '1/' . (1 / floatval($this->exifData['ShutterSpeedValue']));
    }

    public function getMaxApertureValue($real = false)
    {
        return round(floatval($this->exifData['MaxApertureValue']) - 0.05, 1);
    }

    public function asArray()
    {
        $array = array();
        foreach ($this->exifData as $name => $value) {
            $array[$name] = call_user_func(array($this, 'get' . $name));
        }
        return $array;
    }
}

test.php

<?php
use Monolog\Logger;
use PHPExiftool\Reader;

require_once __DIR__ . '/vendor/autoload.php';
require_once __DIR__ . '/ExifHelper.php';

$logger = new Logger('exiftool');
$Reader = Reader::create($logger)->files(array(
    __DIR__ . '/images/IMG_0001.jpg',
    __DIR__ . '/images/IMG_0002.jpg',
));

echo '<h1>exif data</h1>';
foreach ($Reader as $MetaDatas) {
    echo "<h2>" . $MetaDatas->getFile() . "</h2>";
    $data = new ExifHelper($MetaDatas->getMetadatas());
    echo '<table>';
    foreach ($data->asArray() as $key => $value) {
        echo sprintf('<tr><th>%s</th><td>%s</td></tr>', $key, $value);
    }
    echo '</table>';
}


Fork me on GitHub