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.