PDA

View Full Version : آموزش: آموزش ساخت یک captcha



MRmoon
پنج شنبه 10 اسفند 1391, 14:53 عصر
با سلام.

حتما همتون میدونید captcha چی هست.

ما برای ساختن این کپچا که در این جا میخوام آموزشش رو بدم از php استفاده میکنیم.

ما کدی رو که روی عکس نوشته میشه روی یک سشن ذخیره میکنیم!

و به همین راحتی !:لبخند:

اول ما باید width و height رو تنظیم کنیم:



// SET height AND width
$width = '300';
$height = '75';

خوب این شد درزا و پهنای عکس ما!

حالا ما باید تعداد دایره ها ، خط ها ، تعداد حروف ، فاصله و .. تنظیم کنیم!



//SET cicle's and line's count
$circleCount = '10';
$lineCount = '5';
// SET number of characters
$length = '6';
//Other options
$space = '30';
$MaxRadius = '50';

اینم از بقیه ی متغییرا!

حالا ما با تابع header میگیم که صفحه یک عکس هستش!


// SET page is a picture
header("Content-type: image/png");


حالا از اینبه بعد صفحه رو مرورگر به عنوان یک عکس میشناسه!

حالا ما باید نوشته های از a-z و از 0-9 رو تو یک آرایه بریزیم تا بشه ازش استفاده کرد و بعد نوشتمون رو میسازیم که به صورت تصدفی هست:



//Set a characters
$alpha = array_merge(range('a', 'z'), range('0', '9'));
//Generate a code
$text = '';
for($i=0 ; $i < $length ; $i++)
{
$text .= $alpha[ rand() % count($alpha)];
}


حالا ما باید این کد رو توی یک سشن بریزیم تا بشه از اون استفاده کرد!



session_start();
$_SESSION['captcha'] = $text;


حالا قدم بعدی چیه؟

ما باید عکس رو تولید کنیم! یعنی تابع ساخت عکس رو بنویسیم و اون رو توی متغیر $img بریزیم.



// creat image
$img = imagecreate($width,$height);


حالا ما باید آدرس فونت رو بدیم و اندازه ی نوشته ها و عدد ها و یک سری اطلاعات دیگه که هر کدوم کارش رو بالاش نوشته:



// SET background and text color
$bg_color = imagecolorallocate($img,0,0,350);
$text_color = imagecolorallocate($img,250,150,0);
// Asign font
$font = './alger.ttf';
$fontsize = $height * 0.35;
$numsize = $fontsize * 2;
// Calculate the bounding box of the text
$box = imagettfbbox($fontsize, 10, $font, $text);
// Start position of the text
$positionX = ($width - $space * $length) / 2;
// Rotation Direction (1: Anti-clockwise, -1: Clockwise)
$direction = 1;
// Fill the image with background color
imagefill($img, $width / 2, $height / 2, $bg_color);

حالا ما باید یک سری عدد و حروف روی عکس بذاریم!
تعداد حروف و عددها (با هم) به اندازه ی متغییر $length هست که بالا تایین کردیم.
ما با تابع substr متغیر $text که بالا تولید کردیم رو میبریم و چک میکنیم اگه حرف بود سازش چقد و اگه نه سایزش چقد باشه. بعدش اونو رو عکس میندازیم . بعدد مقدار $direction و $positionX که بالا تایین کرده بودیم تغییر میدیم!


// Wright text on the image
for($i = 0; $i < $length; $i++){
$char = substr($text, $i, 1);
$size = (in_array($char, range('a', 'z')) ? $fontsize : $numsize);
imagettftext($img, $size, (rand() % 30 * $direction), $positionX, ($height - $box[5]) / 2 - 2, $text_color, $font, $char);
$direction = -$direction;
$positionX += $space;
}

بعد ما چند تا خط و دایره که تعدادشون رو تنظیم کرده یودیم میندازیم!


