PHP uses built-in functions to generate images,

Source: Internet
Author: User

PHP uses built-in functions to generate images,

This example describes how PHP uses built-in functions to generate images. We will share this with you for your reference. The details are as follows:

Step 1: create an image

Create a PHP file named new-image.php (you can name it as needed to facilitate subsequent calls ).

There are two functions in php to create an image: mageCreate () to create an empty image; ImageCreateFromPng () to create an image with an existing png image as the background. (Here, "Png" can be replaced with "jpg" or "gif", which is determined based on the format of the background image)

$ MyImage = ImageCreate (, 60); // The parameter is width and height $ myImage = ImageCreateFromJpg ("http://farm5.static.flickr.com/418978874_c349c14359_o.jpg ");

No matter which method you use, we have created an image and saved it to the $ myImage variable.

Step 2: add colors

In this case, we need to use the ImageColorAllocate () function to create some colors and store them in variables for later use.

$white=ImageColorAllocate($myImage, 255, 255, 255);$black=ImageColorAllocate($myImage, 0, 0, 0);$red=ImageColorAllocate($myImage, 255, 0, 0);$green=ImageColorAllocate($myImage, 0, 255, 0);$blue=ImageColorAllocate($myImage, 0, 0, 255);

Step 3: draw images

You can also use some built-in functions of php to add some ry:

ImageEllipse () // draw an elliptical ImageArc () // draw a curve ImagePolygon () // draw a polygon ImageRectangle () // draw a rectangular ImageLine () // draw a line

The following is an example.

ImageRectangle($myImage, 50, 20, 200, 15, $blue);

At this time, you will find that only the contour of the rectangle is drawn. If you want to fill the rectangle, use the ImageFilledRectangle () method:

ImageFilledRectangle($myImage, 50, 20, 200, 15, $blue);

Step 4: Add text

You can use the imagettftext () function to add text to an image:

Copy codeThe Code is as follows: imagettftext ($ myImage, 12, 0, 5, 20, $ black, "Fonts/Oblivious font. ttf", "this is the content to be displayed ");

You must note that you need to select a font here (Fonts/Oblivious font. ttf is used here, you can use your favorite font ). If you do not know how to obtain the font, You can google it. There are a lot of websites that download the font, And the content is enough to make you dazzled.

Step 5: generate an image

When you have successfully drawn the image you want to display, You need to display it in the page. The first thing to do is to tell the page that the data you want to send to him is an image:

// Here, png can be changed to jpg or gif. Determine the header ("Content-type: image/png") based on the image format you want to generate; ImagePng ($ myImage );

Next, because we do not need the $ myImage variable, We need to release its memory:

ImageDestroy($myImage);

Step 6: Call Images

In the previous five steps, we have created an image. The following describes how to call this created image.

In an HTML file, we use a url to introduce this image:

 // in src, write the path to the new-image.php file, you will find that the image you just created has been successfully called.

Use imagepng () to generate an image file

<?php imagepng($myImage,"xxx.jpg"); ?>

For example:

<? Php $ myImage = ImageCreate (255,255,255, 60); // The parameter is width and height $ white = ImageColorAllocate ($ myImage,); $ black = ImageColorAllocate ($ myImage, 0, 0, 0); $ red = ImageColorAllocate ($ myImage, 255, 0, 0); $ green = ImageColorAllocate ($ myImage, 0,255, 0); $ blue = ImageColorAllocate ($ myImage, 0, 0,255); imagettftext ($ myImage, 12, 0, 5, 20, $ red, "stxingka. ttf "," Jelly Elvis ");?> <? Php imagepng ($ myImage, "xxx.jpg");?>

The above code will generate a xxx.jpg image file under the root directory.

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.