function is_animated_gif($filename)
{
$lsd_offset = 6; // logical screen descriptor
$gct_offset = 13; // global color table
$chunk_size = 2048; // сколько читаем из начала файла
$fd = fopen($filename, 'r');
$buff = fread($fd, $chunk_size);
fclose($fd);
$packed = ord($buff[$lsd_offset+4]);
$gct_flag = ($packed >> 7) & 1;
$gct_size = $packed & 7;
$gct_length = 1 << ($gct_size+1);
$data_offset = $gct_offset + ($gct_flag? 3*$gct_length: 0);
while ($data_offset < strlen($buff)) {
if ((ord($buff[$data_offset])==0x21) && (ord($buff[$data_offset+1]) == 0xf9)) {
// we hit a Graphic control extension
$delay_time = ord($buff[$data_offset+5])<<8 | ord($buff[$data_offset+4]);
if ($delay_time > 0) {
return true;
} else {
return false;
}
} elseif((ord($buff[$data_offset])==0x21) && (ord($buff[$data_offset+1]) == 0xff)) {
// we hit an application extension
$app_name = substr($buff, $data_offset+3, 8);
$app_bytes = substr($buff, $data_offset+11, 3);
$app_data = array();
$data_offset += 14;
do {
$size = ord($buff[$data_offset]);
$app_data[] = substr($buff, $data_offset+1, $size);
$data_offset += $size+1;
} while (ord($buff[$data_offset]) !=0);
$data_offset += 1;
// found Netscape looping extension. GIF is animated.
if (('NETSCAPE' == $app_name) && ('2.0' == $app_bytes) && (3 == strlen($app_data[0])) && (1 == ord($app_data[0][0]))) {
return true;
}
} elseif((ord($buff[$data_offset])==0x21) && (ord($buff[$data_offset+1]) == 0xfe)) {
// we hit a comment extension
$data_offset += 2;
do {
$size = ord($buff[$data_offset]);
$data_offset += $size+1;
} while (ord($buff[$data_offset]) !=0);
$data_offset += 1;
} elseif(ord($buff[$data_offset]) == 0x2c) {
// we hit an actual image
return false;
} else {
// shit happens
die ("GIF format error.<br>");
}
}
}