Created
June 11, 2021 10:56
-
-
Save dsibi/a0bee0fca2ce373a62333c65b05af3d1 to your computer and use it in GitHub Desktop.
С использованием ImageMagick реализовать простейший генератор CAPTCHA, формирующий картинку со случайным набором 5 цифр.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$width = 100; //Ширина изображения | |
$height = 60; //Высота изображения | |
$font_size = 16; //Размер шрифта | |
$let_amount = 5; //Количество цифр, которые нужно набрать | |
$fon_let_amount = 30; //Количество цифр на фоне | |
$font = "fonts/cour.ttf"; //Путь к шрифту | |
//набор символов | |
$numbers = array(1,2,3,4,5,6,7,8,9,0); | |
//цвета | |
$colors = array("90","110","130","150","170","190","210"); | |
$src = imagecreatetruecolor($width,$height); //создаем изображение | |
$fon = imagecolorallocate($src,255,255,255); //создаем фон | |
imagefill($src,0,0,$fon); //заливаем изображение фоном | |
for($i=0;$i < $fon_let_amount;$i++) //добавляем на фон цифры | |
{ | |
//случайный цвет | |
$color = imagecolorallocatealpha($src,rand(0,255),rand(0,255),rand(0,255),100); | |
//случайный символ | |
$number = $numbers[rand(0,sizeof($numbers)-1)]; | |
//случайный размер | |
$size = rand($font_size-2,$font_size+2); | |
imagettftext($src,$size,rand(0,45), | |
rand($width*0.1,$width-$width*0.1), | |
rand($height*0.2,$height),$color,$font,$number); | |
} | |
for($i=0;$i < $let_amount;$i++) //то же самое для цифр | |
{ | |
$color = imagecolorallocatealpha($src,$colors[rand(0,sizeof($colors)-1)], | |
$colors[rand(0,sizeof($colors)-1)], | |
$colors[rand(0,sizeof($colors)-1)],rand(20,40)); | |
$number = $numbers[rand(0,sizeof($numbers)-1)]; | |
$size = rand($font_size*2-2,$font_size*2+2); | |
$x = ($i+1)*$font_size + rand(1,5); //даем каждому символу случайное смещение | |
$y = (($height*2)/3) + rand(0,5); | |
$cod[] = $number; //запоминаем код | |
imagettftext($src,$size,rand(0,15),$x,$y,$color,$font,$number); | |
} | |
$cod = implode("",$cod); //переводим код в строку | |
header ("Content-type: image/gif"); //выводим готовую картинку | |
imagegif($src); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment