<?php
/**
 *  Kata-kana CAPTCHA
 *
 *  @since      2006.11.26
 *  @author     Nob Funaki <http://blog.zuzara.com/>
 *  @license    GNU Lesser General Public License (GNU LGPL)
 */
class KATAKANA_CAPTCHA
{
    
/**
     *  表示されるカタカナ
     *  @var    string
     */
    
private $textstring '';

    
/**
     *
     */
    
public function __construct()
    {
        
$this->create();
    }

    
/**
     *
     */
    
private function create()
    {
        
$width 150;
        
$height 40;
        
$im imagecreatetruecolor($width$height);

        
// Create some colors
        
$white imagecolorallocate($im255255255);
        
$black imagecolorallocate($im000);
        
imagefilledrectangle($im00$width$height$white);

        
$text $this->generateKeyString();
        
$font dirname(__FILE__).'/fonts/ClownCharlie.TTF'// @todo   フォント増やす

        
imagettftext($im200030$black$font$text);

        
//  This code based on KCAPTCHA PROJECT VERSION 1.2.4
        //  Copyright by Kruglov Sergei, 2006
        //  www.captcha.ru, www.kruglov.ru
        //  @see    kcaptcha.php
        //  {{{
        
$center $width 2;

        
// periods
        
$rand1 mt_rand(750000,1200000)/10000000;
        
$rand2 mt_rand(750000,1200000)/10000000;
        
$rand3 mt_rand(750000,1200000)/10000000;
        
$rand4 mt_rand(750000,1200000)/10000000;
        
// phases
        
$rand5 mt_rand(0,3141592)/500000;
        
$rand6 mt_rand(0,3141592)/500000;
        
$rand7 mt_rand(0,3141592)/500000;
        
$rand8 mt_rand(0,3141592)/500000;
        
// amplitudes
        
$rand9 mt_rand(330,420)/110;
        
$rand10 mt_rand(330,450)/110;

        
$foreground_color = array(mt_rand(0,100), mt_rand(0,100), mt_rand(0,100));
        
$background_color = array(mt_rand(200,255), mt_rand(200,255), mt_rand(200,255));

        
$img2 imagecreatetruecolor($width$height);
        
$foreground imagecolorallocate($img2$foreground_color[0], $foreground_color[1], $foreground_color[2]);
        
$background imagecolorallocate($img2$background_color[0], $background_color[1], $background_color[2]);

        for (
$x 0$x $width$x++) {
            for (
$y 0$y $height$y++) {
                
$sx $x + (sin($x*$rand1+$rand5) + sin($y*$rand3+$rand6)) * $rand9 $width/$center 1;
                
$sy $y + (sin($x*$rand2+$rand7) + sin($y*$rand4+$rand8)) * $rand10;

                if (
$sx || $sy || $sx >= $width-|| $sy >= $height-1) {
                    
$color    255;
                    
$color_x  255;
                    
$color_y  255;
                    
$color_xy 255;
                } else {
                    
$color    imagecolorat($im$sx$sy) & 0xFF;
                    
$color_x  imagecolorat($im$sx+1$sy) & 0xFF;
                    
$color_y  imagecolorat($im$sx$sy+1) & 0xFF;
                    
$color_xy imagecolorat($im$sx+1$sy+1) & 0xFF;
                }

                if (
$color == && $color_x == && $color_y == && $color_xy == 0) {
                    
$newred   $foreground_color[0];
                    
$newgreen $foreground_color[1];
                    
$newblue  $foreground_color[2];
                } else if (
$color == 255 && $color_x == 255 && $color_y == 255 && $color_xy == 255) {
                    
$newred   $background_color[0];
                    
$newgreen $background_color[1];
                    
$newblue  $background_color[2];    
                } else {
                    
$frsx=$sx-floor($sx);
                    
$frsy=$sy-floor($sy);
                    
$frsx1=1-$frsx;
                    
$frsy1=1-$frsy;

                    
$newcolor = ($color*$frsx1*$frsy1 $color_x*$frsx*$frsy1 $color_y*$frsx1*$frsy $color_xy*$frsx*$frsy);

                    if (
$newcolor 255) {
                        
$newcolor 255;
                    }
                    
$newcolor  $newcolor/255;
                    
$newcolor0 1-$newcolor;

                    
$newred   $newcolor0*$foreground_color[0]+$newcolor*$background_color[0];
                    
$newgreen $newcolor0*$foreground_color[1]+$newcolor*$background_color[1];
                    
$newblue  $newcolor0*$foreground_color[2]+$newcolor*$background_color[2];
                }

                
imagesetpixel($img2$x$yimagecolorallocate($img2$newred$newgreen$newblue));
            }
        }
        
//  }}} End KCAPTCHA code.

        
header("Content-Type: image/x-png");
        
imagepng($img2);
    }

    
/**
     *  @param  void
     *  @return string
     */
    
private function generateKeyString()
    {
        
$convert_table = array(
            
'a' => 'チ',
            
'b' => 'コ',
            
'e' => 'イ',
            
'f' => 'ハ',
            
'g' => 'キ',
            
'j' => 'マ',
            
'm' => 'モ',
            
'n' => 'ミ',
            
'o' => 'ラ',
            
'p' => 'セ',
            
'q' => 'タ',
            
'r' => 'ス',
            
's' => 'ト',
            
't' => 'カ',
            
'u' => 'ナ',
            
'v' => 'ヒ',
            
'w' => 'テ',
            
'x' => 'サ',
            
'1' => 'ヌ',
            
'2' => 'フ',
            
'3' => 'ア',
            
'4' => 'ウ',
            
'5' => 'エ',
            
'6' => 'オ',
            
'7' => 'ヤ',
            
'8' => 'ユ',
            
'9' => 'ヨ',
            
'-' => 'ホ',
            
'^' => 'ヘ',
            
';' => 'レ',
            
']' => 'ム',
            
',' => 'ネ',
            
'.' => 'ル',
            
'/' => 'メ',
            
'A' => 'ヂ',
            
'B' => 'ゴ',
            
'F' => 'バ',
            
'G' => 'ギ',
            
'I' => 'ピ',
            
'K' => 'ポ',
            
'N' => 'ロ',
            
'P' => 'ゼ',
            
'Q' => 'ダ',
            
'R' => 'ズ',
            
'S' => 'ド',
            
'T' => 'ガ',
            
'U' => 'パ',
            
'V' => 'ビ',
            
'W' => 'デ',
            
'X' => 'ザ',
            
'!' => 'プ',
            
'"' => 'ブ',
            
'=' => 'ボ',
            
'`' => 'ベ',
            
'<' => 'ペ',
            
'?' => 'ヴ',
            );
        
//  @fixme  ノとレが隣とかダメそう
        
$rand array_rand($convert_table4);
        
$text '';
        
$keys '';
        foreach (
$rand as $val) {
            
$keys .= $val;
            
$text .= $convert_table[$val];
        }
        
$this->textstring $text;
        return 
$keys;
    }

    
/**
     *
     */
    
public function getTextString()
    {
        return 
$this->textstring;
    }
}