Skip to content

no chance to retrieve the error via Throwable in exif_read_data #176

@alpham8

Description

@alpham8

From manual page: https://php.net/function.exif-read-data


I got a JPG picture that has indeed a wrong color profile. So the function exif_read_data throws an E_WARNING.

But it's impossible to retrieve the error message using try / catch block like this:

try {
    $data = exif_read_data(
        __DIR__.'/mypicture.jpg',
        'ANY_TAG'
    );
} catch (Throwable $e) {
    echo 'Error retrieved: ' . $e->getMessage() . PHP_EOL;
}

The only way to retrieve the error message was like this:

$data = @exif_read_data(__DIR__.'/mypicture.jpg', 'ANY_TAG');
$error = error_get_last();
if (
    false === $data
    && is_array($error)
    && in_array($error['type'], [E_WARNING, E_NOTICE])
    && $error['file'] === __FILE__
    && mb_stripos($error['message'], 'exif_read_data') !== false
) {
    echo 'error found in picture metadata: ' . $error['message'] . PHP_EOL;
} else {
    echo 'metadata verified successfully' . PHP_EOL;
}

In the attachment you'll find the faulty test picture.

I've tested it on cli with:

php -v
PHP 8.1.27 (cli) (built: Dec 21 2023 20:17:59) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.1.27, Copyright (c) Zend Technologies
    with Zend OPcache v8.1.27, Copyright (c), by Zend Technologies

Since there's no new changelog, I assume this issue is still present in latest PHP version also.
mypicture

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions