(Advanced article) PHP Image processing Technology-GD Library

Source: Internet
Author: User
Tags color representation
1. Open GD Extension

Extension=php_gd2.dll

Apache restart

2. Example:

Scene:

Make a green picture of 500x300

① Creating a canvas

Canvas, a resource-based data. An image resource that can be manipulated.

Create a new canvas (new)

Imagecreate (wide, high) creates a palette-based canvas.

Imagecreatetruecolor (wide, high); Create a true color canvas.

Create a canvas based on a picture (open)

Imagecreatefromjpeg (image address);

Imagecreatefrompng (image address);

Imagecreatefromgif (image address);

② manipulating the canvas

Assign color: If you need to use a color on the canvas, you should first assign the color to the canvas.

Using functions:

Color identification = imagecolorallocate (canvas, r,g,b);

How colors are represented:

Rgb

③ Fill canvas: Fills (replaces) fill points, contiguous and identical points of color

Using functions:

Imagefill (canvas, fill position x, fill position y, color identification) complete

Location is managed using coordinates:

Origin: 0, 0, the upper-left corner of the canvas.

To the right, the X axis increases and the downward Y axis increases.

Lower right corner coordinates: width-1,height-1

④ Output Canvas

Output the drawing information that is processed in the canvas.


Typical of:

1 output to picture file.

2 Direct output.

Using functions:

Imagepng (canvas [, File address]):

Imagejpeg ();

Imagegif ();

If there is no second argument, the direct output is indicated.

Note: If you print directly to the browser, you need to tell the browser that the type of response data should be a picture in PNG format:

Use instruction Content-type;

One canvas can be output multiple times, output to various formats

⑤ Destroying Canvas Resources

Imagedestroy ()


Example code:

<?php//For example: Make 500x300 green picture $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 function imagecolorallocate (canvas, r,g,b) to assign color, color representation: R  G  b$green =imagecolorallocate ($img, 0x0, 0xFF, 0x0);//Fill Canvas, Origin: 0, 0, upper left corner of canvas, Imagefill (canvas, fill position x, fill position y, color identification) complete Imagefill ($img, 0,0 , $green);//output canvas, direct output to the current path as a picture file, in the format of PNG, JPEG, Gif//imagepng ($img, './green.png ');  If there is no second parameter, the direct output//If the direct output to the browser, you need to tell the browser, the type of response data should be a picture in PNG format: Use instruction Content-typeheader (' content-type:image/png; '); Imagepng ($img);//NOTE: A canvas can be output multiple times, output for various formats//Destroy Canvas resources Imagedestroy ($IMG);


Example code:

<?php//For example: Make 500x300 green picture $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 function imagecolorallocate (canvas, r,g,b) to assign color, color representation: R  G  b$green =imagecolorallocate ($img, 0x0, 0xFF, 0x0);//Fill Canvas, Origin: 0, 0, upper left corner of canvas, Imagefill (canvas, fill position x, fill position y, color identification) complete Imagefill ($img, 0,0 , $green);//output canvas, direct output to the current path as a picture file, in the format of PNG, JPEG, Gif//imagepng ($img, './green.png ');  If there is no second parameter, the direct output//If the direct output to the browser, you need to tell the browser, the type of response data should be a picture in PNG format: Use instruction Content-typeheader (' content-type:image/png; '); Imagepng ($img);//NOTE: A canvas can be output multiple times, output for various formats//Destroy Canvas resources Imagedestroy ($IMG);

Above is (Advanced article) PHP Image processing Technology-GD library content, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!

  • 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.