Routine arrangement of simple graph processing (Classic) _ PHP instances in php

Source: Internet
Author: User
This article is a small series of daily php graphics processing, how to use the GD2 function to add text on the photo, use the GD2 function to create a four-digit verification code. Anyone interested in this article can learn more 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.

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.