0

I'm trying to check if a file is a webp image in PHP

if (false != imagecreatefromwebp($filename)) {
            //do something
        }

But I get the following error

Warning: imagecreatefromwebp(): 'test.webp' is not a valid WEBP file

Same file can be successfully converted to jpg using online converts and I can also see that the file is actually a webp from the bytes

RIFF�5WEBPVP8X....
cksrc
  • 2,062
  • 3
  • 24
  • 39

3 Answers3

1

PHP manual : exif_imagetype

<?php

if (exif_imagetype($filename) === IMAGETYPE_WEBP) {
    echo 'The picture is webp!';
}

?>
matija kancijan
  • 1,205
  • 10
  • 11
0

Please try this:

$finfo = new finfo(FILEINFO_MIME); echo $finfo->buffer($filename);

Alex Muravyov
  • 502
  • 6
  • 13
-1

The GD library throws a lot of warnings, even when it shouldn't do so. I'd use in this particular case the @ operator in front of imagecreatefromwebp()

<?php
$img = @imagecreatefromwebp($filename);
var_dump($img);

Now it will return an image resource identifier on success and boolean false on error.

Boby
  • 826
  • 5
  • 9