MMSHFE
جمعه 21 شهریور 1393, 21:00 عصر
اول از همه کد رو ببینید:
<?php
$yii = dirname(__FILE__) . '/../../framework/yii.php';
$config = dirname(__FILE__) . '/../protected/config/main.php';
require_once ($yii);
Yii::createWebApplication($config);
/**
* Simple and powerful CAPTCHA
* @author Mohammad Mostafa Shahreki
* @copyright All rights reserved for Saina Intelligent Processing Co.
* @link http://www.ncis.ir
*/
// Configuration
$height = 75;
$width = 190;
$length = 4;
$lineCount = 5;
$circleCount = 5;
$maxRadius = 50;
$space = 45;
// Generate random code
$alpha = range('A', 'Z');
$string = '';
for($i = 0; $i < $length; $i++) {
$string .= $alpha [rand() % count($alpha)];
}
Yii::app()->session->open();
Yii::app()->session->add('captcha', strtolower($string));
// Create image
header('Content-type: image/png');
$im = imagecreate($width, $height);
// Create fixed colors
$backColor = imagecolorallocate($im, 255, 255, 255);
// Asign font
$font = './coopbl.ttf';
$fontsize = $height * 0.5;
// Calculate the bounding box of the text
$box = imagettfbbox($fontsize, 0, $font, $string);
// 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($im, $width / 2, $height / 2, $backColor);
// Write the text in the image using font, rotation, position, etc.
for($i = 0; $i < $length; $i++) {
$char = substr($string, $i, 1);
$textColor = imagecolorallocate($im, rand(0, 255), rand(0, 255), rand(0, 255));
imagettftext($im, $fontsize, (rand() % 15 * $direction), $positionX, ($height - $box [5]) / 2, $textColor, $font, $char);
$direction = -$direction;
$positionX += $space;
}
// Add lines to the image
for($i = 0; $i < $lineCount; $i++) {
$x1 = rand() % $width;
$y1 = rand() % $height;
$x2 = rand() % $width;
$y2 = rand() % $height;
$lineColor = imagecolorallocate($im, rand() % 256, rand() % 256, rand() % 96);
imageline($im, $x1, $y1, $x2, $y2, $lineColor);
}
// Add circles to the image
for($i = 0; $i < $circleCount; $i++) {
$cx = rand() % $width;
$cy = rand() % $height;
$rx = rand() % $maxRadius;
$ry = rand() % $maxRadius;
$start = rand() % 360;
$end = rand() % 360;
$lineColor = imagecolorallocate($im, rand() % 256, rand() % 256, rand() % 96);
imagearc($im, $cx, $cy, $rx, $ry, $start, $end, $lineColor);
}
imagepng($im);
imagedestroy($im);
?>
یک نمونه از این کپچا اینجا استفاده شده: http://irgoogle.sainanet.ir/register
123373
فقط دو خط اول رو اصلاح کنید که به مسیر فریمورک و فایل تنظیمات پروژه شما اشاره کنه. حالا توی برنامه خودتون میتونید با Yii::app()->session->get('captcha') کد کپچا رو بخونید. من توی پروژه، این فایل رو در مسیر webroot/images گذاشتم و فونت رو هم همونجا کپی کردم و اینطوری کپچا رو توی صفحه نشون دادم:
echo CHtml::image(Yii::app()->homeUrl . 'images/captcha.php', 'CAPTCHA');
اگه ابهام یا مشکلی بود در خدمتم.
<?php
$yii = dirname(__FILE__) . '/../../framework/yii.php';
$config = dirname(__FILE__) . '/../protected/config/main.php';
require_once ($yii);
Yii::createWebApplication($config);
/**
* Simple and powerful CAPTCHA
* @author Mohammad Mostafa Shahreki
* @copyright All rights reserved for Saina Intelligent Processing Co.
* @link http://www.ncis.ir
*/
// Configuration
$height = 75;
$width = 190;
$length = 4;
$lineCount = 5;
$circleCount = 5;
$maxRadius = 50;
$space = 45;
// Generate random code
$alpha = range('A', 'Z');
$string = '';
for($i = 0; $i < $length; $i++) {
$string .= $alpha [rand() % count($alpha)];
}
Yii::app()->session->open();
Yii::app()->session->add('captcha', strtolower($string));
// Create image
header('Content-type: image/png');
$im = imagecreate($width, $height);
// Create fixed colors
$backColor = imagecolorallocate($im, 255, 255, 255);
// Asign font
$font = './coopbl.ttf';
$fontsize = $height * 0.5;
// Calculate the bounding box of the text
$box = imagettfbbox($fontsize, 0, $font, $string);
// 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($im, $width / 2, $height / 2, $backColor);
// Write the text in the image using font, rotation, position, etc.
for($i = 0; $i < $length; $i++) {
$char = substr($string, $i, 1);
$textColor = imagecolorallocate($im, rand(0, 255), rand(0, 255), rand(0, 255));
imagettftext($im, $fontsize, (rand() % 15 * $direction), $positionX, ($height - $box [5]) / 2, $textColor, $font, $char);
$direction = -$direction;
$positionX += $space;
}
// Add lines to the image
for($i = 0; $i < $lineCount; $i++) {
$x1 = rand() % $width;
$y1 = rand() % $height;
$x2 = rand() % $width;
$y2 = rand() % $height;
$lineColor = imagecolorallocate($im, rand() % 256, rand() % 256, rand() % 96);
imageline($im, $x1, $y1, $x2, $y2, $lineColor);
}
// Add circles to the image
for($i = 0; $i < $circleCount; $i++) {
$cx = rand() % $width;
$cy = rand() % $height;
$rx = rand() % $maxRadius;
$ry = rand() % $maxRadius;
$start = rand() % 360;
$end = rand() % 360;
$lineColor = imagecolorallocate($im, rand() % 256, rand() % 256, rand() % 96);
imagearc($im, $cx, $cy, $rx, $ry, $start, $end, $lineColor);
}
imagepng($im);
imagedestroy($im);
?>
یک نمونه از این کپچا اینجا استفاده شده: http://irgoogle.sainanet.ir/register
123373
فقط دو خط اول رو اصلاح کنید که به مسیر فریمورک و فایل تنظیمات پروژه شما اشاره کنه. حالا توی برنامه خودتون میتونید با Yii::app()->session->get('captcha') کد کپچا رو بخونید. من توی پروژه، این فایل رو در مسیر webroot/images گذاشتم و فونت رو هم همونجا کپی کردم و اینطوری کپچا رو توی صفحه نشون دادم:
echo CHtml::image(Yii::app()->homeUrl . 'images/captcha.php', 'CAPTCHA');
اگه ابهام یا مشکلی بود در خدمتم.