PHP, Captcha Pictures, garbled

Source: Internet
Author: User
Can not find out where the error, but the preview is a lot of garbled

Ob_clean ();
Header ("Content-type:image/png");
$img = @imagecreatetruecolor (50,20) or Die ("FJHB");
$BG _color=imagecolorallocate ($img, 255,255,255);
Imagefill ($img, 0,0, $BG _color);

$border _color=imagecolorallocate ($img, 200,200,200);
Imagerectangle ($img, 0,0,49,19, $border _color);
for ($i =2; $i <18; $i + +) {
$line _color=imagecolorallocate ($img, Rand (200,255), Rand (200,255), Rand (200,255));
Imageline ($img, 2, $i, $i, $line _color);
}
$font _size=12;
$str [0]= "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
$str [1]= "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
$str [2]= "01234567891234567890123456";

$imgstr [0]["s"]= $str [rand (0,2)][rand (0,25)];
$IMGSTR [0]["x"]=rand (2,5);
$imgstr [0]["Y"]=rand (1,4);

$imgstr [1]["s"]= $str [rand (0,2)][rand (0,25)];
$IMGSTR [1]["x"]= $imgstr [0]["x"]+ $font _size-1+rand (0,1);
$imgstr [1]["Y"]=rand (1,3);

$imgstr [2]["s"]= $str [rand (0,2)][rand (0,25)];
$IMGSTR [2]["x"]= $imgstr [1]["x"]+ $font _size-1+rand (0,1);
$imgstr [2]["Y"]=rand (1,4);

$imgstr [3]["s"]= $str [rand (0,2)][rand (0,25)];
$IMGSTR [3]["x"]= $imgstr [2]["x"]+ $font _size-1+rand (0,1);
$imgstr [3]["Y"]=rand (1,3);

for ($i =0; $i <4; $i + +) {
$text _color=imagecolorallocate ($img, Rand (50,180), Rand (50,180), Rand (50,180));
Imagechar ($img, $font _size, $imgstr [$i] ["X"], $imgstr [$i] ["Y"], $imgstr [$i] ["s"], $text _color);
}

Imagepng ($IMG);
Imagedestroy ($IMG);
?>


Reply to discussion (solution)

Garbled is the browser display characters instead of pictures?
Close the browser and re-visit this address to try again. If not, it should be the header problem.
Picture output, preferably with the UTF-8 character set, because it is possible to encounter the output of Chinese.
And UTF-8 words, must pay attention to BOM head.

The landlord is very humorous

Ob_clean (); Clears the buffer contents so that even if there is a BOM header, it does not affect the picture
Header ("Content-type:image/png"); The statement followed by PNG image data

So even if the program has a non-grammatical error, at most it is only a fork, not garbled

I have a normal test here.

Check to see if the BOM header is removed.
GD Library and other environment is complete

The main feeling is still the header problem, the header before the other output?

is not the file encoding reason?

Whether the LZ preview is garbled as follows:
This problem occurs because the file is encoded as:
Then you need to change it into
OK, preview the effect:

Last night no matter how I clean the cache, close the browser, changed the code, has been a bunch of garbled, the computer started, and then tested the Web page, incredibly somehow back to normal, completely touch the mind, can only scold broken computer, play my that!!!!

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