Detecting WEBP format and dimensions of picture (width and height of image) with PHP, without libraries
  
Author:
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);
}
TagsTags:  php, webp

Send by E-mailSend by E-mail   Print versionPrint version
Comments(0)

No comments yet… Be the first to leave comment on this topic!

or
You may sign in using:
Enter with Facebook Enter with Google Enter with VK