1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192 |
- <?php
- namespace App\Admin\Controllers;
- use Dcat\Admin\Http\Controllers\AdminController;
- use Illuminate\Support\Facades\Session;
- class CaptchaController extends AdminController
- {
- public function generate()
- {
-
- $characters = '23456789';
- $captchaString = '';
- for ($i = 0; $i < 6; $i++) {
- $captchaString .= $characters[rand(0, strlen($characters) - 1)];
- }
-
- $width = 120;
- $height = 40;
- $image = imagecreate($width, $height);
-
- $backgroundColor = imagecolorallocate($image, 243, 243, 243);
- $textColor = imagecolorallocate($image, 51, 51, 51);
- $lineColor = imagecolorallocate($image, 204, 204, 204);
-
- for ($i = 0; $i < 5; $i++) {
- $lineColor = imagecolorallocate(
- $image,
- rand(0, 255),
- rand(0, 255),
- rand(0, 255)
- );
- imageline(
- $image,
- rand(0, $width),
- rand(0, $height),
- rand(0, $width),
- rand(0, $height),
- $lineColor
- );
- }
-
- $fontSize = 5;
- $xOffset = 10;
- for ($i = 0; $i < strlen($captchaString); $i++) {
- $textColor = imagecolorallocate(
- $image,
- rand(0, 150),
- rand(0, 150),
- rand(0, 150)
- );
-
- $x = $xOffset + ($i * 15) + rand(-2, 2);
- $y = rand(10, $height - imagefontheight($fontSize));
- imagestring(
- $image,
- $fontSize,
- $x,
- $y,
- $captchaString[$i],
- $textColor
- );
- }
-
- Session::put('captcha', $captchaString);
-
- ob_start();
- imagepng($image);
- $imageData = ob_get_clean();
-
- imagedestroy($image);
- return response($imageData)->header('Content-Type', 'image/png');
- }
- }
|