رتبه موضوع:
  • 0 رای - 0 میانگین
  • 1
  • 2
  • 3
  • 4
  • 5
تغییر ظاهر captcha تولید شده با Yii
#1
سلام
ابتدا در مسیر protectedcomponents کلاس جدیدی ایجاد میکنیم:
class MyCaptcha extends CCaptchaAction
{
    protected function generateVerifyCode()
    {
 $code = array_merge(range('a', 'z'), range('0', '9'));
 $str = '';
 for($i = 0; $i < 6; $i++) {
 $str .= $code[rand() % count($code)];
 }
        return (string)$str;
    }
}
در فایل siteController.php متد actions را پیدا کرده و نام کلاس جدیدی که ساخته ایم را وارد میکنیم:
public function actions()
 {
 return array(
 'captcha'=>array(
 'class'=>'MyCaptcha',
 'backColor'=>0x0088EE,
 ),
 );
 }
بعد به مسیر Yiiframeworkwebwidgetscaptcha میرویم و فایل CCaptchaAction.php را باز کرده و در متد renderImageGD تغییرات مورد نظرمان را اعمال میکنیم:
protected function renderImageGD($code)
 {
 $image = imagecreatetruecolor($this->width,$this->height);

 $backColor = imagecolorallocate($image,
 (int)($this->foreColor % 0x1000000 / 0x10000),
 (int)($this->foreColor % 0x10000 / 0x100),
 $this->foreColor % 0x100);

 imagefilledrectangle($image,0,0,$this->width,$this->height,$backColor);
 imagecolordeallocate($image,$backColor);

 if($this->transparent)
 imagecolortransparent($image,$backColor);

 $foreColor = imagecolorallocate($image, 210, 240, 255);

        $lineColor = imagecolorallocate($image, 150, 120, 255);

 if($this->fontFile === null)
 $this->fontFile = dirname(__FILE__) . '/comicz.ttf';

 $length = strlen($code);
 $box = imagettfbbox(30,0,$this->fontFile,$code);
 $w = $box[4] - $box[0] + $this->offset * ($length - 1);
 $h = $box[1] - $box[5];
 $scale = min(($this->width - $this->padding * 2) / $w,($this->height - $this->padding * 2) / $h);
 $x = 10;
 $y = round($this->height * 27 / 40);
 for($i = 0; $i < $length; ++$i)
 {
 $fontSize = (int)(rand(30,35) * $scale * 0.8);
 $angle = rand(-10,10);
 $letter = $code[$i];
 $box = imagettftext($image,$fontSize,$angle,$x,$y,$foreColor,$this->fontFile,$letter);
 $x = $box[2] + $this->offset;
 }

 imagecolordeallocate($image,$foreColor);

        // Add lines to the image
        $lineCount= rand(5, 8);
 for($i = 0; $i < $lineCount; $i++) {
 $x1 = rand(10, $w);
 $y1 = rand(10, $h/3);
 $x2 = rand(10, $w/3);
 $y2 = rand(10, $h);
 imageline($image, $x1, $y1, $x2, $y2, $lineColor);
 }

 header('Pragma: public');
 header('Expires: 0');
 header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
 header('Content-Transfer-Encoding: binary');
 header("Content-Type: image/jpeg");
 imagejpeg($image);
 imagedestroy($image);
 }
برای اعمال تغییرات، از آموزشی که استاد شهرکی در این انجمن ارائه کرده اند، استفاده شده است. با تشکر از استاد شهرکی.
پاسخ
تشکر شده توسط:




کاربران در حال بازدید این موضوع: 1 مهمان