經過大量的調查後,我發現動畫webp
圖像始終conatins一些字符串,當在文本編輯器中打開和非動畫圖像不打開。字符串是ANMF
和ANIM
。我在所有的webp圖像中檢查了這些字符串。所以這對我來說是完美的。以下是PHP
,Javascript
和Shell Script
一些解決方案:
在PHP:
<?php
function isWebpAnimated($src){
$webpContents = file_get_contents($src);
$where = strpos($webpContents, "ANMF");
if ($where !== FALSE){
// animated
$isAnimated = true;
}
else{
// non animated
$isAnimated = false;
}
return $isAnimated;
}
?>
在Javascript中:
function isAnimatedGif(src) {
var request = new XMLHttpRequest();
request.open('GET', src, true);
request.addEventListener('load', function() {
if(request.response.indexOf("ANMF") != -1){
// animated
alert(true);
}
else{
// non animated
alert(false);
}
});
request.send();
}
但是在大型圖像PHP
和Javascript
的情況都不盡如人意,所以最好解決方法是使用Shell Script
,如果你有Ubuntu
。
在Shell腳本:
echo $(grep -c "ANMF" ~/animated.webp)
則返回0無動畫,否則不爲零的動畫。
據我所知它只有GIF可以動畫 – Andreas
如果^^是正確的,你可以使用這段代碼從PHP手冊https://secure.php.net/manual/en/function.imagecreatefromgif.php#59787 – Andreas
這段代碼只檢查'gif'而不檢查'webp'。 'Webp'也可以動畫,請使用Chrome瀏覽器檢查這個http://cloudinary.com/blog/animated_webp_how_to_convert_animated_gif_to_webp_and_save_up_to_90_bandwidth –