phpexifwebpxmp

php support for WEBP image metadata


Does php support webp image metadata?

Specifically, I want to be able to read and write XMP and EXIF metadata for webp images natively in php code.

I have been experimenting with the below code and it is giving me a "File not supported in" warning.

<?php

$photoSourceThumbnail = "publicAssets/images/att_galleryWebP/A0001_LSF-PHOTOS-WM-TM-WEBP/A0001-EWF-LSF-01.webp";
$photoSourceFull = "assets/images/att_galleryWebP/A0001_LSF-PHOTOS-WM-FULL-WEBP/A0001-EWF-LSF-01.webp";

echo "$photoSourceFull:<br />\n";
$exif = exif_read_data($photoSourceFull, 'IFD0');
echo $exif===false ? "No header data found.<br />\n" : "Image contains headers<br />\n";

$exif = exif_read_data($photoSourceFull, 0, true);
echo "test2.jpg:<br />\n";
foreach ($exif as $key => $section) {
    foreach ($section as $name => $val) {
        echo "$key.$name: $val<br />\n";
    }
} 

Solution

  • It is better to use ExifTool for this.

    Install ExifTool

    https://exiftool.org/

    PHP example

    class ExifToolException extends RuntimeException{}
    
    function getInfo(string $file) : object 
    {
        $info = shell_exec('exiftool -json ' . escapeshellarg($file) . ' 2>&1');
        if(strpos($info, 'Error:') > -1) {
            throw new ExifToolException(rtrim($info, PHP_EOL));
        }
        return json_decode($info)[0];
    }
    
    try {
        var_dump(getInfo('abc.webp')->Megapixels);
    } catch(ExifToolException $e) {
        var_dump($e->getMessage());
    }
    

    Update: Exiftool added WebP support as of version 12.46, Oct. 1, 2022