Reputation: 1265
I am currently re-writing an existing tool in PHP that another person started for optimizing images, for gif
images,I will convert to png
and then optimize the png file
. For animated gifs
I will run through the program called Gifsicle
which can optimize animated gif
images.
In some of the existing codebase, the other guy identified animated gif images with the file extension gifgif
as you can see in the snippets below, they contain all the code that references this gifgif
.
I have search Google and I cannot find a mention of a gifgif
file anywhere, in addition I have never in my life heard of one.
Can someone help me figure out does the actual extension gifgif
exist? Is it possibly something that is embedded in animated gif
files?
The code...
$types = array("gif", "png", "gifgif", "jpg", "jpeg", "bmp");
and
if (substr($type, 0, 6) === "gifgif")
{
$type = "gifgif";
}
and
case "gifgif":
$dest_file .= ".gif";
$cmd = "/usr/bin/gifsicle -O2 $src_file > $dest_file";
exec($cmd, $return, $error);
Upvotes: 2
Views: 255
Reputation: 57316
gifgif
is not a valid extension - and by all means is not a valid way to check whether the file is an animated gif file. In order to check whether the file is an animated gif, you need to actually parse the content of the file and check for frame headers. This function will do it simply:
<?php
function is_ani($filename) {
return (bool)preg_match('#(\x00\x21\xF9\x04.{4}\x00\x2C.*){2,}#s', file_get_contents($filename));
}
?>
P.S. This is not my function, but borrowed from http://php.net/manual/en/function.imagecreatefromgif.php There is another version given in the same post. That version of the function reads the file in chunks and checks each individual chunk, however this approach will not correctly, because the header may end up being split between two chunks.
Upvotes: 1
Reputation: 943213
This is not a standard extension and the code you show appears to care only about file extensions.
Presumably the developer of the system you are using came up with the convention and it is unique to that system.
Upvotes: 2