Simple graph processing in PHP and simple graph processing in PHP _ PHP Tutorial

Source: Internet
Author: User
Simple graph processing in PHP and simple graph processing in PHP. Simple graph processing in PHP, simple graph processing in PHP, simple graph processing should be 134. loading the GD Library is an open function library for dynamically creating images and opening source code. simple graph processing in PHP and simple graph processing in PHP.
Simple graph processing in PHP

134 Linghao

1.Load 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:

$ Im = imagecreate (); // Create a canvas
$ White = imagecolorallocate ($ im, 159,); // 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:

Header ("content-type: image/jpeg"); // defines the output as an 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.
$ Fnt = "c:/windows/fonts/simhei. ttf"; // define the font
$ MOV = iconv ("gb2312", "UTF-8", "This is a test"); // defines the output font string
ImageTTFText ($ im, 480,340, $ textcolor, $ fnt, $ motto); // write TTF text to the graph
Imagejpeg ($ im); // Create a JPEG image
Imagedestroy ($ im); // ends the image and releases the memory space.
?>

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:

Session_start ();
Header ("content-type: image/png"); // you can specify the format of the created image.
$ Image_width = 70; // set the image width
$ Image_height = 18; // set the image height
Srand (microtime () * 100000); // sets the seed of the random number.
For ($ I = 0; $ I <4; $ I ++) {// cyclically outputs a four-digit random number.
$ New_number. = dechex (rand (0, 15 ));
}
$ _ 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); // you can specify the canvas color.
For ($ I = 0; $ I $ Font = mt_rand (); // set a random font.
$ X = mt_rand () + $ image_width * $ I/4; // Set the X coordinate of the random character location
$ Y = mt_rand (1, $ image_height/4); // you can specify the Y coordinate of the random character location.
$ Color = imagecolorallocate ($ num_image, mt_rand (0,100), mt_rand (0,150), mt_rand (0,200); // set the character color
Imagestring ($ num_image, $ font, $ x, $ y, $ _ SESSION [check_checks] [$ I], $ color); // horizontal output character
}
Imagepng ($ num_image); // generate an image in PNG format
Imagedestroy ($ num_image); // release image resources
?>

Create a user login form and call checks. php to output the image content in the form:

Session_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







Simple graph processing in PHP should be 134 Ling Hao 1. loading GD Library is an open function library that dynamically creates images and exposes source 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.