Let's look at the general effect.
So let's just post the code right next.
<?php $image = imagecreatetruecolor (100, 30); Create Canvas $imagecolor = Imagecolorallocate ($image, 255, 255, 255); Background color Imagefill ($image, 0, 0, $imagecolor);
Fill background color for ($i =0; $i <4; $i + +) {//loop 4 digits $fontsize = 6;
$fontcolor = Imagecolorallocate ($image, rand (0), rand (0), rand (0, 200));
$fontcontent = rand (0, 9);
$x = $i *100/4 + rand (5, 15);
$y = rand (5, 10);
Imagestring ($image, $fontsize, $x, $y, $fontcontent, $fontcolor); For ($i =0 $i <200; $i + +) {//Loop add interference point $pointcolor = Imagecolorallocate ($image, Rand, Rand), Rand (
50, 200));
$x = rand (1, 99);
$y = rand (1, 29);
Imagesetpixel ($image, $x, $y, $pointcolor); For ($i =0 $i <3; $i + +) {//Loop add interference line $linecolor = Imagecolorallocate ($image, Rand (M), rand (1
00, 250));
$x 1 = rand (1, 25);
$x 2 = rand (50, 75);
$y 1 = rand (1, 15);
$y 2 = rand (15, 25);
Imageline ($image, $x 1, $y 1, $x 2, $y 2, $linecolor);
Header ("Content-type:image/png"); Imagepng ($iMage); Imagedestroy ($image);?>
To share one more to generate the Chinese authentication Code
<?php//1.qi enables the GD Library GD Library to provide a series of APIs for working with pictures, using the GD library to work with pictures, or to generate pictures.
The GD library on the site is typically used to generate thumbnails or to add watermarks to images or to generate reports on Web site data.
Session_Start (); To convert GBK encoded strings into UTF-8 strings, the first argument is written GBK because this php file is stored in the host code is GBK coding//UTF-8 Coding Browser Universal support, versatility, here is converted to UTF-8 $str = Iconv ("GBK"
, "Utf-8", "all mortal beings green mountains and monuments open Heart will be clouds unroll happiness will always accompany you");
if (!is_string ($str) | | |!mb_check_encoding ($STR, "Utf-8")) {exit ("not a string or not Utf-8"); $zhongwenku _size;
Gets the length of the string by UTF-8 encoding $zhongwenku _size = Mb_strlen ($str, "UTF-8");
Import the above characters into the array $zhongwenku = Array ();
For ($i =0 $i < $zhongwenku _size $i + +) {$zhongwenku [$i] = Mb_substr ($str, $i, 1, "UTF-8");}
$result = ""; Four characters to write on the picture for ($i =0 $i <4; $i + +) {switch (rand (0, 1)) {case 0: $result. = $zhongwenku [rand (0, $zhongwenku _si
ZE-1)];
Break
Case 1: $result. =dechex (rand (0,15));
Break
} $_session["Check" = $result;
Create a true Color picture width 100, high $img = Imagecreatetruecolor (100, 30);
Assign background color $BG = imagecolorallocate ($img, 0, 0, 0); Assign text color $te = imagecolorallocate ($img, 255,255,255);
Write a string on the picture//imagestring ($img, Rand (3,8), Rand (1,70), Rand (1,10), $result, $te); In the picture, you can write special fonts based on the load font imagettftext ($img, Rand (2, 9), $te, "Msyh."
TTF ", $result);
$_session["Check" = $result;
For ($i =0 $i <3; $i + +) {//$t = Imagecolorallocate ($img, rand (0, 255), rand (0, 255), rand (0, 255));
Draw Line Imageline ($img, 0, rand (0), rand (70,100), rand (0,), $te);
$t = Imagecolorallocate ($img, rand (0, 255), rand (0, 255), rand (0, 255)); Add noise for the picture for ($i =0 $i <200; $i + +) {Imagesetpixel ($img, rand (1), rand (1,), $t);//Send HTTP header information Specifies that this time send is in image
JPEG header ("Content-type:image/jpeg");
Output JPEG image to browser imagejpeg ($IMG); ?>
Let's take another example.
<?php session_start ();
function Random ($len) {$srcstr = "1a2s3d4f5g6hj8k9qwertyupzxcvbnm";
Mt_srand ();
$strs = "";
for ($i = 0; $i < $len; $i + +) {$strs. = $srcstr [Mt_rand (0, 30)];
return $strs;
///randomly generated string $str = random (4);
The width of the verification code picture $width = 50;
The height of the verification code picture $height = 25;
Declare the picture format of the layer you want to create @ header ("Content-type:image/png");
Create a layer $im = Imagecreate ($width, $height);
Background color $back = Imagecolorallocate ($im, 0xFF, 0xFF, 0xFF);
Fuzzy point Color $pix = Imagecolorallocate ($im, 187, 230, 247);
Font Color $font = imagecolorallocate ($im, 41, 163, 238);
A point Mt_srand () that paints a blur action;
for ($i = 0; $i < 1000 $i + +) {Imagesetpixel ($im, Mt_rand (0, $width), Mt_rand (0, $height), $pix);
Output characters imagestring ($im, 5, 7, 5, $STR, $font);
Output Rectangle Imagerectangle ($im, 0, 0, $width-1, $height-1, $font);
Output picture imagepng ($im);
Imagedestroy ($im);
$STR = MD5 ($STR);
Select Cookie//setcookie ("Verification", $str, Time () + 7200, "/"); Select Session $_session["Verification "] = $str;?>
Next, just call on the page:
If you want to realize "can't see clearly?" To change the effect, add the following JS to the page
function changing () {
document.getElementById (' Checkpic '). src= "/images/checkcode.php?" +math.random ();
The above mentioned is the entire content of this article, I hope you can enjoy.