Просмотр файла system/view/captcha.php

Размер файла: 2.29Kb
  1. <?php
  2.  
  3. # Конфигурация картинки
  4. $width = 100; # Ширина картинки
  5. $height = 40; # Высота картинки
  6. $font_size = 10; # Размер шрифта
  7. $let_amount = 4; # Кол-во символов на картинке
  8. $fon_let_amount = 10; # Кол-во символов на фоне
  9. $path_fonts = '../../template/fonts/'; # Путь к шрифтам
  10. # Используемые массивы
  11. $letters = array('0', '2', '3', '4', '5', '6', '7', '9', 'a', 'b', 'c', 'd', 'e', 'g', 'h', 'm', 'z', 'x', 'w', 'q'); # Символы на картинке
  12. $colors = array('10', '30', '50', '70', '90', '110', '130', '150', '170', '190', '210'); # Цвета картинки
  13.  
  14. # Создание картинки
  15. $src = imagecreatetruecolor($width, $height);
  16. $fon = imagecolorallocate($src, 255, 255, 255);
  17.  
  18. imagefill($src, 0, 0, $fon);
  19.  
  20. # Выбор шрифта
  21. $fonts = array();
  22.  
  23. $dir = opendir($path_fonts);
  24.  
  25. while ($fontName = readdir($dir)) {
  26. if ($fontName != '.' && $fontName != '..') $fonts[] = $fontName;
  27. }
  28.  
  29. closedir($dir);
  30.  
  31. # Добавление символов на фон
  32. for ($i=0;$i<$fon_let_amount;$i++) {
  33. $color = imagecolorallocatealpha($src, rand(0, 255), rand(0, 255), rand(0, 255), 100);
  34. $font = $path_fonts . $fonts[rand(0, sizeof($fonts) - 1)];
  35. $letter = $letters[rand(0, sizeof($letters) - 1)];
  36. $size = rand($font_size - 2, $font_size + 2);
  37. imagettftext($src, $size, rand(0, 45), rand($width * 0.1, $width - $width * 0.1), rand($height * 0.2, $height), $color, $font, $letter);
  38. }
  39.  
  40. # Добавление основных символов
  41. for ($i=0;$i<$let_amount;$i++) {
  42. $color = imagecolorallocatealpha($src, $colors[rand(0, sizeof($colors) - 1)], $colors[rand(0, sizeof($colors) - 1)], $colors[rand(0, sizeof($colors) - 1)], rand(20, 40));
  43. $font = $path_fonts . $fonts[rand(0, sizeof($fonts) - 1)];
  44. $letter = $letters[rand(0, sizeof($letters) - 1)];
  45. $size = rand($font_size * 2.1 - 2, $font_size * 2.1 + 2);
  46. $x = ($i + 1) * $font_size + rand(4, 7);
  47. $y = (($height * 2) / 3) + rand(0, 5);
  48. $cod[] = $letter;
  49. imagettftext($src, $size, rand(0, 15), $x, $y, $color, $font, $letter);
  50. }
  51.  
  52. # Запуск сессии
  53. session_name('PSID');
  54. session_start();
  55.  
  56. # Сохранение кода в сессию
  57. $_SESSION['captcha'] = implode('', $cod);
  58.  
  59. # Вывод изображения
  60. header('Content-type: image/gif');
  61. imagegif($src);