Home >Backend Development >PHP Tutorial >PHP synthesis or creation of gif animation

PHP synthesis or creation of gif animation

WBOY
WBOYOriginal
2016-07-25 08:49:221191browse
1. First, you need to confirm whether the GD library is normal.

2. If it is a composite image, please make sure to put the decomposed image in the frames folder.

3. GIFEncoder.class.php class
  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. ?>
Copy code


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn