Ji Ying 134 Ling Hao
1.Load GD library
The GD library is an open function library for dynamic image creation and open source code. It can be downloaded from the official website http://www.boutell.com/gd. Currently, the GD library supports multiple image formats such as GIF, PNG, JPEG, WBMP and XBM for image processing.
The GD library is installed by default in PHP 5, but to activate the GD library, you must modify the php.ini file. Delete the semicolon ";" before the ";extension=php_gd2.dll" option in the file, save the modified file and restart the Apache server to take effect.
2. Create a simple image
Use the GD2 function library to process various graphics and images. Creating a canvas is the first step to create an image using the GD2 function library. No matter what kind of image you create, you first need to create a canvas, and all other operations will be completed on this canvas. Creating a canvas in the GD2 function library can be achieved through the imagecreate() function.
Use the imagecreate() function to create a canvas with a width of 200 pixels and a height of 60 pixels, and set the canvas color RGB (225, 66, 159), and finally output an image in GIF format. The code is as follows:
$im = imagecreate(200,60); //Create a canvas
$white = imagecolorallocate($im, 225,66,159); //Set the background color of the canvas to light Green
imagegif($im);
The GD library in PHP supports Chinese, but it must be passed in UTF-8 format parameters. If you use the imageString() function to directly draw a Chinese string, garbled characters will be displayed. This is because GD2 can only accept Chinese. UTF-8 encoding format, and English fonts are used by default, so to output Chinese strings, the Chinese strings must be transcoded and the fonts used for Chinese characters must be set. Otherwise, the output will only be garbled characters.
Use the imageTTFText() function to output the text "This is a test" into the image. The code is as follows:
header("content-type:image/jpeg"); //Define the output as the image type
$im=imagecreatefromjpeg("images/photo.jpg"); //Load Enter the photo
$textcolor=imagecolorallocate($im,56,73,136);//Set the font color to blue and the value to the RGB color value
$fnt="c:/windows/fonts/simhei.ttf" ; //Define the font
$motto=iconv("gb2312","utf-8","This is a test"); //Define the output font string
imageTTFText($im,220,0,480,340,$ textcolor,$fnt,$motto); //Write TTF text into the image
imagejpeg($im); //Create a JPEG graphic
imagedestroy($im); //End the graphic and release memory space
?>
Create a checks.php file, use the GD2 function in the file to create a 4-digit verification code, and save the generated verification code to the session:
session_start();
header("content-type:image/png"); //Set the format of the created image
$image_width=70; //Set the image width
$image_height=18; //Set the image height
srand(microtime()*100000); //Set the random number seed
for($i=0;$i<4;$i ) { Write the verification code into the SESSION variable
$num_image=imagecreate($image_width,$image_height); //Create a canvas
imagecolorallocate($num_image,255,255,255); //Set the color of the canvas
for($i=0;$i
$x=mt_rand(1,8) $image_width*$i/4; //Set the X coordinate of the random character location
$y=mt_rand(1,$image_height/4) ; ; num_image, $ FONT, $ x, $ y, $ _ session [Check_checks] [$ i], $ color); // Horizontal output character
}
ImagePng ($ num_image);
imagedestroy($num_image); //Release image resources
?>
Create a user login form and call checks.php to output the content of the image in the form:
session_start();
if($_POST["Submit"]!=""){
$checks=$_POST["checks"];
if ($checks==""){
echo "<script> alert('Verification code cannot be empty');window.location.href='index.php';</script>";
}
if($checks==$_SESSION[check_checks]){
echo "