Create images and colors and fill in the background (PHP graphic image of the typical application tutorial 1)

Source: Internet
Author: User
Tags image identifier
create images and colors and fill in the background (PHP graphic image of the typical application tutorial 1)

If the page does not have a colorful graphic image is always lack of anger, beautiful graphic images can make the whole page look more attractive, so that many words are difficult to express ideas at a glance, and can clearly express the relationship between the data. We will explain the various techniques of graphics and image processing in PHP separately.

In the previous article, what is the GD library? Loading GD library in PHP specific introduction to the introduction of the GD library installation and activation, then we today this article to introduce you to create images, create colors, fill the background!

1. Create an image

With the GD2 library you can work with various graphic images, creating a canvas is the first step in creating an image using the GD2 library, no matter what image you create, you first need to create a canvas, and everything else is done on this canvas. Let's start by creating a picture of a specified size with a black background and we'll use the Imagecreatetruecolor () function, which has the following syntax:

Resource Imagecreatetruecolor (int $width, int $height)

Imagecreatetruecolor () returns an image identifier representing a black image with a size of x_size and y_size.

Here we only use an example to introduce the function, the code is as follows:

<?phpheader ("content-type:text/html; Charset=utf-8 "); Header (" Content-type:image/png ")  ; Tell the browser, this is a picture, the format of the picture is Png$image = Imagecreatetruecolor (400, 300);//Create a 400*300 picture imagepng ($image);// Generate Picture Imagedestroy ($image);//Destroy picture, free memory?>

Here is a description:

The function of the header is to inform the browser: Although the file requested is a. php suffix, the returned file is not HTML but a PNG picture

Imagepng is used to export generated images to the browser in PNG data format

Imagedestroy used to finally release the created image resource

If you call the generated picture, you can call the image generated by PHP directly as a picture.

The results of the page output are as follows:

Such a canvas is done!

2. Create colors

Create color We're going to use a function imagecolorallocate () Here, and first we'll look at the syntax format for the function:

int Imagecolorallocate (resource $image, int $red, int $green, int $blue)

The first call to Imagecolorallocate () fills the background color of the color palette-based image, and every subsequent call is simply to create a color to call the other functions, such as calling the Imagefill function, to fill the canvas!

Here we use examples to detail, the specific code is as follows:

<?phpheader ("content-type:text/html; Charset=utf-8 "), header (' content-type:image/png ');//Tell the browser that this file is a PNG image $image  =  Imagecreatetruecolor ( 300, 300);//create image $red  =  imagecolorallocate ($image,  255,  0,  0);//Set color-the selection brush color in PS imagefill ( $image,  0,   0,  $red);//Fill Color-PS in the click on the canvas fill imagepng ($image);//Generate Picture Imagedestroy ($image);//Destroy picture, free memory? & Gt

The results of the page output are as follows:

3. Create a new image and fill in the background

After creating the image and creating the color, we can do it one step at a time, we need to use a function imagecreate (), let's take a look at the syntax format of the function:

Resource imagecreate (int $x _size, int $y _size)

Imagecreate () returns an image identifier that represents a blank image of size x_size and y_size. When creating images like PS, fill in the background!

Here we use examples to detail, the specific code is as follows:

<?phpheader ("content-type:text/html; Charset=utf-8 "), Header (" Content-type:image/png "), $image = Imagecreate (+)  ; Create a canvas imagecolorallocate ($image, 255, 0, 0); Sets the background color of the canvas to Red imagepng ($image);                  Output Image Imagedestroy ($image);              Releasing Resources?>

The results of the page output are:

About creating images, colors and so on introduced here, I believe that the small partners have mastered, the following we will introduce how to add text in the picture, specific reading "How to use the GD2 function to add text on the picture (PHP graphic image of the typical Application tutorial 2)"!

Related Article

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.