PHP image processing technology-GD,

Source: Internet
Author: User
Tags color representation

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 );

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.