PHP image processing technology-GD,
1. Enable GD Extension
Extension = php_gd2.dll
Apache restart
2. Example:
Scenario:
Create a green image of 500x300
① Create a canvas
Canvas, a type of resource-based data. Operational image resources.
Create a canvas (new)
ImageCreate (width and height) to create a canvas based on the color palette.
ImageCreateTrueColor (width, height); create a true color canvas.
Create a canvas (open) based on the Image)
ImageCreateFromJPEG (image address );
ImageCreateFromPNG (image address );
ImageCreateFromGIF (image address );
② Operating canvas
Assign color: If you need to use a color on the canvas, you should first assign the color to the canvas.
Function:
Color id = imageColorAllocate (canvas, R, G, B );
Color Representation:
RGB
③ Fill the canvas: Fill (replace) The filled points that are continuous and of the same color)
Function:
ImageFill (canvas, fill position x, fill position Y, color logo) Complete
Coordinates are used for Location Management:
Origin: 0, 0, the upper left corner of the canvas.
To the right, the X axis increases, and the downward Y axis increases.
Coordinates in the lower right corner: width-1, height-1
④ Output canvas
Output the processed image information in the canvas.
Typical:
1. output to the image file.
2. Direct output.
Function:
ImagePNG (canvas [, file address]):
ImageJPEG ();
ImageGIF ();
If there is no second parameter, it indicates direct output.
NOTE: If it is directly output to the browser, You need to inform the browser that the response data type should be in PNG format:
Use the command Content-type;
A canvas can be output multiple times and in various formats
⑤ Destroy canvas Resources
ImageDestroy ()
Sample Code:
<? Php
// For example, create a green image of 500x300
$ Width = 500;
$ Height = 300;
// Create a true-color canvas
$ Img = imageCreateTrueColor ($ width, $ height );
// Var_dump ($ img); // Test Result: resource (2) of type (gd) $ img is a resource type.
// Use the imageColorAllocate function (canvas, R, G, B) to assign a color. The color format is r g B.
$ Green = imageColorAllocate ($ img, 0x0, 0xff, 0x0 );
// Fill the canvas, with the origin: 0, 0, the upper left corner of the canvas, and imageFill (canvas, fill position x, fill position Y, color ID ).
ImageFill ($ img, 0, 0, $ green );
// Output canvas, which is directly output to the current path as an image file. The formats include PNG, JPEG, and GIF.
// ImagePNG ($ img, './green.png'); // if there is no second parameter, it indicates direct output.
// If it is directly output to the browser, You need to inform the browser that the response data type should be in PNG format: Use the command Content-type
Header ('content-Type: image/png ;');
ImagePNG ($ img );
// Note: a canvas can be output multiple times, and the output is in various formats.
// Destroy canvas Resources
ImageDestroy ($ img );
Sample Code:
<? Php
// For example, create a green image of 500x300
$ Width = 500;
$ Height = 300;
// Create a true-color canvas
$ Img = imageCreateTrueColor ($ width, $ height );
// Var_dump ($ img); // Test Result: resource (2) of type (gd) $ img is a resource type.
// Use the imageColorAllocate function (canvas, R, G, B) to assign a color. The color format is r g B.
$ Green = imageColorAllocate ($ img, 0x0, 0xff, 0x0 );
// Fill the canvas, with the origin: 0, 0, the upper left corner of the canvas, and imageFill (canvas, fill position x, fill position Y, color ID ).
ImageFill ($ img, 0, 0, $ green );
// Output canvas, which is directly output to the current path as an image file. The formats include PNG, JPEG, and GIF.
// ImagePNG ($ img, './green.png'); // if there is no second parameter, it indicates direct output.
// If it is directly output to the browser, You need to inform the browser that the response data type should be in PNG format: Use the command Content-type
Header ('content-Type: image/png ;');
ImagePNG ($ img );
// Note: a canvas can be output multiple times, and the output is in various formats.
// Destroy canvas Resources
ImageDestroy ($ img );