//Add lines to picture
for($i=0 ; $i < $lineCount ; $i++){
$cy = rand() % $height;
$cx = rand() % $width;
$ry = rand() % $MaxRadius;
$rx = rand() % $MaxRadius;
// set line's color (colors are random)
$lineColor = imagecolorallocate($img, rand() % 256, rand() % 256, rand() % 256);
//imageline($img,$cx,$cy,$rx,$ry,$lineColor);
imageline($img, $cx, $cy, $rx, $ry, $lineColor);
}
//Add circle to picture
for($i=0 ; $i < $circleCount ; $i++){
$cy = rand() % $height;
$cx = rand() % $width;
$ry = rand() % $MaxRadius;
$rx = rand() % $MaxRadius;
// set line's color (colors are random)
$lineColor = imagecolorallocate($img, rand() % 256, rand() % 256, rand() % 256);
$start = rand() % 360;
$end = rand() % 360;
imagearc($img, $cx, $cy, $rx, $ry,$start,$end, $lineColor);
}

حالا ما با تابع imagepng عکس رو نمایش میدیم!



//show image
imagepng($img);
imagedestroy($im);


اینم شد یک کپچای خوب. کل کد که بهم وصل شده اینه برای اینکه قاطی نکنین:


<?php
ob_start();
// SET height AND width
$width = '300';
$height = '75';
//SET cicle's and line's count
$circleCount = '10';
$lineCount = '5';
// SET number of characters
$length = '6';
//Other options
$space = '30';
$MaxRadius = '50';
// SET page is a picture
header("Content-type: image/png");
//Set a characters
$alpha = array_merge(range('a', 'z'), range('0', '9'));
//Generate a code
$text = '';
for($i=0 ; $i < $length ; $i++)
{
$text .= $alpha[ rand() % count($alpha)];
}
//SET captcha session code
session_start();
$_SESSION['captcha'] = $text;
// creat image
$img = imagecreate($width,$height);
// SET background and text color
$bg_color = imagecolorallocate($img,0,0,350);
$text_color = imagecolorallocate($img,250,150,0);
// Asign font
$font = './alger.ttf';
$fontsize = $height * 0.35;
$numsize = $fontsize * 2;
// Calculate the bounding box of the text
$box = imagettfbbox($fontsize, 10, $font, $text);

// Start position of the text
$positionX = ($width - $space * $length) / 2;

// Rotation Direction (1: Anti-clockwise, -1: Clockwise)
$direction = 1;

// Fill the image with background color
imagefill($img, $width / 2, $height / 2, $bg_color);
// Wright text on the image
for($i = 0; $i < $length; $i++){
$char = substr($text, $i, 1);
$size = (in_array($char, range('a', 'z')) ? $fontsize : $numsize);
imagettftext($img, $size, (rand() % 30 * $direction), $positionX, ($height - $box[5]) / 2 - 2, $text_color, $font, $char);
$direction = -$direction;
$positionX += $space;
}
//Add lines to picture
for($i=0 ; $i < $lineCount ; $i++){
$cy = rand() % $height;
$cx = rand() % $width;
$ry = rand() % $MaxRadius;
$rx = rand() % $MaxRadius;
// set line's color (colors are random)
$lineColor = imagecolorallocate($img, rand() % 256, rand() % 256, rand() % 256);
//imageline($img,$cx,$cy,$rx,$ry,$lineColor);
imageline($img, $cx, $cy, $rx, $ry, $lineColor);
}
//Add circle to picture
for($i=0 ; $i < $circleCount ; $i++){
$cy = rand() % $height;
$cx = rand() % $width;
$ry = rand() % $MaxRadius;
$rx = rand() % $MaxRadius;
// set line's color (colors are random)
$lineColor = imagecolorallocate($img, rand() % 256, rand() % 256, rand() % 256);
$start = rand() % 360;
$end = rand() % 360;
imagearc($img, $cx, $cy, $rx, $ry,$start,$end, $lineColor);
}


//show image
imagepng($img);
imagedestroy($im);
ob_end_flush();
?>


اینم فایل ها که فایل فونت هم توشه. فقط باید فایل فونت رو کنار فایل captcha بذارین!

MRmoon
سه شنبه 22 اسفند 1391, 17:05 عصر
برای ساخت مستطیل هم میتونید از تابع



$recCount = 2;
for($i=0 ; $i < $recCount ; $i++){
$cy = rand() % $height;
$cx = rand() % $width;
$ry = rand() % $MaxRadius;
$rx = rand() % $MaxRadius;
// set line's color (colors are random)
$lineColor = imagecolorallocate($img, rand() % 256, rand() % 256, rand() % 256);
imagerectangle($img, $cx, $cy, $rx, $ry, $lineColor); }