php合成或者创建gif动画

1. 首先需要确认GD库是否正常。

2. 如果是合成图片,请确保把分解的图片放在frames的文件夹里面。

3. GIFEncoder.class.php 类

GIFEncoder.zip

PHP代码
  1. <?php  
  2. include "GIFEncoder.class.php";  
  3.   
  4. ob_start();  
  5. $board_width = 60;  
  6. $board_height = 60;  
  7. $pad_width = 5;  
  8. $pad_height = 15;  
  9. $ball_size = 5;  
  10. $game_width = $board_width - $pad_width*2 - $ball_size;  
  11. $game_height = $board_height-$ball_size;  
  12.   
  13. $x = 0;  
  14. $y = rand(0,$game_height);  
  15. $xv = rand(1,10);  
  16. $yv = rand(1,10);  
  17. $pt[] = array($x,$y);  
  18. do{  
  19.         $x += $xv;  
  20.         $y += $yv;  
  21.         if($x > $game_width){  
  22.                 $xv = -1*$xv;  
  23.                 $x = $game_width - ($x-$game_width);  
  24.         }elseif($x < 0){  
  25.                 $xv = -1*$xv;  
  26.                 $x = abs($x);  
  27.         }  
  28.         if($y>$game_height){  
  29.                 $yv = -1*$yv;  
  30.                 $y = $game_height - ($y - $game_height);  
  31.         }elseif($y<0){  
  32.                 $yv = -1*$yv;  
  33.                 $y = abs($y);  
  34.         }  
  35.         $pt[] = array($x,$y);  
  36. }while($x!=$pt[0][0]||$y!=$pt[0][1]);  
  37.   
  38. $i = 0;  
  39. while(isset($pt[$i])){  
  40.         $image = imagecreate($board_width,$board_height);  
  41.         imagecolorallocate($image, 0,0,0);  
  42.         $color = imagecolorallocate($image, 255,255,255);  
  43.         $color2 = imagecolorallocate($image, 255,0,0);  
  44.           
  45.         if($pt[$i][1] + $pad_height < $board_width){  
  46.                 imagefilledrectangle($image,0,$pt[$i][1],$pad_width$pt[$i][1]+$pad_height,$color);  
  47.         }else{  
  48.                 imagefilledrectangle($image,0,$board_width-$pad_height,$pad_width$board_width,$color);  
  49.         }  
  50.         imagefilledrectangle($image,$board_width-$pad_width,0,$board_width$board_height,$color2);  
  51.         imagefilledrectangle($image,$pad_width+$pt[$i][0], $ball_size+$pt[$i][1]-$ball_size$pad_width+$pt[$i][0]+$ball_size$ball_size+$pt[$i][1],$color);  
  52.         //imagesetpixel($image,$pt[$i][0],$pt[$i][1],$color);  
  53.         imagegif($image);  
  54.         imagedestroy($image);  
  55.         $imagedata[] = ob_get_contents();  
  56.         ob_clean();  
  57.         ++$i;  
  58. }  
  59.   
  60. $gif = new GIFEncoder(  
  61.                             $imagedata,  
  62.                             100,  
  63.                             0,  
  64.                             2,  
  65.                             0, 0, 1,  
  66.                             "bin"  
  67.         );  
  68.           
  69. Header ('Content-type:image/gif');  
  70. echo $gif->GetAnimation();  
  71. ?>  

 



上一篇: php识别电脑还是手机访问网站
下一篇: php web下载网站代码
文章来自: 本站原创
引用通告: 查看所有引用 | 我要引用此文章
Tags: php
相关日志:
评论: 0 | 引用: 0 | 查看次数: 2309
发表评论
昵 称:
密 码: 游客发言不需要密码.
邮 箱: 邮件地址支持Gravatar头像,邮箱地址不会公开.
网 址: 输入网址便于回访.
内 容:
验证码:
选 项:
虽然发表评论不用注册,但是为了保护您的发言权,建议您注册帐号.
字数限制 1000 字 | UBB代码 开启 | [img]标签 关闭

 广告位

↑返回顶部↑