|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright (c) 2019. PHP-QuickORM Captcha |
| 4 | + * Author: Rytia Leung |
| 5 | + |
| 6 | + * Github: github.com/php-quickorm/captcha |
| 7 | + */ |
| 8 | + |
| 9 | +class Captcha |
| 10 | +{ |
| 11 | + private $str = "ABCDEFGHIJKLMNOPQRSTUVWXYZaabcdefghijklmnopqrstuvwxyz1234567890"; |
| 12 | + private $font; |
| 13 | + private $im; |
| 14 | + private $level; |
| 15 | + private $code; |
| 16 | + |
| 17 | + /** |
| 18 | + * Captcha constructor. |
| 19 | + * @param int $level |
| 20 | + */ |
| 21 | + public function __construct($level = 2) |
| 22 | + { |
| 23 | + $this->level = $level; |
| 24 | + |
| 25 | + // load the truetype font |
| 26 | + $this->font = realpath('./arial.ttf'); |
| 27 | + |
| 28 | + // create image |
| 29 | + $this->im = imagecreate(200, 77); |
| 30 | + imagecolorallocate($this->im, 255, 255, 255); |
| 31 | + |
| 32 | + // generate |
| 33 | + $this->generate(); |
| 34 | + |
| 35 | + // add lines |
| 36 | + if ($this->level == 3) { |
| 37 | + $this->addLines(); |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + /** |
| 42 | + * @return resource |
| 43 | + */ |
| 44 | + public function getImageResource() |
| 45 | + { |
| 46 | + return $this->im; |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * @return string |
| 51 | + */ |
| 52 | + public function getCode() |
| 53 | + { |
| 54 | + return $this->code; |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * send http response with the captcha image |
| 59 | + */ |
| 60 | + public function render() |
| 61 | + { |
| 62 | + header('Content-Type: image/png'); |
| 63 | + |
| 64 | + imagepng($this->im); |
| 65 | + imagedestroy($this->im); |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * generate the code and image |
| 70 | + */ |
| 71 | + private function generate() |
| 72 | + { |
| 73 | + $output = ''; |
| 74 | + $length = strlen($this->str); |
| 75 | + |
| 76 | + |
| 77 | + for ($i = 1; $i < 5; $i++) { |
| 78 | + // get random char |
| 79 | + $char = $this->str[rand(0, $length - 1)]; |
| 80 | + $output .= $char; |
| 81 | + |
| 82 | + // get font size |
| 83 | + $fontSize = ($this->level > 1) ? rand(20, 48) : 28; |
| 84 | + imagettftext($this->im, $fontSize, rand(-35, 35), 35 * $i, 55, imagecolorallocate($this->im, rand(0, 240), rand(0, 240), rand(0, 240)), $this->font, $char); |
| 85 | + } |
| 86 | + |
| 87 | + $this->code = $output; |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * add line to image if level > 2 |
| 92 | + */ |
| 93 | + private function addLines() |
| 94 | + { |
| 95 | + |
| 96 | + $lines = rand(1, 3); |
| 97 | + for ($i = 0; $i < $lines; $i++) { |
| 98 | + imageline($this->im, rand(0, 200), rand(0, -77), rand(0, 200), rand(77, 144), imagecolorallocate($this->im, rand(0, 240), rand(0, 240), rand(0, 240))); |
| 99 | + } |
| 100 | + |
| 101 | + for ($i = 0; $i < 5 - $lines; $i++) { |
| 102 | + imageline($this->im, rand(0, -200), rand(0, 77), rand(200, 400), rand(0, 77), imagecolorallocate($this->im, rand(0, 240), rand(0, 240), rand(0, 240))); |
| 103 | + } |
| 104 | + } |
| 105 | +} |
0 commit comments