background image

PHP 如何生成 GIF 动态图片验证码

   
    相信很多人都想过如何用 PHP 生成 GIF 动画来实现动态图片验证码,以下是实现过程。
  ImageCode 函数通过 GIFEncoder 类实现的 GIF 动画的 PHP 源代码,有兴趣的朋友可
以研究一下。
 
  /**
  * ImageCode 生成 GIF 图片验证
  * @param $string 字符串
  * @param $width 宽度
  * @param $height 高度
  * */
  function ImageCode($string = '', $width = 75, $height = 25)
  {
   $authstr = $string ? $string : ((time() % 2 == 0) ? mt_rand(1000, 9999) : mt_rand(10000, 
99999));
  $board_width = $width;
  $board_height = $height;
  // 生成一个 32 帧的 GIF 动画
  for($i = 0; $i < 32; $i++)
  {
  ob_start();
  $image = imagecreate($board_width, $board_height);
  imagecolorallocate($image, 0,0,0);
  // 设定文字颜色数组
  $colorList[] = ImageColorAllocate($image, 15,73,210);
  $colorList[] = ImageColorAllocate($image, 0,64,0);
  $colorList[] = ImageColorAllocate($image, 0,0,64);
  $colorList[] = ImageColorAllocate($image, 0,128,128);
  $colorList[] = ImageColorAllocate($image, 27,52,47);
  $colorList[] = ImageColorAllocate($image, 51,0,102);
  $colorList[] = ImageColorAllocate($image, 0,0,145);
  $colorList[] = ImageColorAllocate($image, 0,0,113);
  $colorList[] = ImageColorAllocate($image, 0,51,51);
  $colorList[] = ImageColorAllocate($image, 158,180,35);
  $colorList[] = ImageColorAllocate($image, 59,59,59);
  $colorList[] = ImageColorAllocate($image, 0,0,0);
  $colorList[] = ImageColorAllocate($image, 1,128,180);
  $colorList[] = ImageColorAllocate($image, 0,153,51);
  $colorList[] = ImageColorAllocate($image, 60,131,1);
  $colorList[] = ImageColorAllocate($image, 0,0,0);
  $fontcolor = ImageColorAllocate($image, 0,0,0);
  $gray = ImageColorAllocate($image, 245,245,245);
  $color = imagecolorallocate($image, 255,255,255);