Сессия не соответствует изображению PHP - PullRequest
3 голосов
/ 28 мая 2011

Мне интересно, почему символы на изображении, сгенерированные этим сценарием, не соответствуют символам, установленным для сеанса.Я не могу понять, почему, если кто-то может помочь, это было бы здорово.Спасибо, Ли.

// Font directory + font name
$font = 'wordpress/wp-content/themes/boldy/fonts/SF Juggernaut Bold.ttf';
// Total number of lines
$lineCount = 20;
// Size of the font
$fontSize = 19;
// Height of the image
$height = 38;
// Width of the image
$width = 120;
$img_handle = imagecreate ($width, $height) or die ("Cannot Create image");
// Set the Background Color RGB
$backColor = imagecolorallocate($img_handle, 242, 242, 242);
// Set the Line Color RGB
$lineColor = imagecolorallocate($img_handle, 207, 207, 207);
// Set the Text Color RGB
$txtColor = imagecolorallocate($img_handle, 95, 95, 95);

// Generate
$string = "bcdefghijklmopqrstwxyz023456789";
for($i=0;$i<6;$i++){
    $pos = rand(0,36);
    $str .= $string{$pos};
}

$textbox = imagettfbbox($fontSize, 0, $font, $str) or die('Error in imagettfbbox function');

$x = ($width - $textbox[4])/2;
$y = ($height - $textbox[5])/2;

imagettftext($img_handle, $fontSize, 0, $x, $y, $txtColor, $font , $str) or die('Error in imagettftext function');

for($i=0;$i<$lineCount;$i++){
    $x1 = rand(0,$width);$x2 = rand(0,$width);
    $y1 = rand(0,$width);$y2 = rand(0,$width);
    imageline($img_handle,$x1,$y1,$x2,$y2,$lineColor);
}

header('Content-Type: image/jpeg');
imagejpeg($img_handle,NULL,100);
imagedestroy($img_handle);

session_start();
$_SESSION['img_number'] = $str;

ОБНОВЛЕНИЕ: После тестирования я заметил, что при первой загрузке изображения сеанс НЕ устанавливается, поэтому при обновлении изображениясеанс устанавливается с последним значением $ str

Ответы [ 3 ]

1 голос
/ 28 мая 2011

У вас могут возникнуть проблемы с вызовом session_start (), если ваши заголовки уже отправлены.

Изменить

header('Content-Type: image/jpeg');
imagejpeg($img_handle,NULL,100);
imagedestroy($img_handle);

session_start();
$_SESSION['img_number'] = $str;

на

session_start();
header('Content-Type: image/jpeg');
imagejpeg($img_handle,NULL,100);
imagedestroy($img_handle);

$_SESSION['img_number'] = $str;
0 голосов
/ 29 мая 2011
session_start();

// Font directory + font name
$font = 'wordpress/wp-content/themes/boldy/fonts/SF Juggernaut Bold.ttf';
// Total number of lines
$lineCount = 20;
// Size of the font
$fontSize = 19;
// Height of the image
$height = 38;
// Width of the image
$width = 120;
$img_handle = imagecreate ($width, $height) or die ("Cannot Create image");
// Set the Background Color RGB
$backColor = imagecolorallocate($img_handle, 242, 242, 242);
// Set the Line Color RGB
$lineColor = imagecolorallocate($img_handle, 207, 207, 207);
// Set the Text Color RGB
$txtColor = imagecolorallocate($img_handle, 95, 95, 95);

// Generate
$string = "bcdefghijklmopqrstwxyz023456789";
$str = '';
for($i=0;$i<6;$i++){
    $pos = rand(0,36);
    $str .= substr($string, $pos, 1);
}

$_SESSION['img_number'] = $str;

$textbox = imagettfbbox($fontSize, 0, $font, $str) or die('Error in imagettfbbox function');

$x = ($width - $textbox[4])/2;
$y = ($height - $textbox[5])/2;

imagettftext($img_handle, $fontSize, 0, $x, $y, $txtColor, $font , $str) or die('Error in imagettftext function');

for($i=0;$i<$lineCount;$i++){
    $x1 = rand(0,$width);$x2 = rand(0,$width);
    $y1 = rand(0,$width);$y2 = rand(0,$width);
    imageline($img_handle,$x1,$y1,$x2,$y2,$lineColor);
}

header('Content-Type: image/jpeg');
imagejpeg($img_handle,NULL,100);
imagedestroy($img_handle);
0 голосов
/ 28 мая 2011

Если вы отлаживаете скрипт с помощью firebug или devtools, он дважды вызывает ваше изображение.

Попробуйте отключить firebug / devtools, и вы увидите PROFIT.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...