Location: PHPKode > scripts > Hippo Chinese Cert Code > hippo-chinese-cert-code/hippo_chinese_cert_code.php
<?php
/******************************************************************************
**  File        :  hippo_chinese_cert_code.php                               **
**  Class name  :  hippo_chinese_cert_code                                   **
**  Author      :  DavidLanz (hide@address.com)                           **
**                 Huijun (http://www.huijun.org/)                           **
**                                                                           **
**  Description :  This class is for prevent register spam, using Chinese    **
**                 font instead of English or numeral. It means You guys are **
**                 going to type in Chinese to pass it through               **
**                                                                           **
**  Date          :  28/04/2006                                              **
**  Last Modified :  28/04/2006                                              **
**                                                                           **
**  PHP Version   :  4.x                                                     **
*******************************************************************************/

class hippo_chinese_cert_code
{
  var $str_background;
  var $str_font;
  var $str_magicword;
  var $str_cht_filename;
  var $int_how_many_char;
  var $ary_hippo_tmp;
  var $ary_res_char;
  var $err_str;

  /* Constructor */
  function hippo_chinese_cert_code($str_background, $str_font, $str_cht_filename, $gen_how_many_char)
  {
    $this->str_background = $str_background;
    $this->str_font = $str_font;
    $this->str_cht_filename = $str_cht_filename;
    $this->int_how_many_char = $gen_how_many_char;
    $this->ary_hippo_tmp = Array();
    $this->ary_res_char = Array();
    if(!$this->init())
    {
      $this->show_error();
    }
  }
  
  function init()
  {
    if(!file_exists($this->str_background))
    {
      $this->err_str .= 'Background Picture NOT found<br>';
      return false;
    }
    
    if(!file_exists($this->str_font))
    {
      $this->err_str .= 'Font NOT found<br>';
      return false;
    }
    
    if(!file_exists($this->str_cht_filename))
    {
      $this->err_str .= 'Chinese Character NOT found<br>';
      return false;
    }
    
    if(!session_is_registered('ar_res_char'))
    {
      session_register('ar_res_char');
    }
    $this->generate_string();
    return true;
  }
  
  function generate_string()
  {
    $this->load_cht_str_file($this->str_cht_filename);
    
    for($i=0; $i<count($this->ary_res_char);$i++)
    {
      $this->str_magicword .= $this->ary_res_char[$i];
    }
    $this->str_magicword = iconv('big5', 'UTF-8', $this->str_magicword);
    
    $font_size = '40';
    $axis_x = '23';
    $axis_y = '65';
    
    $green_R = 0x00;
    $green_G = 0xCC;
    $green_B = 0x00;

    $black_R = 0x00;
    $black_G = 0x00;
    $black_B = 0x00;

    $orange_R = 0xff;
    $orange_G = 0x80;
    $orange_B = 0x00;

    $blue_R = 0x00;
    $blue_G = 0x66;
    $blue_B = 0xb3;
    
    $gs = getimagesize($this->str_background);
    if($gs[2]==1){$origImg=imagecreatefromgif($this->str_background);}
    else if($gs[2]==2){$origImg=imagecreatefromjpeg($this->str_background);}
    else if($gs[2]==3){$origImg=imagecreatefrompng($this->str_background);}
    
    $black = imagecolorallocate ($origImg ,$black_R ,$black_G ,$black_B);
    $green = imagecolorallocate ($origImg ,$green_R ,$green_G ,$green_B);
    $orange = imagecolorallocate ($origImg ,$orange_R ,$orange_G ,$orange_B);
    $blue = imagecolorallocate ($origImg ,$blue_R ,$blue_G ,$blue_B);

    imagettftext($origImg, $font_size, 0, $axis_x, $axis_y, $black, $this->str_font, $this->str_magicword);
    header("Content-type: image/png");
    imagepng($origImg);
    imagedestroy($origImg);
    
  }
  
  function load_cht_str_file($str_cht_filename)
  {
    if(!$file=fopen($str_cht_filename, "r"))
    {
      $this->err_str = "Error: can't load filename.";
      return false;
    }
    
    $this->ary_hippo_tmp = file($str_cht_filename);
    $total = count($this->ary_hippo_tmp);
    
    for($i=0; $i<$this->int_how_many_char; $i++)
    {
      $ran_row = rand(0, $total-1);
      $str_temp = $this->ary_hippo_tmp[$ran_row];
      $ran_col = rand(0, strlen($str_temp)-3 );
      if($ran_col%2!=0){$ran_col--;}
      $this->ary_res_char[] = $str_temp[$ran_col].$str_temp[$ran_col+1];
      
      /*
      echo 'the'.($i+1).'word('.$ran_row.'row/'.$ran_col.'col)'.$str_temp[$ran_col].$str_temp[$ran_col+1].'<br>';
      echo 'debug:'.$str_temp[$ran_col].$str_temp[$ran_col+1].'<br>';
      */
    }
    $_SESSION['ar_res_char'] = $this->ary_res_char;
    return true;
  }
  
  function show_error()
  {
    echo 'Error:'.$this->err_str;
  }
  
}
?>
Return current item: Hippo Chinese Cert Code