我在项目中使用了mews / captcha插件。 但问题是我无法在同一页面中添加多个验证码。
谢谢&问候, Monang Shah
答案 0 :(得分:0)
我没有使用mews / captcha,而是使用自定义验证码。请找到以下代码: -
控制器功能: -
public function get_captcha($id, $width = 120, $height = 30) {
$config = array(
'fontsizes' => array(14, 15, 16, 17, 18),
'length' => 5,
'width' => $width,
'height' => $height,
'space' => 20,
'colors' => array('128,23,23', '128,23,22', '33,67,87', '67,70,83', '31,56,163', '48,109,22', '165,42,166', '18,95,98', '213,99,8'),
'sensitive' => false, // case sensitive (params: true, false)
'quality' => 80 // image quality
);
$fonts = $backgrounds = array();
$assets = __DIR__ . '/../../../captcha/';
foreach (glob($assets . '*.ttf') as $filename) {
$fonts[] = $filename;
}
foreach (glob($assets . '*.png') as $filename) {
$backgrounds[] = $filename;
}
$char = Str::random($config['length']);
Session::put('captchaHash'.$id, ($config['sensitive'] === true ? $char : Str::lower($char)));
$bg_image = $backgrounds[rand(0, count($backgrounds) - 1)];
$bg_image_info = getimagesize($bg_image);
if ($bg_image_info['mime'] == 'image/jpg' || $bg_image_info['mime'] == 'image/jpeg')
{
$old_image = imagecreatefromjpeg($bg_image);
}
elseif ($bg_image_info['mime'] == 'image/gif')
{
$old_image = imagecreatefromgif($bg_image);
}
elseif ($bg_image_info['mime'] == 'image/png')
{
$old_image = imagecreatefrompng($bg_image);
}
$new_image = imagecreatetruecolor($config['width'], $config['height']);
$bg = imagecolorallocate($new_image, 255, 255, 255);
imagefill($new_image, 0, 0, $bg);
imagecopyresampled($new_image, $old_image, 0, 0, 0, 0, $config['width'], $config['height'], $bg_image_info[0], $bg_image_info[1]);
$bg = imagecolorallocate($new_image, 255, 255, 255);
for ($i = 0; $i < strlen($char); $i++)
{
$color_cols = explode(',', $config['colors'][rand(0, count($config['colors']) - 1)]);
$fg = imagecolorallocate($new_image, trim($color_cols[0]), trim($color_cols[1]), trim($color_cols[2]));
imagettftext($new_image, $config['fontsizes'][rand(0, count($config['fontsizes']) - 1)], rand(-10, 15), 10 + ($i * $config['space']), rand($config['height'] - 10, $config['height'] - 5), $fg, $fonts[rand(0, count($fonts) - 1)], $char[$i]);
}
imagealphablending($new_image, false);
header('Cache-Control: no-cache, no-store, max-age=0, must-revalidate');
header('Pragma: no-cache');
header("Content-type: image/jpg");
header('Content-Disposition: inline; filename=' . $id . '.jpg');
imagejpeg($new_image, null, $config['quality']);
imagedestroy($new_image);
}
我已使用
将验证码图像调用到视图中{{ Form::image(SITE_URL."get-captcha/captcha1?".time(),'Captcha image',array('id'=>'captcha1')) }}
注意:为每个验证码动态生成captchaHash。你可以将captcha zip //103.8.216.142/projects/captcha.zip下载到浏览器中
谢谢,Monang