Fun Image function library-common graphic operations (PHP)

Source: Internet
Author: User
Tags 0xc0 image identifier
I try not to talk about big theory, for example, what is png. PHP has bound its own GD2 Library since version 4.3. you can download and set it yourself. to check whether your php version supports the gd module (JPEG, PNG, WBMP, But GIF is no longer supported), use the following method: if (! Function_exists (imagecreate) {die (this server does not support

I try not to talk about big theory, for example, what is png.

PHP has bound its own GD2 Library since version 4.3. you can download and set it yourself. to check whether your php version supports the gd module (supports JPEG, PNG, WBMP, but does not support GIF), use the following method:

If (! Function_exists ('imagecreate ')){
Die ('the current server does not support GD module ');
}

If not, how can I configure it? Download the dll file of the gd module, modify php. ini, and restart the server.

The PHP plot is PS.

When you plan to PS, you should complete the following steps, which is necessary.

1: Create a basic PS object (I suppose it is $ image) and fill in the background (Black by default). all subsequent ps operations will be based on this background image.
2: Plot on $ image.
3: output this image.
4. destroy objects and clear memory usage.

First, let's get to know several common functions. these functions are described in detail in the php Manual.

Resource imagecreate (int x_size, int y_size)
Imagecreate () returns an image identifier, representing a blank image of x_size and y_size.
This function is basically the same as imagetruecolor ($ width, $ height ).

Int imagecolorallocate (resource image, int red, int green, int blue)
Imagecolorallocate () returns an identifier representing a color consisting of a given RGB component. The image parameter is the return value of the imagecreatetruecolor () function. Red, green, and blue are the required colors respectively. These parameters are integers from 0 to 255 or hexadecimal values 0x00 to 0xFF. Imagecolorallocate () must be called to create the color of each image used in the image.

Bool imagefill (resource image, int x, int y, int color)
Imagefill () is filled with the color execution area (that is, with x, y points are in the same color and adjacent points are filled ).


Bool imageline (resource image, int x1, int y1, int x2, int y2, int color)
Imageline () uses color to draw a line segment from the coordinates x1, y1 to x2, y2 (0 in the upper left corner of the image.

Bool imagestring (resource image, int font, int x, int y, string s, int col)
Imagestring () uses the col color to draw string s to the x and y coordinates of the image (this is the coordinate of the upper left corner of the string, and the upper left corner of the entire image is 0, 0 ). If the font is 1, 2, 3, 4, or 5, the built-in font is used.

Array imagettftext (resource image, float size, float angle, int x, int y, int color, string fontfile, string text)
This function is important and has many parameters. it is not listed here. it is mainly written to an image. it is similar to the above function, but must be powerful.

Bool imagefilltoborder (resource image, int x, int y, int border, int color)
Imagefilltoborder () starts from x, y (0, 0 in the upper left corner of the image) and fills the area with color until the border of the color is border is reached. [Note: all colors in the boundary are filled. If the specified boundary color is the same as that of the vertex, it is not filled. If this boundary color does not exist in the image, the entire image will be filled .]

Bool imagefilledellipse (resource image, int cx, int cy, int w, int h, int color)
Imagefilledellipse () draws an elliptical image centered on cx and cy (0, 0 in the upper left corner of the image. W and h specify the width and height of the elliptic respectively. Fill the ellipse with color. If the call succeeds, TRUE is returned. if the call fails, FALSE is returned.

Output image data: imagepng ($ image [, $ filename])

Example 1: output blue background and cross white line graphics

 

 

Output Image:

Online demonstration:Http://www.phzzy.org/temp/5do8/ex1.php

Example 2: yin/yang diagram

$ Width = 400;
$ Height = 400;
$ Image = imagecreatetruecolor ($ width, $ height );
// Extract color
$ Color_black = imagecolorallocate ($ image, 0, 2, 0 );//
$ Color_white = imagecolorallocate ($ image, 255,255,255); // White
$ Color_blue = imagecolorallocate ($ image, 108,); // Blue
$ Color_red = imagecolorallocate ($ image, 151,0, 4); // Red
$ Color_my = imagecolorallocate ($ image, 192,192,255); // background
$ Color_temp = imagecolorallocate ($ image, 199,199,199); // background
// Plot
Imagefill ($ image, 0, 0, $ color_white );

// The first one is a big circle.
Imagefilledarc ($ image, $ width/2, $ height/2, $ height, $ height, 0,360, $ color_blue, IMG_ARC_PIE );
// Two Circles
Imagefilledellipse ($ image, $ width/2, $ height/4, $ height/2, $ height/2, $ color_red );
Imagefilledellipse ($ image, $ width/2, $ height/4*3, $ height/2, $ height/2, $ color_blue );
/* Imagefilledellipse -- draw an ellipse and fill it */
Imagefilledarc ($ image, $ width/2, $ height/2, $ height, $ height,-90, $ color_red, IMG_ARC_PIE );
Imagefilledellipse ($ image, $ width/2, $ height/4*3, $ height/2, $ height/2, $ color_blue );
// Send the object to the header
Header ('content-type: image/png ');
Imagepng ($ image );
/*
// Send an object to a file
$ Filename = "ex1.png ";
Imagepng ($ image, $ filename );
*/
// Destroy the object
Imagedestroy ($ image );
?>

 

Demo:Http://www.phzzy.org/temp/5do8/ex2.php

Example 3: 3D images-cool

 

Demo:Http://www.phzzy.org/temp/5do8/ex3.php

$ Width = 400;
$ Height = 400;
$ Image = imagecreatetruecolor ($ width, $ height );
$ White = imagecolorallocate ($ image, 0xFF, 0xFF, 0xFF );
$ Gray = imagecolorallocate ($ image, 0xC0, 0xC0, 0xC0 );
$ Darkgray = imagecolorallocate ($ image, 0x90, 0x90, 0x90 );
$ Navy = imagecolorallocate ($ image, 0x00, 0x00, 0x80 );
$ Darknavy = imagecolorallocate ($ image, 0x00, 0x00, 0x50 );
$ Red = imagecolorallocate ($ image, 0xFF, 0x00, 0x00 );
$ Darkred = imagecolorallocate ($ image, 0x90, 0x00, 0x00 );
Imagefill ($ image, 0, 0, $ white );

// Make the 3D effect
For ($ I = $ height/2 20; $ I >$ height/2; $ I --){
Imagefilledarc ($ image, $ width/2, $ I, $ width/2, $ height/2, 0, 45, $ darknavy, IMG_ARC_PIE );
Imagefilledarc ($ image, $ width/2, $ I, $ width/2, $ height/2, 45, 75, $ darkgray, IMG_ARC_PIE );
Imagefilledarc ($ image, $ width/2, $ I, $ width/2, $ height/2, 75,360, $ darkred, IMG_ARC_PIE );
}

Imagefilledarc ($ image, $ width/2, $ height/2, $ width/2, $ height/2, 0, 45, $ navy, IMG_ARC_PIE );
Imagefilledarc ($ image, $ width/2, $ height/2, $ width/2, $ height/2, 45, 75, $ gray, IMG_ARC_PIE );
Imagefilledarc ($ image, $ width/2, $ height/2, $ width/2, $ height/2, 75,360, $ red, IMG_ARC_PIE );


// Flush image
Header ('content-type: image/png ');
Imagepng ($ image );
Imagedestroy ($ image );
/*
// Send an object to a file
$ Filename = "ex1.png ";
Imagepng ($ image, $ filename );
*/

?>

$ Width = 35;
$ Height = 35;
// Create an object
$ Image = imagecreate ($ width, $ height );
// Extract color
$ Color_white = imagecolorallocate ($ image, 255,255,255); // White
$ Color_blue = imagecolorallocate ($ image, 108,); // Blue
Imagefill ($ image, 0, 0, $ color_blue );
// Plot
// Line width
Imagesetthickness ($ image, 3 );
Imageline ($ image, 0, 0, $ width, $ height, $ color_white );
Imageline ($ image, $ width, 0, 0, $ height, $ color_white );

// Send the object to the header
Header ('content-type: image/png ');
Imagepng ($ image );
/*
// Send an object to a file
$ Filename = "ex1.png ";
Imagepng ($ image, $ filename );
*/
// Destroy the object
Imagedestroy ($ image );
?>

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.