How does PHP generate dynamic images?

Source: Internet
Author: User
Summary: PHP has a set of image functions, can dynamically generate GIF format image data stream and output to the server, so that we can in accordance with the needs of users in time to generate images online. Now let's talk about how to generate a dynamic image using PHP as an example of creating a button.

How is the dynamic image generated?

Let's take a look at the following code, the default value is "go!" if the page is browsed for the first time or if the user's form is empty. If the user enters information and submits the form, the code is called back and initialized.

?
If (!isset ($string) | | $string = = "") {$string = "go!";}
? >


Button text;


The rest of the file is simply to output some elements in the table and invoke other files.

Now, let's start making buttons. The first example is the Oval text button that produces a black-side green bottom. We display a button image through a standard tag, and the value of SRC is the file that generates the image of the button.

">

Below, let's look at the file "green_ellipse.php" that generates the ellipse

Generate ellipse

Here's a list of green_ellipse.php code that starts with header () and sends an HTTP header message to the Apache server, which tells the server what data to use, and in this case we use the GIF format:

?
Header ("Content-type:image/gif");

Now we're going to generate an empty image and assign it to the variable $id:
$id =imagecreate ($img _width, $img _height);
Image Polishing
......
Use the embedded number fifth to write text on the button, and then set the variable $black color for the text on the button:
Imagestring ($id, 5, ($img _center_x-($str _width/2+1), img_center_y-($str _height/2)), $string, $black);
Finally, the image is shown from memory to the standard output buffer--This is the content of the image that the server will get:
Imagegif ($id);
? >

This program only fits like "go!" Such a short button, like "Please click here!" Such a long string would not look good. It doesn't matter, look at this rectangle with the round edge.

To produce a more beautiful pattern

This example will produce a black-edged, yellow-angled pattern. You can see that this method is better suited to a long string of buttons than the first example.

If you look at the previous code, the code here is easy to read. We'll start with the shape that produces the button.

Creates an edge of 20 pixels at the end of each string and constructs an edge of 10 pixels above and below the string:

initialization, parameter setting
......
In the image of the left drew a semicircle, and then from six points through nine points to 12 points:
Imagearc ($id, $img _center_y,20, $str _height+20,90,270, $black);
For the right end, it's the same as the top, just starting at 20 pixels from the right, and skimming the opposite half--from 12 points through 3 to six points:
Imagearc ($id, ($img _width-20), $img _center_y,20, $img _height+20,270,90, $black);
Finally, draw a straight line from the top end of one end to the other, as well as a straight line at the bottom. This closes the interior of the shape.
Imageline ($id, 20,0, ($img _width-20), 0, $black);
Imageline ($id, $img _height-1, ($img _width-20), $img _height-1, $black);
In this area, fill in the color defined by the variable $yellow and fill the entire area:
Imagefilltoborder ($id, $img _center_x, $img _center_y, $black, $yellow);
Then writes the string to the image, then prints the image to the standard output:
Imagestring ($id, 5, ($img _center_x-($str _width/2) +1, ($img _center_y-($str _height/2)), $string, $black);
Imagegif ($id);
? >
How do I modify an image that already exists?

Here, we will modify an existing image, save it as a new image file, and display a dynamic image. First, we call the mymonkdy.gif into memory and assign it to $id.

?
$id =imagecreatefromgif ("Mymonkey.gif");
Now let's set the font color and put it on the image.
$yellow =imagecolorallocate ($id, 255,255,0);
With the upper-left corner (0,0), the string is written in embedded font fourth from the (10,20) place, and I set it to uppercase and an exclamation point to make it more vivid.
Imagestring ($id, 4,10,20,strtoupper ($string, "!"), $yellow);
Now we store the image from memory into the file http://edu.cnzz.cn/NewsInfo/newmonkey.gif.
Imagegif ($id, "http://edu.cnzz.cn/NewsInfo/newmonkey.gif");
? >
This is about the same: Http://edu.cnzz.cn/NewsInfo/newmonkey.gif can be used to show that the browser is set up like this:




How to do the graphical display of statistical data without the support of GD in PHP?

The use of HTML graphics control can be fully achieved our needs, before we begin to review the HTML control of graphics syntax:

"IMG src=# alt=# width=# height=# ... >

What we need to do is control the width and height two parameters, and here we give an example:

$rows []={12,45,43,23,32,87,14,22,19,40}; Array initialization, which can be taken out of the data.
$sums = 0;
for ($i = 0; $i $i + +) $sums + = $rows [$i]; Sum
for ($i = 0; $i $i + +) {
if ($sums * $rows [$i]) {//0 will not be processed to avoid 0 except overflow
$tmp = $rows [$i]/$sums *100;
echo " ";
Output graphics, Http://edu.cnzz.cn/NewsInfo/bar.gif is only a point of the graphics file, here is the horizontal graphics, vertical change height can be.
echo "(". sprintf ('%01.2f ', $tmp). "%);
echo "
";
}
}
? >

How to implement the interactive operation of PHP and Flash animation?

We use a flash plus PHP as an example of identity authentication to illustrate:

First, add three TextField and a button to the home scene.

And then it is to give these three TextField to pay the name.
 first is name, used to enter user name
 The second is password, used to enter a password
 The third is output, which is used to show the result of judgment.

The last is the click of the button to add action.

load Variables ("check.php", 1, Vars=post)

This action is a lot of content, the first is to the main scene of several variable values to check.php. Second, we will return the output value from the check.php.

  .....
 .....
///use the SQL statement to find the $name password assigned to password
//determines the contents of the second text box, whether the password is found in the first text text box name
if ($passwd = = $password)
echo "Output=welcome". $name. "This was a program by PHP and Flash"; If equal, the value of the output text box is equal to welcome information
else
echo "Output=sorry,". $name. "Your password is not correct"; If not equal, an error message is displayed.
?>



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.