View Full Version : Is this a mistake in code to check for animated GIF?

11-18-2010, 08:05 PM
I wish to use the code from the following page that can be used to check if an uploaded image is an animated GIF:

Here is the function:

function is_ani($filename) {
if(!($fh = @fopen($filename, 'rb')))
return false;
$count = 0;
//an animated gif contains multiple "frames", with each frame having a
//header made up of:
// * a static 4-byte sequence (\x00\x21\xF9\x04)
// * 4 variable bytes
// * a static 2-byte sequence (\x00\x2C)

// We read through the file til we reach the end of the file, or we've found
// at least 2 frame headers
while(!feof($fh) && $count < 2)
$chunk = fread($fh, 1024 * 100); //read 100kb at a time
$count += preg_match_all('#\x00\x21\xF9\x04.{4}\x00\x2C#s', $chunk, $matches);

return $count > 1;

Am I right in saying that there are curly brackets missing after the while line? The indenting indicates that both of the 2 following lines should be included in the while loop, but the lack of curly brackets mean that only the line beginning with $chunk is included in the loop.

The code actually seems to work with and without the curly brackets. Does anyone understand this code well enough to know whether or not the curly brackets should be included?

Thanks in advance.

11-18-2010, 10:18 PM
It seems to me like you would need the brackets. Is it possible it is working fine either way for you b/c you are processing files that are less than 100kb?

I agree that the indentation is misleading if you don't need them. I don't see how the second line is possibly being executed in a file that is larger than 100kb....

11-19-2010, 05:20 PM
Thanks tjfoz. You are right, the animated GIFs I was using were less than 100kb, which is why it appeared to work either way. I will make sure the brackets are added.