PHP first knowledge of GD library

Source: Internet
Author: User
Tags image identifier

d===== ( ̄▽ ̄*) b

Speech

PHP is not limited to HTML output, but can also create and manipulate a wide variety of image files, such as GIF, PNG, JPEG, WBMP, XBM, etc.

PHP can also display the image stream directly in the browser.

To process the image, you need to use the PHP GD library.

PS: Make sure the GD library can be loaded in the php.ini file. You can find "; Extension=php_gd2.dll" In the php.ini file, delete the semicolon before the option, save it, and then restart the Apache server.

Steps

Creating an image in PHP typically requires four steps:

1. Create a background image, and all future operations are based on this background.

2. Draw on the image, and so on.

3. Output the final image.

4. Destroy the image resources in memory.

1. Create a background image

The following function can return an image identifier, which represents a background with a width of x_size pixels, a height of y_size pixels, and black by default.

1 resource imagecreatetruecolor (int x_size, int y_size)

Drawing on an image takes two steps: You first need to select a color. Create a Color object by using the Imagecolorallocate () function.

1 int imagecolorallocate (resource image, int red, int green, int blue)

The color is then drawn onto the image.

1 bool Imagefill (resource image, int x, int y, int color)

The Imagefill () function fills with a color color at the coordinates (x, y) of the image images.

2. Drawing on the image

1 bool Iamgeline (resource image, int begin_x, int begin_y, int end_x, int end_y, int color)

The Imageline () function uses a color color to draw a segment from (begin_x,begin_y) to (end_x,end_y) in image images.

1 bool Imagestring (resource image, int font, int begin_x, int begin_y, string s, int color )

The imagestring () function uses a color color to draw the string s to the image (Begin_x,begin_y) (which is the upper-left coordinate of the string). If the font equals 1,2,3,4 or 5, the built-in font is used, and the number represents the weight of the font.

If the font fonts are not built-in, you need to import the font library after you use them.

3. Output final image

After creating the image you can output graphics or save to a file, if you need to output to the browser you need to use the header () function to send a graphical header "spoofing" the browser, so that it thinks that the PHP page running is an image.

1 header("Content-type:image/png");

After sending the data header, use the Imagepng () function to output the graph. The following filename is optional, which represents the saved name of the generated image file.

1 bool Image (resource image [, string filename])

4. Destroying the associated memory resources

Finally, you need to destroy the memory resources occupied by the image.

1 bool Imagedestroy (resource image)

Example:

1<?PHP2 $width= 300;//Image Width3 $height= 200;//Image Height4 $img=imagecreatetruecolor ($width,$height);//Create an image5 $white=imagecolorallocate ($img, 255,255,255);//White6 $black=imagecolorallocate ($img, 0,0,0);//Black7 $red=imagecolorallocate ($img, 255,0,0);//Red8 $green=imagecolorallocate ($img, 0,255,0);//Green9 $blue=imagecolorallocate ($img, 0,0,255);//BlueTenImagefill ($img, 0, 0,$white);//set the background to white OneImageline ($img, 20, 20,260,150,$red);//draw a red line. AImagestring ($img, 5,50,50, "hello,world!!",$blue);//Display the blue text - Header("Content-type:image/png");//MIME type of output image -Imagepng ($img);//output a PNG image data theImagedestroy ($img);//Empty Memory

Effect:

PHP first knowledge of the GD library

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.