douxu2467 2014-12-14 14:02
浏览 24

当从rand()函数获取输入字符串时,为什么imagettftext()将不同的字符串值作为输入?

I am trying to create a captcha image and store the value in session variable.

Captcha.php

<?php
session_start();

function rand_str() {
    $range = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';
    $i0=rand(0,35);
    $i1=rand(0,35);
    $i2=rand(0,35);
    $i3=rand(0,35);
    return $range[$i0].$range[$i1].$range[$i2].$range[$i3];
}
$str_captcha = rand_str();
$_SESSION['captcha'] = $str_captcha;

$im       = imagecreatetruecolor(150, 70);
$color    = imagecolorallocate($im, 0xFF, 0xFF, 0xFF);
imagettftext($im, 20, 0, 30, 30, $color, 'fonts/Ewert-Regular.ttf', $str_captcha);

header('Content-type: image/png');
header('Pragma: no-cache');
header('Cache-Control: no-store, no-cache, proxy-revalidate');
imagepng($im);
imagedestroy($im);
?>

Check.php

<?php
session_start();
echo $_SESSION['captcha'].'<br>';
?>

My Test Cases


Scenario 1 : For Example, if Check.php gives DMJF (generated actual value), then Captcha.php gives different image, for instance, 77B0.

If i use

$str_captcha = "ABCD";

instead of

$str_captcha = rand_str();

it works fine. Captcha.php gives ABCD image and Check.php gives ABCD string.

Scenario 2 : If i use the following

$i0=11;
$i1=12;
$i2=13;
$i3=14;

instead of

$i0=rand(0,35);
$i1=rand(0,35);
$i2=rand(0,35);
$i3=rand(0,35);

it also works fine. Captcha.php gives BCDF image and Check.php gives BCDF string.

Scenario 3 : If i use the following

$i0=11;
$i1=12;
$i2=rand(0,35);
$i3=rand(0,35);

it also works fine for last two characters. but the first two characters are different. For Example, if Check.php gives BCJF (generated actual value), then Captcha.php gives partially correct image, for instance, BC0M.

Please help me that why does the function imagettftext() takes a different value if I use rand() function.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
    • ¥15 python的qt5界面
    • ¥15 无线电能传输系统MATLAB仿真问题
    • ¥50 如何用脚本实现输入法的热键设置
    • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
    • ¥30 深度学习,前后端连接
    • ¥15 孟德尔随机化结果不一致
    • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
    • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
    • ¥15 谁有desed数据集呀