<?php

session_start();
$str = '23456789abcdefghizkmnpqrstuvwxyzABCDEFGHJKLMNPQRSTUVWSYZ';
$key = '';
$length = 4;
$minsize = 18;
$maxsize = 22;
$ttf = array('ARCADECLASSIC.TTF', 'Alpha Thin.ttf', 'Antique.ttf', 'animeace_i.ttf');
$ttfpath = 'captcha_fonts/';
for ($i = 0; $i < $length; $i++)
{
    $key .= $str{mt_rand(0, 55)};
}
$_SESSION['code'] = strtolower($key);
$width = ($length + 1) * (int)(($maxsize + $minsize) / 1.5);
$height = (int)(2.4 * $maxsize);
$image = imagecreatetruecolor($width, $height);
$bgcolor = @imagecolorallocate($image, mt_rand(224, 255), mt_rand(224, 255),
    mt_rand(224, 255));
@ImageFilledRectangle($image, 0, 0, $width, $height, $bgcolor);
for ($i = 0; $i < $length * 2; $i++)
{
    $size = intval(mt_rand((int)($minsize / 1.2), (int)($maxsize / 1.5)));
    $angle = intval(mt_rand(0, 360));
    $x = intval(mt_rand(0, $width));
    $y = intval(mt_rand(0, (int)($height - ($size / 5))));
    $color = imagecolorallocate($image, mt_rand(160, 224), mt_rand(160, 224),
        mt_rand(160, 224));
    $text = chr(intval(mt_rand(45, 250)));
    @ImageTTFText($image, $size, $angle, $x, $y, $color, $ttfpath . $ttf[mt_rand(0,
        3)], $text);
}
for ($i = 0, $x = intval(mt_rand($minsize, $maxsize)); $i < $length; $i++)
{
    $text = substr($key, $i, 1);
    $angle = intval(mt_rand(-25, 25));
    $size = intval(mt_rand($minsize, $maxsize));
    $y = intval(mt_rand((int)($size * 1.2), (int)($height - ($size / 5))));
    $r = mt_rand(0, 127);
    $g = mt_rand(0, 127);
    $b = mt_rand(0, 127);
    $color = imagecolorallocate($image, $r, $g, $b);
    $shadow = imagecolorallocate($image, $r + 127, $g + 127, $b + 127);
    @ImageTTFText($image, $size, $angle, $x + (int)($size / 15), $y, $shadow, $ttfpath .
        $ttf[mt_rand(0, 3)], $text);
    @ImageTTFText($image, $size, $angle, $x, $y - (int)($size / 15), $color, $ttfpath .
        $ttf[mt_rand(0, 3)], $text);
    $x += (int)($size + ($minsize / 5));
}
header('content-type:image/jpeg');
@ImageJPEG($image);
@ImageDestroy($image);

?>