PHP to use the GD library to draw pictures, make verification code browser can not output

Source: Internet
Author: User

1, the code is as follows:

<?php

function fillrandomstring () {

Generate Verification Code

$char = Array_merge (range (0, 9), Range ("A", "Z"), Range ("A", "Z"));

Talk about string $char upset

$luan = Str_shuffle (Implode ("", $char));

Randomly take four bits from a string

Return substr ($luan, 0,4);

}

Create a canvas

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

$height = 20;

$width = 80;

$image = Imagecreate ($width, $height);

$black = imagecolorallocate ($image, 0, 0, 0);

$white = Imagecolorallocate ($image, 255, 255, 255);

Draw a picture rectangle

Imagefilledrectangle ($image, 1, 1, $width-2, $height-2, $white);

padding string

Imagestring, $image, 4, fillrandomstring (), $black);

Output picture

Imagepng ($image);

Destroying pictures

Imagedestroy ($image);


However, the output on the browser is not displayed because the cache was not cleaned before output to the browser

Add code Ob_clean () before the header ();

This article is from the "Chensir" blog, make sure to keep this source http://sourliki.blog.51cto.com/4838492/1557635

PHP to use the GD library to draw pictures, make verification code browser can not output

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.