<?PHP
/***************************************************
captcha.php

version        : 1.0
copyright    : (C) Lemat
email        : lemat at lemat.priv.pl
licence        : GPL

darmowe pliki z fontami mozna pobrac na przyklad z http://www.webpagepublicity.com/free-fonts.html

****************************************************/

function confirm($string) {
$config = array(
    
'font'=>'cactus_plain'// nazwa pliku z fontem ttf (bez rozszerzenia), nie moze zawierac spacji!
    
'fontpath'=>'.'// polozenie pliku z fontem
    
'image'=>'captcha.png'// nazwa obrazka typu PNG uzywanego jako background
    
'light'=>40// jasnosc napisu, 100=bialy, 0=czarny
    
'saturation'=>90// nasycenie koloru napisu, 0=szary, 100=najbardziej nasycony
    
'alpha'=>40 // przezroczystosc napisu, 0=bez przezroczystosci, 100=calkowicie transparentny
    
);

putenv('GDFONTPATH=' realpath($config['fontpath']));
$im    imagecreatefrompng($config['image']);
if (
$im) {
    
$imageheight=imagesy($im);
    
$imagewidth=imagesx($im);
    
$maxfontheight=$imageheight*0.6;
    
$minfontheight=$imageheight*0.4;
    
$string=substr($string,0,$imagewidth/($maxfontheight));
    
    
$px    $imagewidth/(strlen($string)+2);
    
$py    $imageheight*0.7;

    
header('Content-Type: image/png');
    
header('Cache-control: no-cache, no-store');

    for (
$i=0$i<strlen($string); $i++) {
        
$rgb=hls2rgb(mt_rand(0359),$config['light'],$config['saturation']);
        
$color imagecolorallocatealpha($im$rgb[0], $rgb[1],$rgb[2],$config['alpha']);
        
$angle=mt_rand(-3030);
        
$fontheight=mt_rand($minfontheight$maxfontheight);
        
$posx=($i+1)*$px+mt_rand(0$px/4);
        
imagettftext$im$fontheight$angle$posx$py+($angle*0.2), $color$config['font'], $string[$i] );
        }
    
imagepng($im);
    
imagedestroy($im);
    }
else
    die(
'Problem with file '.$config['image']);
}

function 
XRGB($hh$mm1$mm2) {
// funkcja pomocnicza
$hh=$hh%360;

if (
$hh 60
    return 
$mm1 + ($mm2 $mm1) * $hh 60;
elseif (
$hh 180
    return 
$mm2;
elseif (
$hh 240
    return 
$mm1 + ($mm2 $mm1) * (240 $hh) / 60;
else
    return 
$mm1;
}

function 
hls2rgb($h,$l,$s) {
$il = ($l%100) / 100;
$ih = ($h%360);
$is = ($s%100) / 100;
if (
$il <= 0.5
    
$m2 $il * ($is);
else 
    
$m2 $il $is $il $is;

$m1 2*$il $m2;

if (
$s == 0) {
    
$ir $il;
    
$ig $il;
    
$ib $il;
    }
else {
    
$ir XRGB($ih 120$m1$m2);
    
$ig XRGB($ih $m1$m2);
    
$ib XRGB($ih 120$m1$m2);
    }
return array(
round($ir 255),round($ig 255),round($ib 255));
}

//confirm((!empty($_GET['txt'])?$_GET['txt']:'captcha'));
?>