Php, verification code Image, garbled

Source: Internet
Author: User
Php, verification code Image, garbled code cannot be found, but a lot of garbled code will appear during preview

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, $ 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, 47, $ 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 );
$ 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 );
$ 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 );
$ 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)

Is garbled characters rather than images displayed in the browser?
Close your browser and try again. If not, it should be a header issue.
Image output, it is best to use the UTF-8 character set, because May encounter Chinese output.
And UTF-8, should pay attention to BOM header.

Humorous

Ob_clean (); // clear the buffer content, so that even if there is a BOM header, the image will not be affected.
Header ("Content-Type: image/png"); // declare that the post Content is png image data

So even if the program has non-syntactic errors, it is only a cross, rather than garbled code.

I tested it normally here.

Check whether the BOM header is removed.
Are the gd database and other environments complete?

The main problem is the header problem. Is there any output before the header?

Is it because of file encoding?

Whether or not the lZ Preview is garbled as follows:
This problem occurs because the file encoding is:
Then you need to modify it
The preview effect is as follows:

Last night, no matter how I cleaned up the cache, closed my browser, changed the code, and kept a bunch of garbled characters. Today, my computer started and tested the web page again, so it was inexplicably recovered and I was completely confused, you can only beat your computer and play with me !!!!

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.