gpt4 book ai didi

php - session 与图像 PHP 不匹配

转载 作者:搜寻专家 更新时间:2023-10-31 21:44:27 25 4
gpt4 key购买 nike

我想知道为什么此脚本生成的图像中的字符与为 session 设置的字符不匹配。我找不到为什么会这样,如果有人可以提供帮助,那就太好了。谢谢,莉亚。

// 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;

更新:在测试时我注意到第一次加载图像时, session 未设置,因此当图像刷新时, session 设置为最后一个 $str值

最佳答案

如果您的 header 已发送,您可能无法调用 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;

关于php - session 与图像 PHP 不匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6163614/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com