Can I detect animated gifs using php and gd?

后端 未结 6 1317
旧巷少年郎
旧巷少年郎 2020-12-02 12:54

I\'m currently running into some issues resizing images using GD.

Everything works fine until i want to resize an animated gif, which delivers the first frame on a b

6条回答
  •  没有蜡笔的小新
    2020-12-02 13:58

    Here's the working function:

    /**
     * Thanks to ZeBadger for original example, and Davide Gualano for pointing me to it
     * Original at http://it.php.net/manual/en/function.imagecreatefromgif.php#59787
     **/
    function is_animated_gif( $filename )
    {
        $raw = file_get_contents( $filename );
    
        $offset = 0;
        $frames = 0;
        while ($frames < 2)
        {
            $where1 = strpos($raw, "\x00\x21\xF9\x04", $offset);
            if ( $where1 === false )
            {
                break;
            }
            else
            {
                $offset = $where1 + 1;
                $where2 = strpos( $raw, "\x00\x2C", $offset );
                if ( $where2 === false )
                {
                    break;
                }
                else
                {
                    if ( $where1 + 8 == $where2 )
                    {
                        $frames ++;
                    }
                    $offset = $where2 + 1;
                }
            }
        }
    
        return $frames > 1;
    }
    

提交回复
热议问题