PDA

View Full Version : Animated gif with GD



zhono
03-10-2009, 08:11 PM
I'm adding a hit counter service to one of my sites, and I want to be able to use animated gif's. This works fine with regular images, but if it's animated, it only uses one frame. Does anyone know how I can make this work properly with an animated gif? Or if it's even possible? Thanks in advance.



<?

session_start() ;

require_once("config.php") ;
require_once("functions/functions.php") ;
$account = jc_code($_GET['acc'], 'off', 'off', 'name') ;

if((file_exists('data/' . $account . '.jac')) && (!$account == "")) {
$handle = fopen('data/' . $account . '.jac', r) ;
$content = fread($handle, filesize('data/' . $account . '.jac')) ;
$line_content = explode("\r\n", $content) ;
$section_content = explode("|", $line_content[0]) ;
fclose($handle) ;
$hits = $section_content[3] + 1 ;
if($_SESSION['last_log'] > time() - 3) {
//cant log yet - too early
}

else {
$_SESSION['last_log'] = time() ;
$section_content[3] = $hits ;
$section_content[4] = time() ;
$handle = fopen('data/' . $account . '.jac', w) ;
for($a = 0; $a < 11; $a ++) {
$write_data .= $section_content[$a] . '|' ;
}

fwrite($handle, $write_data) ;
fclose($handle) ;
}

if(file_exists("images/counter/$section_content[1].gif")) {
$img_handle = ImageCreateFromGif("images/counter/$section_content[1].gif") ;
}

else {
$img_handle = ImageCreateFromGif("images/counter/default.gif") ;
}

$txt_color = imagecolorallocate ($img_handle, 0, 0, 0) ;

if(strlen($hits) < 2) {
$hits = '000' . $hits ;
}

elseif(strlen($hits) < 3) {
$hits = '00' . $hits ;
}

elseif(strlen($hits) < 4) {
$hits = '0' . $hits ;
}

$center = 155 - ((strlen($hits) * 9) / 2) ;

header("Content-type: image/gif") ;
$font = imageloadfont($load_font) ;
Imagestring($img_handle, $font, $center, 2, $hits, $text_color) ;
imagegif($img_handle);
}

else {
$img_handle = ImageCreateFromGif("images/counter/default.gif") ;
$txt_color = imagecolorallocate ($img_handle, 0, 0, 0) ;
$text = "- - - - -" ;
$center = 155 - ((strlen($text) * 9) / 2) ;
header("Content-type: image/gif") ;
$font = imageloadfont($load_font) ;
Imagestring($img_handle, $font, $center, 2, $text, $text_color);
imagegif($img_handle);
}

?>

JasonDFR
03-10-2009, 08:21 PM
I have no experience with this myself, but I found this. If it works, let us know.

http://www.phpclasses.org/browse/package/3163.html

zhono
03-15-2009, 07:53 PM
Unfortunately, that had to do with creating an animated gif using multiple image files as source. I ended up just modifying the script a bit to place the text onto a transparent gif, and using a div to layer that onto an unmodified animated gif. Looks just as good, and allows my users to easily use their own animation or other image, instead of just what I provide. Thanks anyways.