简体   繁体   English

PHP 检测文件是否为 webp 图像

[英]PHP detect if file is webp image

I'm trying to check if a file is a webp image in PHP我正在尝试检查文件是否是 PHP 中的 webp 图像

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可以使用在线转换将同一文件成功转换为 jpg,我还可以从字节中看到该文件实际上是一个 webp

RIFF�5WEBPVP8X....

PHP manual : exif_imagetype PHP 手册:exif_imagetype

<?php

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

?>

请试试这个:

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

The GD library throws a lot of warnings, even when it shouldn't do so. GD 库会抛出很多警告,即使它不应该这样做。 I'd use in this particular case the @ operator in front of imagecreatefromwebp()在这种特殊情况下,我会在imagecreatefromwebp()前面使用@运算符

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

Now it will return an image resource identifier on success and boolean false on error.现在它将在成功时返回一个图像资源标识符,在错误时返回布尔值 false。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM