PHP generated captcha picture does not show problems _php tutorial

Source: Internet
Author: User
Today in doing a PHP verification code program found that the generated pictures do not appear, and began to think that PHP GD library is not open, check with Phpinfo is open ah, the following small series to introduce this problem solution.

Clear the BOM, the code is also the top line to start writing, GD Library is also open, from here to look at the estimate is not the problem of GD library, may be in the code of the program.

Code to generate the verification code:

The code is as follows Copy Code

/*
* Verification Code Generation Program
*/
$letter = ";
Get random numbers
for ($i =0; $i <2; $i + +) {
$letter. = Chr (Mt_rand (48,57));
}
Get random letters
for ($i =0; $i <2; $i + +) {
$letter. = Chr (Mt_rand (65,90));
}
Refactoring character Order
$strs = Str_split ($letter);
Shuffle ($STRS);
$rndstring = "";
while (list (, $str) = each ($strs)) {
$rndstring. = $str;
}


If GD is supported, the drawing
if (function_exists ("Imagecreate"))
{
Write cookies to the browser
Setcookie ("Zjs_ckstr", MD5 (Strtolower ($rndstring)), Time () +300, '/');//Verification code valid for 5 minutes
$rndcodelen = strlen ($rndstring);
Picture size
$im = Imagecreate (100,30);
$im = Imagecreatefromgif ("Code.gif");
Font
$font _type = dirname (dirname (__file__)). " /data/font/avantgardebookbt.ttf ";
Background color
$backcolor = Imagecolorallocate ($im, 255,255,255);
Font Color
Imagettftext not supported
$fontColor = Imagecolorallocate ($im, 0,0,0);
Support Imagettftext
$fontColor 2 = imagecolorallocate ($im, 0,0,0);
Shadow
$fontColor 1 = imagecolorallocate ($im, 255,255,25);
Add background clutter
$pixColor = Imagecolorallocate ($im, 199, 199, 199);//Miscellaneous color
for ($j =0; $j <1000; $j + +) {
Imagesetpixel ($im, Rand (0,100), Rand (0,30), $pixColor);
}
Add a background line
for ($j =0; $j <=3; $j + +) {
Background Line Color
$lineColor 1 = imagecolorallocate ($im, rand (0, 255), rand (0, 255), rand (0, 255));
Background line direction size
Imageline ($im, Rand (0,40), Rand (3,25), Rand (40,88), Rand (3,25), $lineColor 1);
}


$strposs = Array ();
Text
for ($i =0; $i < $rndcodelen; $i + +) {
if (function_exists ("Imagettftext")) {
$strposs [$i][0] = $i *16+17;//x axis
$strposs [$i][1] = Mt_rand (20,23);//y axis
Imagettftext ($im, 5, 5, $strposs [$i][0]+1, $strposs [$i][1]+1, $fontColor 1, $font _type, $rndstring [$i]);
} else{
Imagestring ($im, 5, $i *16+7, Mt_rand (2, 4), $rndstring [$i], $fontColor);
}
}
Text
for ($i =0; $i < $rndcodelen; $i + +) {
if (function_exists ("Imagettftext")) {
Imagettftext ($im, 16,5, $strposs [$i][0]-1, $strposs [$i][1]-1, $fontColor 2, $font _type, $rndstring [$i]);
}
}


Header ("Pragma:no-cachern");
Header ("Cache-control:no-cachern");
Header ("Expires:0rn");
Output a specific type of picture format, with a priority of GIF-, JPG
if (function_exists ("Imagegif")) {
Header ("CONTENT-TYPE:IMAGE/GIFRN");
Imagegif ($im);
}else{
Header ("Content-type:image/jpegrn");
Imagejpeg ($im);
}
Imagedestroy ($im);
}
?>

Feeling is not no problem, and then Baidu found a man said the key is to join the Ob_clean, this let me think of the reason.

Solutions

Ob_clean (); Key code to prevent the ' image cannot be displayed because of its own error '.


Before the header output is added

The code is as follows Copy Code


Header (' content-type:image/png ');

http://www.bkjia.com/PHPjc/633063.html www.bkjia.com true http://www.bkjia.com/PHPjc/633063.html techarticle today in doing a PHP verification code program found that the generated pictures do not appear, began to think that PHP GD library is not open, check with phpinfo is open, ah, the following small series to everyone ...

  • 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.