Я создаю изображение с капчей (пожалуйста, не предлагайте предварительно сделанные).
Это дает вам возможность звонить
$captcha = new captcha();
$captcha->size(30)->getImage();
, который устанавливает размер шрифта 30, а затем генерирует капчу.
моя проблема с размерами.
Как определить, насколько широко должно быть изображение?
капча имеет 6 символов.
Я подумал, что смогу просто сделать ($size*6)+10
, чтобы сделать изображение достаточно широким + дать ему отступ по 5 пикселей с каждой стороны (текст по центру), но, видимо, нет.
<ч />
Код
<?php
session_start();
class captcha {
private $font = 'monofont.ttf';
private $characters = '23456789bcdfghjkmnpqrstvwxyz';
private $size = 30;
private $count = 6;
private $colors = array(
'b' => array('r' => 0, 'g' => 0, 'b' => 0),
't' => array('r' => 200, 'g' => 200, 'b' => 200),
'n' => array('r' => 127, 'g' => 127, 'b' => 127)
);
function count($count) {
$this->count = $count;
return $this;
}
function size($size){
$this->size = $size;
return $this;
}
function characters($characters) {
$this->characters = $characters;
return $this;
}
function backgroundColor($red, $green, $blue){
$this->colors['b']['r'] = $red;
$this->colors['b']['g'] = $green;
$this->colors['b']['b'] = $blue;
return $this;
}
function noiseColor($red, $green, $blue){
$this->colors['n']['r'] = $red;
$this->colors['n']['g'] = $green;
$this->colors['n']['b'] = $blue;
return $this;
}
function textColor($red, $green, $blue){
$this->colors['t']['r'] = $red;
$this->colors['t']['g'] = $green;
$this->colors['t']['b'] = $blue;
return $this;
}
function generateCode() {
$code = '';
$i = 0;
while ($i < $this->count) {
$code .= strtoupper(substr($this->characters, mt_rand(0, strlen($this->characters) - 1), 1));
$i++;
}
return $code;
}
function getWidth() {
return ($this->count * $this->size);
}
function getHeight() {
return $this->size+10;
}
function getCaptcha() {
$code = $this->generateCode();
$this->width = $this->getWidth();
$this->height = $this->getHeight();
$image = @imagecreate($this->width, $this->height) or die('Cannot initialize new GD image stream');
//define colors
$tColor = imagecolorallocate($image, $this->colors['t']['r'], $this->colors['t']['g'], $this->colors['t']['b']);
$nColor = imagecolorallocate($image, $this->colors['n']['r'], $this->colors['n']['g'], $this->colors['n']['b']);
$bColor = imagecolorallocate($image, $this->colors['b']['r'], $this->colors['b']['g'], $this->colors['b']['b']);
//fill image
imagefill($image, 0, 0, $bColor);
/* generate random dots in background */
for ($i = 0; $i < ($this->width * $this->height) / 3; $i++) {
imagefilledellipse($image, mt_rand(0, $this->width), mt_rand(0, $this->height), 1, 1, $nColor);
}
/* generate random lines in background */
for ($i = 0; $i < ($this->width * $this->height) / 150; $i++) {
imageline($image, mt_rand(0, $this->width), mt_rand(0, $this->height), mt_rand(0, $this->width), mt_rand(0, $this->height), $nColor);
}
/* create textbox and add text */
$textbox = imagettfbbox($this->size, 0, $this->font, $code) or die('Error in imagettfbbox function');
$x = ($this->width - $textbox[4]) / 2;
$y = ($this->height - $textbox[5]) / 2;
imagettftext($image, $this->size, 0, $x, $y, $tColor, $this->font, $code) or die('Error in imagettftext function');
//imagefilter($image, IMG_FILTER_NEGATE);
imagefilter($image, IMG_FILTER_SMOOTH, 1);
/* output captcha image to browser */
header('Content-Type: image/jpeg');
header('Cache-Control: no-cache, must-revalidate');
imagejpeg($image);
imagedestroy($image);
$_SESSION['security_code'] = $code;
}
}
$captcha = new captcha();
$captcha->size(30)->count(6)->getCaptcha();
?>