PHP Basic Practice--Simple Verification Code implementation _ Practical skills

Source: Internet
Author: User
Tags rand

Copy Code code as follows:

<?php
/**
* Vcode
* param $w Canvas width
* param $h Canvas height
* param $fnum The number of verification code elements
* param $fontsize Verification code element size
* param $obs Interferon number
*/

function Vcode ($w, $h, $fnum, $fontsize, $obs) {
$img = Imagecreatetruecolor ($w, $h);

$white = Imagecolorallocate ($img, 255, 255, 255);
$black = imagecolorallocate ($img, 0, 0, 0);
$gray = Imagecolorallocate ($img, 180, 180, 180);

Imagefill ($img, 0, 0, $gray);

for ($i =0; $i < $obs; $i + +) {
Imagesetpixel ($img, rand (0, $w), rand (0, $h), $black);
}

$x = ($w-$fnum * $fontsize)/2;
$y = $h/2 + $fontsize/2;
$fontfile = "Final/fonts/msyh.ttf";
$arr = Array_merge (range (0,9), Range (' A ', ' Z '), Range (' A ', ' Z '));
Shuffle ($arr);
$str =implode (array_slice ($arr, 0, $fnum));
Imagettftext ($img, $fontsize, 0, $x, $y, $black, $fontfile, $STR);

Header ("Content-type:image/jpeg");
Imagejpeg ($IMG);

Imagedestroy ($IMG);
}

Vcode (80, 40, 4, 15, 1000);


Run Effect chart:

Through this basic exercise, learn the simple way to deal with the problem, meet the requirements of the target, do not blindly knock code.

For example, the verification code, the core of which is the Imagettftext () function, around this function to expand the design of the verification code.

Note: Use of the range () function, if the parameter is a character, use either single or double quotes, otherwise the header header is considered to have 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.