根据 Spektre 的回答编写了 PHP gif 修复程序:
/**
* gif image loop fixer, prints image full url
*
* as this is written as a part of framework, there are some config options
*/
// put your images absolute path here eg '/var/www/html/www.example.com/images/'
// or use autodetection below
$GLOBALS['config']['upload_path'] = str_replace("\\", "/", trim(getcwd(), " /\\")).'/images/';
// put your images relative/absolute url here, eg 'http://www.example.com/images/';
$GLOBALS['config']['upload_url'] = '/images/';
function _ig($image){
$image_a = pathinfo($image);
$new_filename = $GLOBALS['config']['upload_path'].$image_a['dirname'].'/_'.$image_a['filename'].'.'.$image_a['extension'];
$new_url = $GLOBALS['config']['upload_url'].$image_a['dirname'].'/_'.$image_a['filename'].'.'.$image_a['extension'];
if ($image_a['extension'] == 'gif'){
if (!file_exists($new_filename)){
// load file contents
$data = file_get_contents($GLOBALS['config']['upload_path'].$image);
if (!strstr($data, 'NETSCAPE2.0')){
// gif colours byte
$colours_byte = $data[10];
// extract binary string
$bin = decbin(ord($colours_byte));
$bin = str_pad($bin, 8, 0, STR_PAD_LEFT);
// calculate colour table length
if ($bin[0] == 0){
$colours_length = 0;
} else {
$colours_length = 3 * pow(2, (bindec(substr($bin, 1, 3)) + 1));
}
// put netscape string after 13 + colours table length
$start = substr($data, 0, 13 + $colours_length);
$end = substr($data, 13 + $colours_length);
file_put_contents($new_filename, $start . chr(0x21) . chr(0xFF) . chr(0x0B) . 'NETSCAPE2.0' . chr(0x03) . chr(0x01) . chr(0x00) . chr(0x00) . chr(0x00) . $end);
} else {
file_put_contents($new_filename, $data);
}
}
print($new_url);
} else {
print($GLOBALS['config']['upload_url'].$image);
}
}
这会将 image.gif 的副本复制为 _image.gif,并在颜色表之后插入所需的字符串并打印出新的 src url。(首先检查图像是否已经修复。)
用法:
<img src="<?php _ig($image); ?>">
或者
<img src="<?php _ig('image.gif'); ?>">
免责声明:不承担任何责任,我知道这可以优化:)