Home > front end >  Thinkphp frame display verification code of gibberish, don't walk framework shows normal, why
Thinkphp frame display verification code of gibberish, don't walk framework shows normal, why

Time:10-10

PHP beginners to ask a question about verification code error,
Note: xampp collocation thinkphp framework, Mac, phpstormIDE,

Description:
Is a simple function display verification code,

If directly within the phptorm write the output function of the authentication code (don't use the structure of a class), post code:
& lt; ? PHP
/* *
* Created by PhpStorm.
* the User: templzg
* the Date: 2019-12-09
* Time: o
*/
$x=250; $y=62;
$count=5;
$TheRandomRootString='AVCDEFGHIJKLMNOPQ123456789';
$TheCreatedString=' ';
$LengthOfStr=strlen ($TheRandomRootString) - 1;

For ($I=0; $i<=$count; $I + +)
{
//that is, $TheRandomRootString can be thought of as a character array, when taking the subscript, the subscript value is 0 ~ $LengthOfStr random value,
$TheCreatedString.=$TheRandomRootString [mt_rand (0, $LengthOfStr)];
}
$thecode=$TheCreatedString;
//echo $thecode;
//die ();
$im=imagecreate ($x, $y);

//set captcha text color (actually still should have font)
Imagecolorallocate ($im, mt_rand (50200), mt_rand (0155), mt_rand (0155));
$fontcolor=imagecolorallocate ($im, 225225225);
$fontfile='hanti. The vera.ttf';
//to draw in the image verification code
For ($I=0, $len=strlen ($thecode); $i<$len; $I + +)
{
Imagettftext ($im, 30, mt_rand (0, 20) - mt_rand (0, 25), 40, 32 + $I * mt_rand (30, 50), $fontcolor, $fontfile, $thecode ($I));
}
//add eight interference line, color, location of random

For ($I=0; $i<8; $I + +)
{
$linecolor=imagecolorallocate ($im, mt_rand (0255), mt_rand (0255), mt_rand (0255));
Imageline ($im, mt_rand (0, $x), 0, mt_rand (0, $x), $y, $linecolor);
}
//add 250 pixels
For ($I=0; $i<250; $I + +)
{
Imagesetpixel ($im, mt_rand (0, $x), mt_rand (0, $y), $fontcolor);
}
//clear the cache
Ob_clean ();
//set the header
The header (' the content-type: image/PNG ');
//output images
Imagepng ($im);
Imagedestroy ($im);

-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- line
Can be normal show the verification code:



But specific to use tp5 framework to show, there will be a problem, namely the garbled words


There currently is the garbled:
In the application directory to create a user directory - & gt; The controller directory - & gt; GetCode. All the way down, PHP code posted:

<? PHP
/* *
* Created by PhpStorm.
* the User: templzg
* the Date: 2019-12-07
* Time: those
*/
The namespace app/user/controller;
Use think \ controller;
The class GetCode extends controller
{

//to generate images
Public static function show ($x=150, $y=62)
{

//clear the cache

Ob_clean ();
//set the header
The header (' the content-type: image/PNG ');
$thecode=self: : creat ();
//var_dump ($thecode);
//create image resources, randomly generated background color
$im=imagecreate ($x, $y);

//set captcha text color (actually still should have font)
Imagecolorallocate ($im, mt_rand (50200), mt_rand (0155), mt_rand (0155));
$fontcolor=imagecolorallocate ($im, 255255255);
$fontfile='../thinkphp/library/fonts/hanti. The vera.ttf ';
//to draw in the image verification code
For ($I=0, $len=strlen ($thecode); $i<$len; $I + +)
{
Imagettftext ($im, 30, mt_rand (0, 20) - mt_rand (0, 25), 40, 32 + $I * mt_rand (30, 50), $fontcolor, $fontfile, $thecode ($I));
}
//add eight interference line, color, location of random
For ($I=0; $i<8; $I + +)
{
$linecolor=imagecolorallocate ($im, mt_rand (0255), mt_rand (0255), mt_rand (0255));
Imageline ($im, mt_rand (0, $x), 0, mt_rand (0, $x), $y, $linecolor);
}
//add 250 pixels
For ($I=0; $i<250; $I + +)
{
Imagesetpixel ($im, mt_rand (0, $x), mt_rand (0, $y), $fontcolor);
}

//output images
Imagepng ($im);
Imagedestroy ($im);
}
//here's the count value verification code generated for the specified number of characters
Private static function creat ($count=5)
{
$TheRandomRootString='AVCDEFGHI123456';
$TheCreatedString=' ';
$LengthOfStr=strlen ($TheRandomRootString) - 1;

For ($I=0; $i<$count; $I + +)
{
//that is, $TheRandomRootString can be thought of as a character array, when taking the subscript, the subscript value is 0 ~ $LengthOfStr random value,
$TheCreatedString.=$TheRandomRootString [mt_rand (0, $LengthOfStr)];
}
Return $TheCreatedString;
}
}


//
At present is to configure the virtual host name:
www.tp5.com
Visit the show () operation (function) of the url path is:
http://www.tp5.com/user/GetCode/show

In addition the GD library open


-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- line
In addition in the use of template file to render tie-in img tags tried several:




And SCR using {: url (' GetCode ', 'show')} won't display normal - all is gibberish,

-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- line
In addition, using
"& lt; Img SRC="https://bbs.csdn.net/application/user/GetImage.php" width="580" height='550' & gt;"
Then the GetImage. PHP output verification code function, defined according to the result is a question mark placeholder pictures don't show figure,

I hope someone can give directions, thank you,
  • Related