欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

php合成或者创建gif动画

程序员文章站 2022-03-29 14:12: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 $xv = -1*$xv;
  22. $x = abs($x);
  23. }
  24. if($y>$game_height){
  25. $yv = -1*$yv;
  26. $y = $game_height - ($y - $game_height);
  27. }elseif($y $yv = -1*$yv;
  28. $y = abs($y);
  29. }
  30. $pt[] = array($x,$y);
  31. }while($x!=$pt[0][0]||$y!=$pt[0][1]);
  32. $i = 0;
  33. while(isset($pt[$i])){
  34. $image = imagecreate($board_width,$board_height);
  35. imagecolorallocate($image, 0,0,0);
  36. $color = imagecolorallocate($image, 255,255,255);
  37. $color2 = imagecolorallocate($image, 255,0,0);
  38. if($pt[$i][1] + $pad_height imagefilledrectangle($image,0,$pt[$i][1],$pad_width, $pt[$i][1]+$pad_height,$color);
  39. }else{
  40. imagefilledrectangle($image,0,$board_width-$pad_height,$pad_width, $board_width,$color);
  41. }
  42. imagefilledrectangle($image,$board_width-$pad_width,0,$board_width, $board_height,$color2);
  43. 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);
  44. //imagesetpixel($image,$pt[$i][0],$pt[$i][1],$color);
  45. imagegif($image);
  46. imagedestroy($image);
  47. $imagedata[] = ob_get_contents();
  48. ob_clean();
  49. ++$i;
  50. }
  51. $gif = new GIFEncoder(
  52. $imagedata,
  53. 100,
  54. 0,
  55. 2,
  56. 0, 0, 1,
  57. "bin"
  58. );
  59. Header ('Content-type:image/gif');
  60. echo $gif->GetAnimation();
  61. ?>
复制代码