Last time I talked about using GD to create various geometric shapes and fill colors. One of the more complicated situations is deliberately left for later, which is the fill color of arbitrary polygons and arbitrary polygons.
<?
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);
//Define an array whose 12 members are the horizontal and vertical coordinates of 6 points.
ImagePolygon($im,$parray,6,$col_grn);
// This is the function for drawing arbitrary polygons. $parray is the array just defined.
// 6 means six points. Note that the six points connected form a hexagon.
// There is no need to artificially add a point at the end that is the same as the first point in order to close the shape.
ImagePNG($im);
ImageDestroy($im);
?>
You should have thought of the function of filling color of any polygon:
<?
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);
?>
Hmm. Now we can write on the image. But don’t get too excited just yet, writing Chinese characters will take some trouble.
This will be explained gradually later. Let’s take a look at how to write Western characters simply.
<?
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, ImageString is called five times in a row, at different locations.
//The string $str is output in font sizes from small to large.
// ImageString function only supports five fonts (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 changed to ImageStringUp, and the usage remains unchanged.
// is to output vertical text.
ImagePNG($im);
ImageDestroy($im);
?>
While using the function that outputs characters, if you can know the width and height that characters of different font types occupy in the image,
How convenient it would be to arrange the position of output characters! PHP provides us with: ImageFontWidth() and
ImageFontHeight(), its parameter is very simple, there is only one: the number of the font. For example ImageFontWidth(5)
It is to get the width of each character of size 5 font, and ImageFontHeight(3) is to get the height of each character of size 3 font. It's so simple, so I won't give an example. It will be used in the subsequent code later.
Similar to outputting strings, ImageChar and ImageCharUp output a single character. They have fewer uses and may not even be used - regardless of characters or strings, just use ImageString and ImageStringUp!
Next, I will use part of the code that I have used to draw stock K-line analysis charts to systematically apply the content mentioned above. Because it involves a database, we cannot bring the original code over for everyone to take back for testing. We can only construct some data to simulate the stock market conditions obtained from the database. Since there may not be many people here who understand the stock K-line, you may not know how to draw the K-line chart. However, I can't talk about the specifics of K-line here. I can only introduce a series of methods. After you finish the drawing, you will definitely be able to tell that you have seen pictures like 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);
$d5line = ImageColorAllocate($im,255,127,0);
$d20line = ImageColorAllocate($im,0,0,127);
$d10line = ImageColorAllocate($im,255,0,255);
// First define the colors used to draw various objects.
for($i=20;$i<=220;$i+=25)
ImageLine($im, 60, $i, 560, $i, $gird);
for($j=60;$j<=560;$j+=25)
ImageLine($im, $j, 20, $j, 220, $gird);
// Calculate the position and grid width in advance, and use a for loop to draw the line, which saves a lot of trouble.
$zzg=10.55;
$zzd=7.63;
$lzg=10350;
// Hypothetical stock market data,
// $zzg is the highest price during the period to be analyzed, assuming it is 10.55 yuan.
//$zzd is the lowest price during the period to be analyzed, assuming it is 7.63 yuan.
// $lzg is the highest trading volume during the period that needs to be analyzed, assuming it is 10350 lots.
// This is important data for calculating the "tick" of the coordinate grid.
$bl=$zzg-$zzd;
// The difference between the highest price and the lowest price. According to its ratio to the total height of the grid,
// You can get an actual price position in the grid.
for($i=1;$i<=7;$i++)
{
$y=$i*25-10;
// Calculate the appropriate height (ordinate) of the label scale based on the position of the grid lines.
$str=Number_Format($zzg-($i-1)/6*$bl,2,".",",");
// Calculate the price corresponding to each tick mark and format the string.
$x=55-ImageFontWidth(2)*StrLen($str);
// Calculate the appropriate abscissa based on the width that this 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);
// Since there are only two scales for writing trading volume, it is not cost-effective to write in a loop.
// If the number is relatively large, a loop should also be used.
// Since a K-line chart requires drawing countless small K-line bars, draw a small K-line bar as a function
function kline($img,$kp,$zg,$zd,$sp,$cjl,$ii)
// Parameters: $img image; $kp $zg $zd $sp is the opening, highest, lowest and closing price;
// $cjl trading volume; $ii counter, indicating the serial number of the K-line bar.
{
global $bl,$zzd,$lzg;
// Declare the three variables $bl, $zzd, and $lzg used in this function as global variables.
$h=150; //The height of the K-line column area is 150.
$hh=200; //The total height of the K-line column area and the volume column area is 200.
if($sp<$kp)
$linecolor = ImageColorAllocate($img,0,175,175);
// If the closing price is lower than the opening price, it is a negative line, colored cyan
else
$linecolor = ImageColorAllocate($img,255,0,0);
// Otherwise, it is a positive line, use red.
$x=58+$ii*4;
// Calculate the abscissa based on the K-line bar number.
$y1=20+$h-($kp-$zzd)/$bl*$h;
// Calculate the corresponding vertical coordinate based on the opening price.
$y2=20+$h-($sp-$zzd)/$bl*$h;
// Calculate the corresponding vertical coordinate based on the closing price.
$y3=20+$h-($zg-$zzd)/$bl*$h;
// Calculate the corresponding vertical coordinate based on the highest price.
$y4=20+$h-($zd-$zzd)/$bl*$h;
// Calculate the corresponding vertical coordinate based on the lowest price.
$y5=20+$hh-$cjl/$lzg*($hh-$h);
// Calculate the corresponding vertical coordinate based on the trading volume.
if($y1<=$y2) ImageFilledRectangle($img,$x-1,$y1,$x+1,$y2,$linecolor);
else ImageFilledRectangle($img,$x-1,$y2,$x+1,$y1,$linecolor);
//The abscissa is minus 1 to plus 1, and the span is 3. That is, draw a small filled rectangle with a width of 3.
//The height and vertical coordinate are determined by the opening and closing prices.
// After testing, it was found that this function must write the coordinates of the upper left point before the coordinates of the lower right point.
// Instead of automatically judging which of the two points is the upper left or the lower right.
ImageFilledRectangle($img,$x-1,$y5,$x+1,220,$linecolor);
//Draw the volume cylinder based on the trading volume.
ImageLine($img,$x,$y3,$x,$y4,$linecolor);
// Draw upper and lower shadow lines based on the highest price and lowest price.
}
// Try drawing one. The opening was 8.50, the highest was 8.88, the lowest was 8.32, and the closing was 8.80, with 6578 lots traded.
kline($im,8.50,8.88,8.32,8.80,6578,1);
//Draw another one. The opening was 8.80, the highest was 9.50, the lowest was 8.80, and the closing was 9.50, with 8,070 lots traded.
// What a great Yangxian with a bare head and bare feet!
kline($im,8.80,9.50,8.80,9.50,8070,2);
// Another negative line. The opening was 9.80, the highest was 9.80, the lowest was 8.90, and the closing was 9.06, with 10,070 lots traded.
// Paid! How nice it was to throw it away yesterday.
kline($im,9.80,9.80,8.90,9.06,10070,3);
// ……
ImagePNG($im);
ImageDestroy($im);
?>
Of course, it would be too troublesome to write data like this for every day. What I do is fetch data from the database.
That’s all for this time.