Home > Mobile >  php support for WEBP image metadata
php support for WEBP image metadata

Time:11-25

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";
    }
} 

CodePudding user response:

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 " . $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 does not support writing webp

Instead you can look at webpmux from Google: https://developers.google.com/speed/webp/docs/webpmux

CodePudding user response:

I am doing SEO tasks and looking at what images are getting indexed and with what meta data.

So far my website has 6000 images which I converted from jpg to webp using this online tool. https://nsspot.herokuapp.com/imagetowebp/

The jpg images had full XMP and EXIF metadata manually added for each one however the conversion tool did not pass over from jpg to webp any of the metadata.

Additionally I am looking at what is optimal metadata for indexing images. https://developers.google.com/search/docs/advanced/guidelines/google-images

I mean if webp is supposed to be for images on the web then surely the metadata fields should be: image filename image title image description image keywords image latitude image longitude

My conclusions so far are that while webp format does improve pagespeed there seems to be a lack of though on the part of google about clearly defining the metadata standards for webp images which are supposed to be optimised for a web environment.

moving forward I would like to find a means that can take a row from a sql table of image metadata values and write it to the image and repeat 6000 times for all my images.

if this does not seam easily possible I am considering rolling back from webp to jpg till such a time that google have sorted out the standards.

  • Related