开发者

File Upload Deny Animated GIFs (using PHP/Zend Framework)

i am thinking i may want users to be able to upload static GIFs but not animated ones. say for avatar's as they may look ... unprofessional and distracting. is there a way in PHP or Zend Framework that i can v开发者_如何学JAVAalidate a file upload that way?


You can use gd library to save your images. With gif type of files is saves only first frame from gif file if it animated. See imagegif function for more info how to use it.


Form the PHP: imagecreatefromgif - Manual:

I wrote two alternate versions of ZeBadger's is_ani() function, for determining if a gif file is animated

Original:
http://us.php.net/manual/en/function.imagecreatefromgif.php#59787

The first alternative version is just as memory intensive as the original, and more CPU intensive, but far simpler:

<?php
function is_ani($filename) {
    return (bool)preg_match('#(\x00\x21\xF9\x04.{4}\x00\x2C.*){2,}#s', file_get_contents($filename));
}
?>

The second alternative is about as CPU intensive as the original function, but uses less memory (and may also result in less disk activity)

<?php
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);

    fclose($fh);
    return $count > 1;
}
?>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