Talking about PHP drawing (c)

Source: Internet
Author: User
Tags header variables
Last talk about using GD as a variety of geometries, as well as fill color. Which deliberately put such a more complex case
Left to the back, this is the freeform and freeform polygon fill color.

?
Header ("Content-type:image/png");
$im = imagecreate (200, 100);
$col _blk = imagecolorallocate ($im, 0,0,0);
$col _grn = imagecolorallocate ($im, 0,255,0);

$parray = Array (40,10,60,10,70,20,60,50,40,50,30,20);
Defines an array, with 12 members being the horizontal ordinate of 6 points.
Imagepolygon ($im, $parray, 6, $col _GRN);
This is the function that draws the freeform, $parray is the array you just defined,
6 represents six points. Note that the six dots are connected by hexagonal.
It is not necessary to add an identical point to the 1th at the end in order to close the graph.

Imagepng ($im);
Imagedestroy ($im);
?>

You should have thought of the function of the freeform fill color:

?
Header ("Content-type:image/png");
$im = imagecreate (200, 100);
$col _blk = imagecolorallocate ($im, 0,0,0);
$col _orn = imagecolorallocate ($im, 255,192,0);
$col _yel = imagecolorallocate ($im, 255,255,0);
$col _red = imagecolorallocate ($im, 255,0,0);
$col _grn = imagecolorallocate ($im, 0,255,0);
$col _blu = imagecolorallocate ($im, 0,0,255);

$parray = Array (40,10,60,10,70,20,60,50,40,50,30,20);
Imagefilledpolygon ($im, $parray, 6, $col _GRN);

Imagepng ($im);
Imagedestroy ($im);
?>

Well. We can write on the image below. However, do not be happy, to write Chinese characters still have to cost some trouble.
This is explained gradually later again. Let's take a look at how to simply write a western character.

?
Header ("Content-type:image/png");
$im = imagecreate (200, 250);
$col _blk = imagecolorallocate ($im, 0,0,0);
$col _orn = imagecolorallocate ($im, 255,192,0);

$str = "This is a test.";
Imagestring ($im, 1,10,10, $str, $col _orn);
Imagestring ($im, 2,10,30, $str, $col _orn);
Imagestring ($im, 3,10,60, $str, $col _orn);
Imagestring ($im, 4,10,100, $str, $col _orn);
Imagestring ($im, 5,10,150, $str, $col _orn);
Here five consecutive calls to imagestring, in different locations,
The string $str is output from a small to large font respectively.
The Imagestring function supports only five kinds of glyphs (1~5)

Imagepng ($im);
Imagedestroy ($im);
?>

Look again:

?
Header ("Content-type:image/png");
$im = imagecreate (200, 250);
$col _blk = imagecolorallocate ($im, 0,0,0);
$col _orn = imagecolorallocate ($im, 255,192,0);

$str = "This is a test.";
Imagestringup ($im, 1,10,180, $str, $col _orn);
Imagestringup ($im, 2,20,180, $str, $col _orn);
Imagestringup ($im, 3,40,180, $str, $col _orn);
Imagestringup ($im, 4,70,180, $str, $col _orn);
Imagestringup ($im, 5,110,180, $str, $col _orn);
The function name is replaced with Imagestringup, and the usage is unchanged.
is the text of the output vertical.

Imagepng ($im);
Imagedestroy ($im);
?>

In a function that uses the output character, if you know the width and height of the different font characters in the image,
How convenient is it to arrange the output character position? PHP is available to us: Imagefontwidth () and
Imagefontheight (), its parameters are simple, only one: that is, the number of the font. For example Imagefontwidth (5)
is to get the width of 5th characters per character, Imagefontheight (3) is to get the height of 3rd characters per character. So
Simple, no example, and so on later in the Code also useful.

Like the output string, Imagechar and Imagecharup output a single character that is less useful and can even
No--either character or string, use imagestring and imagestringup on it!

Below, I took advantage of the part of the code that I had done to draw the stock line analysis diagram and put the preceding content system
To apply. Because it involves the database, can not bring the original code for everyone to take back to test. can only construct some
Data to simulate stock quotes from a database. In view of the people here know the stock candlestick may not much, we may not know the K-line
How the picture should be drawn. However, I can not talk about how the K-line is specific, but the introduction of such a series of methods. Wait for the painting
Well, you can certainly see that you've seen this before.

<?php
Header ("Content-type:image/png");

$im = Imagecreate (640,260);
$bkground = Imagecolorallocate ($im, 255,255,255);
$data = Imagecolorallocate ($im, 0,0,0);
$gird = Imagecolorallocate ($im, 200,200,160);
$upline = Imagecolorallocate ($im, 255,0,0);
$dnline = Imagecolorallocate ($im, 0,175,175);
$d 5line = imagecolorallocate ($im, 255,127,0);
$d 20line = imagecolorallocate ($im, 0,0,127);
$d 10line = imagecolorallocate ($im, 255,0,255);
First, define the colors used to draw the various objects.

