Lavarelのオリジナル検証コード使用

1695 ワード

laravelはheaderヘッダを処理しているため、多くの原生画像生成の書き方がlaravelでは適用されず、書き方を変える必要があります
以下はオリジナルphpの書き方で、通常のphpファイルでもtpフレームワークでも検証コードを正常に出力できます.
           session_start();
            $tatted_code=dechex(mt_rand(0,15));
            for($i=0;$i<3;$i++){
                $tatted_code.=dechex((mt_rand(0,15)));
            }
            $_SESSION['code']=$tatted_code;
            $width=70;
            $height=25;
            $img=imagecreatetruecolor($width, $height);
            $_white=imagecolorallocate($img,236,251,208);
            
            imagefill($img,0,0,$_white);
            $bg=imagecolorallocate($img,204,204,204);
            imagerectangle($img,0,0,$width-1,$height-1,$bg);
            for($i=0;$i

以上の書き方は、laravelでは適用されず、laravelがheaderを処理したため、文字化けしが山積みになります.次のコードはlaravel 5です.2の書き方は、他のバージョンも似ています
Route::get('verify',function(){
    ob_clean();
    ob_start();
    session_start();
    $tatted_code=dechex(mt_rand(0,15));
    for($i=0;$i<3;$i++){
        $tatted_code.=dechex((mt_rand(0,15)));
    }
    $_SESSION['code']=$tatted_code;
    $width = 70;
    $height = 25;
    $img = imagecreatetruecolor($width, $height);
    $_white = imagecolorallocate($img,236,251,208);
            
    imagefill($img,0,0,$_white);
    $bg = imagecolorallocate($img,204,204,204);
    imagerectangle($img,0,0,$width-1,$height-1,$bg);
    for($i=0;$iheader('Content-Type','image/png');
});