The function `isWebpAnimated` checks whether a given WebP image file...
The function isWebpAnimated
checks whether a given WebP image file is an animated WebP or not. Here's a detailed breakdown of what the code does:
-
Input and Result Initialization:
- The function accepts a single argument,
$fn
, which is the file path to a WebP image. - It initializes a variable
$result
tofalse
. This will later hold whether the WebP file is animated or not.
- The function accepts a single argument,
-
Open the File:
- The file specified by
$fn
is opened in binary read mode usingfopen
.
- The file specified by
-
Check for WebP Extended Header:
-
Using
fseek
, the file pointer is moved to the 12th byte. -
It reads 4 bytes starting from this position using
fread
. For a WebP file, these 4 bytes will tell whether the file uses the "extended" WebP format (indicated by the stringVP8X
). -
If the 4 bytes equal
VP8X
, the file uses the extended WebP format, in which case further checks are performed.
-
-
Check the Animation Flag:
-
If the file is a WebP extended format (
VP8X
), the function moves the file pointer to the 20th byte usingfseek
. -
It reads 1 byte from this position using
fread
. This byte contains a series of bits, one of which (the second bit) is the animation flag. -
The function extracts this flag by:
- Using
ord($myByte)
to convert the byte to an integer. - Shifting right by 1 bit (
>> 1
) to target the second bit. - Using bitwise AND (
& 1
) to isolate whether the second bit is set.
- Using
-
If the second bit is set, it means the WebP file is animated, and
$result
is set totrue
.
-
-
Close the File:
- The function closes the file using
fclose
, ensuring no file resource leaks.
- The function closes the file using
-
Return the Result:
- The function returns the value of
$result
, which istrue
if the WebP file is animated andfalse
otherwise.
- The function returns the value of
Summary:
This function determines whether a WebP file is an animated WebP by checking the animation flag in the file's header if the file uses the extended WebP format (VP8X
). It does this by reading specific bytes from the file, interpreting the format, and checking the relevant flag bit.