“Favorites of Success” logo Detecting WEBP format and dimensions of picture (width and height of image) with PHP, without libraries
© FAVOR.com.ua
 
Detecting WEBP format and dimensions of picture (width and height of image) with PHP, without libraries
  
function is_webp($hdr) {
  return substr($hdr, 0, 4) == 'RIFF' &&
         substr($hdr, 8, 4) == 'WEBP';
}
// Getting width and height of WEBP image PHP's GD lib. Works even in PHP lower than v5.5.
function get_webp_size($fn) {
  $w = 0;
  $h = 0;
  if ($f = @fopen($fn, 'rb')) {
    $hdr = fread($f, 32);
    fclose($f);
    if (strlen($hdr) >= 30) {
      $w = unpack('C3b', substr($hdr, 24, 3));
      $w = ($w['b1'] + $w['b2']*256 + $w['b3']*65536) + 1;
      $h = unpack('C3b', substr($hdr, 27, 3));
      $h = ($h['b1'] + $h['b2']*256 + $h['b3']*65536) + 1;
    }
  }
  return array($w, $h);
}
https://favor.com.ua/en/blogs/30962.html