Routine PHP graphical processing (classic) and php graphic processing. Routine PHP graphical processing (classic), php graphic processing 1. the GD Library is an open function library for dynamically creating images and opening source code. it can be used to process simple PHP graphics (classic) and php graphics on the official website http:
1. load the GD Library
The GD Library is an open function library for dynamically creating images and opening source code. it can be downloaded from the official website http://www.boutell.com/gd. Currently, the GD library supports GIF, PNG, JPEG, WBMP, XBM, and other image formats for image processing.
The GD Library is installed by default in PHP 5. 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.
2. create a simple image
You can use the GD2 function library to process various images. Creating a canvas is the first step to use the GD2 function library to create an image. no matter what image you create, you must first create a canvas. other operations will be completed on the canvas. Create a canvas in the GD2 function library and use the imagecreate () function.
Use the imagecreate () function to create a canvas with a width of 200 pixels and a height of 60 pixels, set the canvas color RGB (159, 66,), and output an image in GIF format, the code is as follows:
<? Php $ im = imagecreate (159, 60); // Create a canvas $ white = imagecolorallocate ($ im ); // set the background color of the canvas to light green imagegif ($ im); // output image?>
3. use the GD2 function to add text to the photo
The GD Library in PHP supports Chinese, but must be passed in UTF-8 format parameters, if you use the imageString () function to draw a Chinese string directly will show garbled, this is because GD2 can only receive the UTF-8 encoding format for Chinese characters, and the English font is used by default, so to output the Chinese character string, you must transcode the Chinese character string, and set the font used by the Chinese character. Otherwise, garbled characters are output.
Use the imageTTFText () function to output the text "This is a test" to the image. the code is as follows:
<? Phpheader ("content-type: image/jpeg"); // define the output as the image type $ im = imagecreatefromjpeg ("images/photo.jpg "); // load the Photo $ textcolor = imagecolorallocate ($ im, 56, 73, 136); // set the font color to blue and the value to RGB color $ fnt = "c: /windows/fonts/simhei. ttf "; // define the font $ MOV = iconv (" gb2312 "," UTF-8 "," This is a test "); // define the output font string imageTTFText ($ im, 0, 480,340, $ textcolor, $ fnt, $ motto); // write TTF text to imagejpeg ($ im) in the image; // Create a JPEG image imagedestroy ($ im ); // stop the image and release the memory?>
4. PHP generate verification code
Create a checks. php file and use the GD2 function in the file to create a four-digit verification code. then, save the generated verification code to the session:
<? Phpsession_start (); header ("content-type: image/png"); // set the image creation format $ image_width = 70; // set the image width $ image_height = 18; // set the image height srand (microtime () * 100000); // Set the seed of the random number for ($ I = 0; $ I <4; $ I ++) {// cyclically output a 4-digit random number $ new_number. = dechex (rand ();} $ _ SESSION [check_checks] = $ new_number; // write the obtained random number verification code to the SESSION variable $ num_image = imagecreate ($ image_width, $ image_height); // Create a canvas imagecolorallocate ($ num_image, 255,255,255); // Set the canvas color for ($ I = 0; $ I
Create a user login form and call checks. php to output the image content in the form:
<? Phpsession_start (); if ($ _ POST ["Submit"]! = "") {$ Checks = $ _ POST ["checks"]; if ($ checks = "") {echo "script" alert ('verification code cannot be blank '); window. location. href = 'index. php'; script ";} if ($ checks = $ _ SESSION [check_checks]) {echo" script alert ('User logon successful! '); Window. location. href = 'index. php'; script ";} else {echo" script "alert ('The verification code you entered is incorrect! '); Window. location. href = 'index. php'; script ";}}?>
Application of rand functions
The above content is a small series to share with you about simple graph processing in php. I hope you will like it.
Upload (classic), php graphics processing 1. loading the GD library of the GD Library is an open function library that dynamically creates images and exposes source code. you can go to the official website http :...