2013-10-28 3 views
1

Я использовал следующий код для создания Captcha в моей форме. Captcha создает хорошо. Теперь я хочу изменить размер шрифта и пробелы шрифтов. Я не знаю, как изменить следующий код.Captcha validation in PHP

 <?php 
      session_start(); 
      $code= substr(str_shuffle("abcdefghijklmnopqrstuvwxyz"), 0, 6); 
      $_SESSION["code"]=$code; 
      $im = imagecreatetruecolor(150, 35); 
      $bg = imagecolorallocate($im, 255, 255, 255); 
      $fg = imagecolorallocate($im, 0, 0, 0); 
      imagefill($im, 5, 5, $bg); 
      imagestring($im, 5, 8, 8, $code, $fg); 
      header("Cache-Control: no-cache, must-revalidate"); 
      header('Content-type: image/png'); 
      imagepng($im); 
      imagedestroy($im); 
     ?> 

ответ

2

Вы можете использовать этот код, чтобы реализовать простую капчу, используя библиотеку Б-жьей PHP. Как вы новичок, вот пример кода для быстрого тестирования, она охватывает шрифта проклейки также:

<?php 
session_start(); 
header('Content-type: image/jpeg'); 

$text = rand(1000, 9999); 
$font_size = 30; 

$image_width = 200; 
$image_height = 40; 

$image = imagecreate($image_width, $image_height); 
imagecolorallocate($image, 255, 255, 255); 
$text_color = imagecolorallocate($image, 0, 0, 0); 

for ($x=1; $x<=40; $x++) { 
    $x1 = rand(1, 100); 
    $y1 = rand(1, 100); 
    $x2 = rand(1, 100); 
    $y2 = rand(1, 100); 

imageline($image, $x1, $y1, $x2, $y2, $text_color); 
} 

imagettftext($image, $font_size, 0, 15, 30, $text_color, 'FREESCPT.ttf', $text); 
imagejpeg($image); 

?> 

В функции imagettftext:

imagettftext($image, $font_size, $angle, $x, $y, $text_color, '$font-family', $text); 

    $image is the $imagecreate function 
    $font_size is the size of font you want. 
    $angle is the angle of the fonts tilted 
    $x and $y are coordinates. 
    $text_color is the imagecolorallocate function 
    $font-family is the family of font you want to use 
    $text is the text or random text to be displayed 

Вот хороший учебник о том, как построить капчу в php ->link

2

вы можете изменить шрифт, используя функцию imagestring

+0

Я новичок в разработке PHP. Я не знаю, как реализовать в своем коде. Пожалуйста, помогите мне. – Velmurugan

+0

вы можете загрузить шрифт, используя $ font = imageloadfont (''); и передать $ font в imagestring imagestring ($ im, $ font, 8, 8, $ code, $ fg); –

+0

Спасибо другу. Я реализовал шрифт. Как изменить размер шрифта? – Velmurugan