Laravel How to write verification code file, and prevent Chinese garbled.

Source: Internet
Author: User

This example generates a Chinese verification code, which is designed for Laravel.

Controller: Public Function Getvcode (Request $request) {$width = 845;        $height = 125; $fp =fopen (the URL ('/'). '        /front/register/chengyu.txt ', ' r ');//open local file with 150 idioms stored in it! One line.        $hang =rand (0,149); for ($i = 0;;            $i + +) {$get =fgets ($fp);        if ($i = = $hang) break;        } $text = $get;//Get the Idiom string header (' Content-type:image/jpeg ');        1. Create Canvas $img = Imagecreatetruecolor ($width, $height);        1-1. Background color random 0~125 dark 126~255 Light $bgColor = Imagecolorallocate ($img, Rand (126,255), Rand (126,255), Rand (126,255));        Imagefill ($img, 0,0, $bgColor); 2, write or draw//2-1, draw point for ($i = 0; $i <= ($height * $width/15); $i + +) {$pixX = rand (5, $width-5); The position of the random x $pixY = rand (5, $height-5);//The position of the random y $pixColor = imagecolorallocate ($img, Rand (0,125), rand (0,1            ), Rand (0,125));        Imagesetpixel ($img, $pixX, $pixY, $pixColor); }//2-2, write $code = ' AbcdefghijklmnoPqrstuvwxyz ';        $len = Mb_strlen ($text, ' UTF-8 ');            for ($j = 1; $j <= 4; $j + +) {//font size randomly from 15 pixels to pixels $fontSize = rand (($height/2-5), ($height/2+10)); The position of x $fontX = ($width/4) * ($J-1);//25//y Position $fontY = $height *0.            75;            Font Color $fontColor = Imagecolorallocate ($img, Rand (0,125), Rand (0,125), Rand (0,125));            Each random word $fontText = Mb_substr ($text, $j -1,1, ' UTF-8 ');//Read a Chinese character//echo $fontText; Imagettftext ($img, $fontSize, 0, $fontX, $fontY, $fontColor, $_server[' Document_root '). ' /front/register/fzstk.        TTF ', $fontText);        }//3, Output or save imagejpeg ($img); 4, Analytic template view (' Front.register.showvcode ', [' dd ' = imagejpeg ($img)]);
5 Destruction of resources
Image_destroy ($IMG); }

Address one of the following issues:

1 laravel Routing and template files are self-designed.

2 fopen Open the file under the public directory:

$fp =fopen (the URL ('/'). ' /front/register/chengyu.txt ', ' R ');

3 intercepting Chinese characters, UTF8 format, using mb_substr

Mb_substr ($text, $j -1,1, ' UTF-8 ');

4 TTF file access

$_server[' Document_root ']. ' /front/register/fzstk. TTF '

5 variables brought to the template

[' dd ' = imagejpeg ($img)]

6 file saved as Utf-8 no BOM format , and note whether TTF is going to output English or Chinese !! Don't be mistaken.

Laravel How to write verification code file, and prevent Chinese garbled.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.