views:

29

answers:

3

I've taken over work for the custom forums that a previous developer created at my job, and came across a minor bug. We don't allow animated GIFs as avatars in the forums. However, if someone were to take an animated GIF, rename it to imagename.jpeg, and then upload it, the forums will show the image as being animated.

It's a very odd bug as I didn't even think it played the animation if the extension wasn't .gif.

So my question is: Is there a way to check (in .NET 2.0) if an image is an animated gif, even if the extension isn't?

Bara

A: 

I am not sure if this works, but check last post:

http://forums.asp.net/p/1188302/2033730.aspx

meep
+2  A: 

You could open the file and analyze its header to see if it is a GIF. According to http://www.onicos.com/staff/iz/formats/gif.html, the first 3 bytes of the header should be "GIF".

Justin Ethier
That'll do, thank you :)
Bara
A: 

Here's a snippet of PHP (snagged from the PHP forums) that does exactly what you're looking for. Converting it to .NET should be pretty simple.

function is_animated_gif($filename)
{
    $filecontents=file_get_contents($filename);

    $str_loc=0;
    $count=0;
    while ($count < 2) 
    {
            $where1=strpos($filecontents,"\x00\x21\xF9\x04",$str_loc);
            if ($where1 === FALSE)
            {
                    break;
            }
            else
            {
                    $str_loc=$where1+1;
                    $where2=strpos($filecontents,"\x00\x2C",$str_loc);
                    if ($where2 === FALSE)
                    {
                            break;
                    }
                    else
                    {
                            if ($where1+8 == $where2)
                            {
                                    $count++;
                            }
                            $str_loc=$where2+1;
                    }
            }
    }

    if ($count > 1)
    {
            return(true);
    }
    else
    {
            return(false);
    }
}
jvenema