How to troubleshoot PHP generated CAPTCHA images do not display problems

Source: Internet
Author: User
Tags border color set background

Recently encountered a problem, is that the verification code on someone else's computer can be displayed, but my own computer up can't. Reason for a long time, haha, finally found! Now let's share:

Program:

<?php

$w = 80; Set picture width and height
$h = 26;
$str = Array (); Used to store random code
$string = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";//random selection of 4 characters, you can also choose more, pay attention to the loop when added, the width of the appropriate adjustment
for ($i = 0; $i < 4; $i + +) {
$STR [$i] = $string [rand (0,35)];
$vcode. = $str [$i];
}
Session_Start (); Enable Hyper-Global variables session
$_session["Vcode"] = $vcode;
$im = Imagecreatetruecolor ($w, $h);
$white = Imagecolorallocate ($im, 255,255,255); First Call set background color
$black = Imagecolorallocate ($im, 0,0,0); Border color
Imagefilledrectangle ($im, 0,0, $w, $h, $white); Draw a rectangular fill
Imagerectangle ($im, 0,0, $w-1, $h-1, $black); Draw a rectangular box
Create Snowflake background
for ($i = 1; $i < $i + +) {
$x = Mt_rand (1, $w-9);
$y = Mt_rand (1, $h-9);
$color = Imagecolorallocate ($im, Mt_rand (200,255), Mt_rand (200,255), Mt_rand (200,255));
Imagechar ($im, 1, $x, $y, "*", $color);
}
Write a captcha to a pattern
for ($i = 0; $i < count ($str); $i + +) {
$x = + $i * ($w-15)/4;
$y = Mt_rand (3, $h/3);
$color = Imagecolorallocate ($im, Mt_rand (0,225), Mt_rand (0,150), Mt_rand (0,225));
Imagechar ($im, 5, $x, $y, $str [$i], $color);
}
Header ("Content-type:image/jpeg"); Output in JPEG format, note that it is not possible to print any characters above, otherwise an error
Imagejpeg ($im);
Imagedestroy ($im);
?>
Verified that it works on someone else's computer, but my own computer is not working. The reasons are as follows:

The first thing to declare is that I thought the GD2 Library did not open, but with phpinfo view, found open. The BOM is cleared and the code is written at the top line, so the problem may appear on the code. Later, after research, we find that we still need to change the program, to precede the header with Ob_clean () This statement, so you can run.

Add before the header output:

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

Header ("Content-type:image/jpeg"); So the problem is solved.

How to troubleshoot PHP generated CAPTCHA images do not display problems

Related Article

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.