Home > Backend Development > PHP Tutorial > Talk about PHP drawing (3)_PHP tutorial

Talk about PHP drawing (3)_PHP tutorial

WBOY
Release: 2016-07-13 17:35:42
Original
801 people have browsed it

上次说到用GD作各种几何图形,以及填充颜色。其中故意把这样一个较复杂的情况
留到后面,这就是任意多边形和任意多边形的填充颜色。

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);
// 定义一个数组,12个成员是6个点的横纵坐标。
ImagePolygon($im,$parray,6,$col_grn);
// 这就是绘制任意多边形的函数,$parray是刚才定义的数组,
// 6表示六个点。注意六个点连成的是六边形。
// 不必人为地为了闭合图形而在最后增加一个与第一点相同的点。

ImagePNG($im);
ImageDestroy($im);
?>

    你应该已经想到了,任意多边形填充颜色的函数:
   
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);
?>

    嗯。下面我们可以在图象上写字了。不过,先别高兴,要想写汉字还得费一些麻烦。
这个以后再逐渐解释。先看看怎么简单地写西文字符吧。

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);
// 这里连续五次调用ImageString,在不同位置,
// 分别用从小到大的字型输出了字符串 $str。
// ImageString 函数只支持五种字型(1~5)

ImagePNG($im);
ImageDestroy($im);
?>

再看:

//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);
// 函数名换成了 ImageStringUp,用法不变。
// 是输出竖排的文字。

ImagePNG($im);
ImageDestroy($im);
?>

    在使用输出字符的函数同时,如果能知道不同字型的字在图象里要占用的宽度、高度,
对于安排输出字符的位置将是多么方便的啊!PHP提供给我们了:ImageFontWidth()和
ImageFontHeight(),其参数很简单,只有一个:即字型的编号。例如ImageFontWidth(5)
就是取得5号字每个字符的宽度,ImageFontHeight(3)就是取得3号字每个字符的高度。这么
简单,就不举例了,等一下在后面的代码中还有用到。

    跟输出字符串类似,ImageChar和ImageCharUp输出单个字符,用途比较少,甚至可以
不用——无论字符还是字符串,都用ImageString和ImageStringUp就可以了嘛!

    下面,我就利用我做过的绘制股票K线分析图的其中一部分代码,把前面讲到的内容系统
地应用一下。因为其中涉及数据库,不能把原始代码拿过来给大家拿回去测试。只能构造一些
数据,模拟从数据库里取得的股市行情。鉴于这里懂股票K线的人可能不多,大家可能不知道K线
图应该怎么画法。然而,我也不能在这里讲K线具体是怎么回事,只是介绍这样一系列方法。等画
好以后,你肯定可以看出,以前确实见过这样的图。

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 methods used to draw various objects color.

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, use The for loop draws lines, which saves a lot of trouble.

$zzg=10.55;
$zzd=7.63;
$lzg=10350;
// Hypothetical stock market data,
// $zzg is the highest value during the period that needs to be analyzed The price is assumed to be 10.55 yuan.
// $zzd is the lowest price during the period that needs 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 "scale" of the coordinate grid.
$bl=$zzg-$zzd;
// The difference between the highest price and the lowest price. According to the ratio between it and the total height of the grid,
// can get an actual price position in the grid.

for($i=1;$i<=7;$i++)
{
$y=$i*25-10;
// Calculate the label scale based on the position of the grid line the appropriate height (ordinate).
$str=Number_Format($zzg-($i-1)/6*$bl,2,".",",");
// Calculate the price and format corresponding to each tick mark ize 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 volume, it is not easy to write it in a loop. It's a good deal.
// If the number is relatively large, looping should also be used.


// Since a K-line chart requires drawing countless small K-line bars, so drawing a small K-line bar is written as a function
function kline($img,$kp,$zg, $zd,$sp,$cjl,$ii)
// Parameters: $img image; $kp $zg $zd $sp is the opening, highest, lowest, 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, $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, it is a negative line, use 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 column number.
$y1=20+$h-($kp-$zzd)/$bl*$h;
// Calculate the corresponding ordinate based on the opening price.
$y2=20+$h-($sp-$zzd)/$bl*$h;
// Calculate the corresponding ordinate based on the closing price.
$y3=20+$h-($zg-$zzd)/$bl*$h;
// Calculate the corresponding ordinate based on the highest price.
$y4=20+$h-($zd-$zzd)/$bl*$h;
// Calculate the corresponding ordinate based on the lowest price.
$y5=20+$hh-$cjl/$lzg*($hh-$h);
// Calculate the corresponding ordinate 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 decreases from 1 to increases by 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 determining 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.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/508314.htmlTechArticleLast time I talked about using GD to make 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. ...

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template