<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
	<title type="html"><![CDATA[Code The World — Custom Captcha]]></title>
	<link rel="self" href="https://codetheworld.com/extern.php?action=feed&amp;tid=123&amp;type=atom" />
	<updated>2010-06-21T07:00:32Z</updated>
	<generator>PunBB</generator>
	<id>https://codetheworld.com/viewtopic.php?id=123</id>
		<entry>
			<title type="html"><![CDATA[Custom Captcha]]></title>
			<link rel="alternate" href="https://codetheworld.com/viewtopic.php?pid=380#p380" />
			<content type="html"><![CDATA[<p>Something I use when designing websites that way to give the look and feel of a personal website (without branding the captcha with the current one)</p><p>Uses random fonts (from actual font files, ttf)<br />Random colors per character<br />Choose what characters to use<br />random font size per character used<br />can choose random amount of characters to draw (min, max)<br />sin wav&#039;s the image to create a more warped look (looking to implement a better wave but right now thats what i got, and seems to do the job <img src="https://codetheworld.com/img/smilies/big_smile.png" width="15" height="15" alt="big_smile" /> )</p><p>In WORKS (haven&#039;t had much time lately, been busy with other things)<br />Adding speech to this for people with disabilities in reading!<br />Custom backgrounds drawn to help combat bot detection</p><p>Code for Actual Captcha System<br /></p><div class="codebox"><pre><code>&lt;a href=&quot;#&quot; onclick=&quot;document.getElementById(&#039;img_captcha&#039;).src = &#039;captcha/script.php?a=&#039;+Math.round(Math.random()*10000);&quot;&gt;&lt;img src=&quot;captcha/script.php&quot; id=&quot;img_captcha&quot; border=&quot;0&quot; /&gt;&lt;/a&gt;</code></pre></div><p>Next Code Goes Under <br />captcha/script.php<br /></p><div class="codebox"><pre><code>&lt;?php
session_start();
// characters to use when generating random string
$chr = &#039;abcdefghkmnpqrstuvwxyz0123456789&#039;;
// array of fonts to use (in same directory)
$ttf = array(
    &#039;arial.ttf&#039;,
    &#039;barrettprime.ttf&#039;,
    &#039;gabriola.ttf&#039;
);
// random sizes to use in pt
$siz = array(
    20,
    25,
    30,
    35
);
// random colors to use per character
$txtC = array(
    &#039;000000&#039;,
    &#039;333333&#039;,
    &#039;990000&#039;,
    &#039;009900&#039;
);
// random colors to use per background (soon will be replaced with images)
$bd = array(
    &#039;CCCCCC&#039;,
    &#039;999999&#039;
);
// mininum amount of random characters generated
$min = 4;
// maximum amount of random characters generated
$max = 8;
/* NO NEED TO EDIT BELOW */
$ltp = 2;
$len = rand($min, $max);

function hex2rgb($c){
    if(!$c) return false;
    $c = trim($c);
    $out = false;
    $c = str_replace(&#039;#&#039;,&#039;&#039;, $c);
    $l = strlen($c) == 3 ? 1 : (strlen($c) == 6 ? 2 : false);
    
    if($l){
        unset($out);
        $out[0] = hexdec(substr($c, 0,1*$l));
        $out[1] = hexdec(substr($c, 1*$l,1*$l));
        $out[2] = hexdec(substr($c, 2*$l,1*$l));
    }else
        $out = false;
          
    return $out;
}
function genRand($char, $length){
    $rtn = &#039;&#039;;
    for($i=0;$i&lt;$length;$i++){
        $rtn .= $char{rand(0, strlen($char)-1)};
    }
    return($rtn);
}
$spc = 40;
$wid = $len * $spc;
$hei = 50;
$cpt = genRand($chr, $len);
$_SESSION[&#039;captcha_key&#039;] = md5($cpt);
$captcha = imagecreate($wid, $hei);
$bgR = hex2rgb($bd[rand(0, count($bd)-1)]);
$bg = imagecolorallocate($captcha, $bgR[0], $bgR[1], $bgR[2]);

imagerectangle($captcha, 0, 0, $wid, $hei, $bg);

// DRAW captcha
$ang = 20;

for($i=0;$i&lt;strlen($cpt);$i++){
    $c = $cpt[$i];
    $a = rand(-1*$ang, $ang);
    $bgR = hex2rgb($txtC[rand(0, count($txtC)-1)]);
    $fg = imagecolorallocate($captcha, $bgR[0], $bgR[1], $bgR[2]);
    $ch = rand(0, count($ttf)-1);
    $si = $siz[$ch];
    $ft = $ttf[$ch];
    $bx = imagettfbbox($si, $a, $ft, $c);
    $ii = $i; $ii++;
    $ww = $bx[2] - $bx[0]; if($ww&lt;0) $ww*=-1;
    $hh = $bx[3] - $bx[5]; if($hh&lt;0) $hh*=-1;
    $add =  rand($i*$spc, $i*$spc+($spc-$ww));
    $rnX = $i*$spc + rand($ltp, $spc-$ww-$ltp);
    $rnY = $hh + rand($ltp, $hei-$hh-$ltp);
    $xb = imagettftext($captcha, $si, $a, $rnX, $rnY, $fg, $ft, $c);
}
// next fn
function wave_area($img, $x, $y, $width, $height, $amplitude = 10, $period = 10){
    // Make a copy of the image twice the size
    $height2 = $height * 2;
    $width2 = $width * 2;
    $img2 = imagecreatetruecolor($width2, $height2);
    imagecopyresampled($img2, $img, 0, 0, $x, $y, $width2, $height2, $width, $height);
    if($period == 0) $period = 1;
    // Wave it
    for($i = 0; $i &lt; ($width2); $i += 2)
        imagecopy($img2, $img2, $x + $i - 2, $y + sin($i / $period) * $amplitude, $x + $i, $y, 2, $height2);
    // Resample it down again
    imagecopyresampled($img, $img2, $x, $y, 0, 0, $width, $height, $width2, $height2);
    imagedestroy($img2);
}
wave_area($captcha, 0, 0, $wid, $hei, 3, 6);

header(&quot;Content-type: image/png&quot;);
imagepng($captcha);
?&gt;</code></pre></div><p>HUGE NOTE place fonts within the captcha folder<br />in TTF format i will be looking into a way to use OTF but i&#039;m not for sure on the matter</p><p>Felt like sharing this as this as I love to code please note me if you can (comment in php so it doesn&#039;t ruin the html files i know how much people hate this)</p><p>As my first code post I hope I&#039;ll contribute MANY more <img src="https://codetheworld.com/img/smilies/smile.png" width="15" height="15" alt="smile" /></p>]]></content>
			<author>
				<name><![CDATA[cr4kt0rch]]></name>
				<uri>https://codetheworld.com/profile.php?id=429</uri>
			</author>
			<updated>2010-06-21T07:00:32Z</updated>
			<id>https://codetheworld.com/viewtopic.php?pid=380#p380</id>
		</entry>
</feed>
