PHP uses built-in functions to generate images in a detailed way, _php tutorial

Source: Internet
Author: User
Tags getting started with php

PHP uses built-in functions to generate images in a detailed way,


This example describes how PHP uses built-in functions to generate pictures. Share to everyone for your reference, as follows:

First step: Create a picture

Create a new PHP file named new-image.php (you can name it to make it easier to call later).

PHP has two functions to create a picture: Magecreate () creates an empty picture; Imagecreatefrompng () creates a picture with an existing PNG image as the background. ("Png" here can be replaced with "JPG" or "gif", depending on the format of the background image)

$myImage =imagecreate (400,60); The parameters are width and height $myimage=imagecreatefromjpg ("http://farm5.static.flickr.com/418978874_c349c14359_o.jpg");

Whichever method you use above, we create a picture and save the picture in the $myimage variable.

Step Two: Add color

At this point we need to create some colors by using the imagecolorallocate () function to store them in a variable for easy use later.

$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 three: Draw a graphic

You can also use some of the built-in functions of PHP to add some geometry:

Imageellipse ()//Draw ellipse Imagearc ()//Draw Curve Imagepolygon ()//Draw Polygon Imagerectangle ()//Draw Rectangle Imageline ()//Draw line

Here is an example

Imagerectangle ($myImage, $blue);

At this point, you will find that only the outline of the rectangle is drawn, if you want a filled rectangle, use the Imagefilledrectangle () method:

Imagefilledrectangle ($myImage, $blue);

Fourth Step: Add text

We can use the Imagettftext () function to add text to a picture:

Copy the Code code as follows: Imagettftext ($myImage, 0, 5, $black, "Fonts/oblivious Font.ttf", "This is the content to be displayed");

You must have noticed, here you need to choose the font (here is the fonts/oblivious Font.ttf, you can use your own favorite font). If you do not know how to get the font, you can Google, there are a lot of download fonts on the website, the contents of the content is enough to dazzle you.

Fifth step: Create a picture

When you have successfully mapped the image you want to display, you will 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 a picture:

PNG can be converted to JPG, or GIF, to determine the header ("Content-type:image/png") according to the image format you want to create; Imagepng ($myImage);

Next, because we do not need to $myimage this variable, we need to release its memory:

Imagedestroy ($myImage);

Sixth step: Call the picture

Through the previous five steps, we have created a picture, below to tell you how to call this created image.

In the HTML file, we use URLs to introduce this image:

In SRC, write the path to the new-image.php file, and you'll see that the image you just created has been successfully called.

Use Imagepng () to generate a picture file

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

For example:

<?php$myimage=imagecreate (400,60); Parameters are width and height $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); Imagettftext ($myImage, 0, 5, $red, "Stxingka.ttf", "jelly Elvis");? ><?php imagepng ($myImage, "xxx.jpg");?>

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

More about PHP related content readers can view this site topic: "PHP graphics and picture Operation skills Summary", "PHP array" operation Skills Daquan, "PHP Math Skills Summary", "PHP date and Time usage summary", "PHP Object-oriented Programming tutorial", PHP String Usage Summary, "Getting Started with Php+mysql database operations" and "PHP Common Database Operations Skills Summary"

I hope this article is helpful to you in PHP programming.

http://www.bkjia.com/PHPjc/1125893.html www.bkjia.com true http://www.bkjia.com/PHPjc/1125893.html techarticle PHP uses built-in functions to generate images in a detailed way, this article describes how PHP uses built-in functions to generate pictures. Share to everyone for your reference, as follows: The first step: Create ...

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