当前位置:Gxlcms > PHP教程 > php合成或者创建gif动画

php合成或者创建gif动画

时间:2021-07-01 10:21:17 帮助过:22人阅读

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

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

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

人气教程排行