Game php gd database Wang zebin

Source: Internet
Author: User
The GD library in Php mainly provides a set of Php graphics processing functions, which are very powerful and can make amazing results. In Web applications, it is more or less common. Therefore, when you apply for a php engineer for the WEB Website, you are often asked about the GD library. In fact, the GD library functions in php are relatively simple, and you are generally aware of their functions

The GD library in Php mainly provides a set of Php graphics processing functions, which are very powerful and can make amazing results. In Web applications, it is more or less common. Therefore, when you apply for a php engineer for the WEB Website, you are often asked about the GD library. In fact, the GD library functions in php are relatively simple, and you are generally aware of their functions

The GD library in Php mainly provides a set of Php graphics processing functions, which are very powerful and can make amazing results. In Web applications, it is more or less common. Therefore, when you apply for a php engineer for the WEB Website, you are often asked about the GD library. In fact, the GD library functions in php are relatively simple, and you can simply understand their functions. I have provided several small examples to help you remember and understand them.

The source code of the following example can be downloaded at http://wangzebin.blog.51cto.com/attachment/200902/653300_1235091601.zip.

1. Chinese Character Verification Code

Generally, verification codes for websites use combinations of numbers or characters. However, more strict verification codes may be required for some valuable WebSite Services. For example, the Chinese character authentication is used to different degrees for malicious registration verification in the 163 mailbox and QQ number application verification. Using the GD library of php, you can easily handle several lines of code. If you output Chinese or special characters, you need to provide the TTF font.

The effect is as follows:

The Code is as follows:

// Define output as image type

Header ("content-type: image/gif ");

// Create an image

$ Picture = imagecreate (150,40 );

// Define the black and white colors

$ Cl_black = imagecolorallocate ($ picture, 0, 0 );

$ Cl_white = imagecolorallocate ($ picture, 255,255,255 );

// Specify the font library

$ Lib_font = "c: // WINDOWS // fonts // simhei. ttf ";

// Define the output font string and UTF-8 encoding

$ Str_output = chr (0xE4). chr (0xB8). chr (0xAD );

// Write TTF text to the graph

Imagettftext ($ picture, 20,-10, 40, 25, $ cl_white, $ lib_font, $ str_output );

// Define the output font string and UTF-8 encoding

$ Str_output = chr (0xE5). chr (0x9B). chr (0xBD );

// Write TTF text to the graph

Imagettftext ($ picture, 20, 10, 80, 30, $ cl_white, $ lib_font, $ str_output );

// Create a GIF Image

Imagegif ($ picture );

// End the image and release the memory space

Imagedestroy ($ picture );

?>

2. Image thumbnails

The Conversion Function of image size is very common and can be used by many websites. For example, for a common SNS website, users can upload images and set their own portraits. Because the images provided by users are of different sizes, the website needs to process images in a uniform size when users upload images. Using the GD library of php, several lines of code can be easily implemented. The effect is as follows:

The Code is as follows:

// $ SrcFile original file, $ dstW, $ dsomething is the width and height of the thumbnail.

Function makethumb ($ srcFile, $ dstW, $ dsomething ){

$ Data_pic = GetImageSize ($ srcFile, & $ info );

Switch ($ data_pic [2]) {

Case 1: // image type. 1 is a GIF image.

$ Old_pic = @ ImageCreateFromGIF ($ srcFile );

Break;

Case 2: // image type; 2: JPG

$ Old_pic = @ imagecreatefromjpeg ($ srcFile );

Break;

Case 3: // The image type. 3 is a PNG image.

$ Old_pic = @ ImageCreateFromPNG ($ srcFile );

Break;

}

// Create a reduced image

$ SrcW = ImageSX ($ old_pic );

$ SrcH = ImageSY ($ old_pic );

$ New_pic = ImageCreate ($ dstW, $ dsomething );

ImageCopyResized ($ new_pic, $ old_pic, 0, 0, 0, $ dstW, $ dsomething, $ srcW, $ srcH );

// Output lock small image

Header ("content-type: image/gif ");

ImageJpeg ($ new_pic );

Imagedestroy ($ new_pic );

Imagedestroy ($ new_old );

}

Makethumb ("1.jpg", 40, 50 );

?>

3. Ascii Image

It is cool to have some character pictures attached to text BBS. Here we provide an example generated by using the GD library of php. If you are interested, you can study it. The effect is as follows:

Because the code is long, it is not pasted out. You can download it directly at the link of this article.

In addition, it is easy to watermark an image and embed it into another image. You can try 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.