PHP Construction Verification Code

Source: Internet
Author: User

The code is as follows:

<?phpheader (' content-type:image/jpeg '); $width =120; $height =40; $element =array (' A ', ' B ', ' C ', ' d ', ' e ', ' f ', ' g ', ' H ') , ' I ', ' j ', ' K ', ' m ', ' n ', ' o ', ' P ', ' Q ', ' R ', ' s ', ' t ', ' u ', ' V ', ' w ', ' x ', ' y ', ' z '), $string = '; for ($i =0; $i <5; $i + +) {< Span style= "White-space:pre" ></span> $string. = $element [Rand (0,count ($element)-1)];} $img =imagecreatetruecolor ($width, $height);//Set Picture size $colorbg=imagecolorallocate ($img, Rand (200,255), rand (200,255) , Rand (200,255));//randomly produce background color $colorborder=imagecolorallocate ($img, Rand (200,255), Rand (200,255), Rand (200,255));// Randomly generated background color $colorstring=imagecolorallocate ($img, Rand (10,100), Rand (10,100), Rand (10,100)), Imagefill ($img, 0,0,$ COLORBG)///Set Picture background color Imagerectangle ($img, 0,0, $width-1, $height-1, $colorBorder);//Build Rectangle border for ($i =0; $i <100; $i + +) {// Draw 100 small pixels <span style= "White-space:pre" ></span>imagesetpixel ($img, rand (0, $width-1), rand (0, $height-1), Imagecolorallocate ($img, Rand (100,200), Rand (100,200), Rand (100,200));//Draw a single pixel}for ($i =0; $i <3; $i + +) {//Draw three random lines <span style= "White-space:pre" &GT;&LT;/span>imageline ($img, rand (0, $width/2), rand (0, $height), Rand ($width/2, $width), rand (0, $height), Imagecolorallocate ($img, Rand (100,200), Rand (100,200), Rand (100,200))), <span style= "White-space:pre" ></ span>//first two is the starting point coordinates, the latter two are the end coordinates, the starting point control in the left half, the end point control in the right half}//imagestring ($img, 5,0,0, ' ABCD ', $colorString); Imagettftext ($img, 14,rand ( -5,5), Rand (5,15), Rand (30,35), $colorString, ' Font/sketchycomic.ttf ', $string);//Draw text, you can choose the method of rich font// Description: Array imagettftext (Resource $image, float $size, float $angle, int $x, int $y, int $color, string $fontfile, S Tring $text) imagejpeg ($img); Imagedestroy ($img);

Key Introduction Imagettftext () method:

Array Imagettftext( Resource$image , float$size , float$angle , int$x , int$y , int$color , string$fontfile , string$text )

Image
image resources. See Imagecreatetruecolor ().
Size
font size. Depending on the GD version, you should specify (GD1) or point size (GD2) in pixel size.
Angle
Angle
System represents the angle, 0 degrees is the text read from left to right. A higher value indicates a counterclockwise rotation. For example, 90 degrees is the text that reads from the bottom up.
X
The
coordinates represented by x, y define the basic point of the first character (presumably the lower-left corner of the character). This differs from imagestring () , where x, y defines the upper-left corner of the first character. For example, "top left" is 0, 0.
Y
The
Y-coordinate. It sets the position of the font baseline, not the bottom of the character.
Color
The
color index. Using a negative color index value has the effect of turning off anti-aliasing. See Imagecolorallocate ().
Fontfile
is the path to the TrueType font you want to use. depending on the GD library used by PHP, when fontfile does not start with / begin with . TTF will be added to the file name and will attempt to search for it in the library definition font path. when using the GD library version below 2.0.18, a space character instead of a semicolon will be used as the "path delimiter" for different font files. Careless use of this attribute will result in a warning message: Warning:could not find/open font. The only solution to the affected version is to move the font to a path that does not contain spaces.

In many cases, fonts are placed in the same directory as the script. The following tips can alleviate the problems that are included.

<?php
// Set the enviroment variable for GD
putenv(‘GDFONTPATH=‘ . realpath(‘.‘));

// Name the font to be used (note the lack of the .ttf extension)
$font = ‘SomeFont‘;
?>

Text
The
text string. you can include decimal digitized character representations (in the form:& #8364;) to access characters that exceed position 127 in the font. UTF-8 encoded strings can be passed directly. If a character used in a string is not supported by the font, a hollow rectangle replaces the character.

imagettftext () returns an array of 8 cells representing the four corners of the text frame, in the order of sitting, lower right, upper right, upper-left corner. These points are relative to the text and are independent of the angle, so the upper left corner refers to the upper-left corner of the text when viewed horizontally.

Example #1 imagettftext () Example

The script in this example will generate a white 400x30 pixel PNG image with black (shaded) Arial "testing ..." written in the font.

<?php
//Set the Content-type
Header("Content-type:image/png");

//Create the image
$im= Imagecreatetruecolor( -,  -);

//Create some colors
$white= imagecolorallocate($im, 255, 255, 255);
$grey= imagecolorallocate($im,  -,  -,  -);
$black= imagecolorallocate($im, 0, 0, 0);
Imagefilledrectangle($im, 0, 0, 399,  in, $white);

//The text to draw
$text= ' testing ... ';
//Replace path by your own font path
$font= ' Arial.ttf ';

//Add Some shadow to the text
Imagettftext($im,  -, 0,  One,  +, $grey, $font, $text);

//Add the text
Imagettftext($im,  -, 0, Ten,  -, $black, $font, $text);

Using imagepng () results in clearer text compared with imagejpeg ()
imagepng($im);
Imagedestroy($im);
?>

PHP Construction Verification Code

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.