Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 17
0.00% covered (danger)
0.00%
0 / 1
CRAP
0.00% covered (danger)
0.00%
0 / 1
Util
0.00% covered (danger)
0.00%
0 / 17
0.00% covered (danger)
0.00%
0 / 1
56
0.00% covered (danger)
0.00%
0 / 1
 isAnimatedGif
0.00% covered (danger)
0.00%
0 / 17
0.00% covered (danger)
0.00%
0 / 1
56
1<?php
2
3declare(strict_types=1);
4
5namespace Cosray\Assets;
6
7use Cosray\Exception\RuntimeException;
8
9class Util
10{
11    public static function isAnimatedGif(string $fileName): bool
12    {
13        // Check if the file exists
14        if (!file_exists($fileName)) {
15            throw new RuntimeException('File does not exist: ' . $fileName);
16        }
17
18        // Open the file
19        $fileHandle = fopen($fileName, 'rb');
20
21        if (!$fileHandle) {
22            throw new RuntimeException('File could not be opened: ' . $fileName);
23        }
24
25        // Read the first few bytes of the file
26        $header = fread($fileHandle, 3);
27
28        // Close the file handle
29        fclose($fileHandle);
30
31        // Check if the file header matches the GIF magic number
32        if ($header === 'GIF') {
33            $fileHandle = fopen($fileName, 'rb');
34            $frameCount = 0;
35
36            while (!feof($fileHandle) && $frameCount < 2) {
37                $chunk = fread($fileHandle, 1024 * 100); // read 100kb at a time
38                $frameCount += substr_count($chunk, "\x00\x21\xF9\x04");
39
40                if ($frameCount > 1) {
41                    fclose($fileHandle);
42
43                    return true;
44                }
45            }
46        }
47
48        return false;
49    }
50}