for ($i =20; $i <=220; $i +=25)
Imageline ($im, $i, 560, $i, $gird);
for ($j =60; $j <=560; $j +=25)
Imageline ($im, $j, $j, $gird);
Calculate a good position in advance, lattice width, with for loop line drawing, more convenient.

$ZZG = 10.55;
$zzd = 7.63;
$LZG = 10350;
Hypothetical stock market data,
$ZZG is the highest price to analyze for a period of time, assuming it is 10.55 yuan.
$ZZD is the lowest price that needs to be analyzed for a period of time, assuming it is 7.63 yuan.
$LZG is required to analyze the maximum turnover for this period of time, assuming that it is 10350 hands.
This is an important data that calculates the "scale" of the coordinate grid.
$BL = $zzg-$zzd;
The difference between the top price and the lowest price. According to the ratio between the total height of the grid,
You can draw a real price in the grid where the position.

For ($i =1 $i <=7; $i + +)
{
$y = $i *25-10;
Calculates the appropriate height (ordinate) of the callout scale based on the position of the grid line.
$str =number_format ($ZZG-($i-1)/6* $BL, 2, ".", ",");
Calculates the price for each tick mark, and formats the string.
$x =55-imagefontwidth (2) *strlen ($STR);
Calculate the appropriate horizontal axis according to the width that the string will occupy.
Imagestring ($im, 2, $x, $y, $str, $data);
Write this string.
}

$str =number_format ($LZG, 0, ".", ",");
Imagestring ($im, 2,564,164, $str, $data);
$str =number_format ($lzg/2,0, ".", ",");
Imagestring ($im, 2,564,189, $str, $data);
Because there are only two marks on the volume, it is not cost-effective to write with a loop.
If the quantity is more, the circulation should also be used.


Because a piece of chart to draw countless root small candlestick, so, draw a small K-line column written function
Function Kline ($img, $KP, $zg, $zd, $sp, $CJL, $ii)
Parameters: $img image; $KP $zg $zd $sp is the opening, the highest, the lowest, the closing;
$CJL volume; $ii counter that represents the serial number of the K-bar.
{
Global $BL, $ZZD, $LZG;
Declare the $BL used in the function, $ZZD, $LZG Three variables are global variables.
$h = 150; The K-bar area height is 150.
$HH = 200; K-Line column area, volume column area total height is 200.

if ($sp < $KP)
$linecolor = Imagecolorallocate ($img, 0,175,175);
If the closing price is lower than the opening, it is overcast line, with Cyan
Else
$linecolor = Imagecolorallocate ($img, 255,0,0);
Otherwise the Yang line, with red.

$x =58+ $ii *4;
Calculate the horizontal axis according to the K-line column ordinal.
$y 1=20+ $h-($KP-$ZZD)/$BL * $H;
The corresponding ordinate is calculated according to the opening price.
$y 2=20+ $h-($SP-$ZZD)/$BL * $H;
Calculates the corresponding ordinate according to the closing price.
$y 3=20+ $h-($ZG-$ZZD)/$BL * $H;
Calculate the corresponding ordinate based on the highest price.
$y 4=20+ $h-($ZD-$ZZD)/$BL * $H;
Calculate the corresponding ordinate according to the lowest price.
$y 5=20+ $hh-$CJL/$lzg * ($HH-$h);
According to the volume calculation corresponding ordinate.

if ($y 1<= $y 2) imagefilledrectangle ($img, $x-1, $y 1, $x +1, $y 2, $linecolor);
else Imagefilledrectangle ($img, $x-1, $y 2, $x +1, $y 1, $linecolor);
The horizontal coordinate is reduced by 1 to plus 1, and the span is 3. A small filled rectangle with a width of 3 is painted.
The height and ordinate are determined by the opening and closing price.
The test found that this function must be left to the point coordinates written in the lower right point coordinates,
Instead of automatically judging two points to the left, which is the lower right.

Imagefilledrectangle ($img, $x-1, $y 5, $x +1,220, $linecolor);
Draw Volume column according to turnover.
Imageline ($img, $x, $y 3, $x, $y 4, $linecolor);
Draw the upper and lower shadow lines according to the highest price and lowest price.
}

Try drawing a root. Open 8.50 Top 8.88 minimum 8.32 close 8.80 deal 6578 hands.
Kline ($im, 8.50,8.88,8.32,8.80,6578,1);
Draw one more. Open 8.80 Top 9.50 minimum 8.80 close 9.50 deal 8070 hands.
The big Yang line of bare feet of bald head!
Kline ($im, 8.80,9.50,8.80,9.50,8070,2);
One more yin line. Open 9.80 top 9.80 minimum 8.90 close 9.06 deal 10070 hands.
It's Lost! How nice to throw away yesterday.
Kline ($im, 9.80,9.80,8.90,9.06,10070,3);

......

Imagepng ($im);
Imagedestroy ($im);
?>


Of course, it's too much trouble to write every day's data like that. What I do is fetch the data from the database.


This time, here.

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.