上次说到用GD作各种几何图形,以及填充颜色。其中故意把这样一个较复杂的情况 你应该已经想到了,任意多边形填充颜色的函数: $parray = array(40,10,60,10,70,20,60,50,40,50,30,20); 嗯。下面我们可以在图象上写字了。不过,先别高兴,要想写汉字还得费一些麻烦。 再看: ImagePNG($im); 在使用输出字符的函数同时,如果能知道不同字型的字在图象里要占用的宽度、高度, 跟输出字符串类似,ImageChar和ImageCharUp输出单个字符,用途比较少,甚至可以 下面,我就利用我做过的绘制股票K线分析图的其中一部分代码,把前面讲到的内容系统 Header("Content-type: image/png"); $im = ImageCreate(640,260); for($i=20;$i ImageLine($im, 60, $i, 560, $i, $gird); $zzg=10.55; for($i=1;$i { $str=Number_Format($lzg,0,".",","); if($y1 else ImageFilledRectangle($img,$x-1,$y2,$x+1,$y1,$linecolor);
留到后面,这就是任意多边形和任意多边形的填充颜色。
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);
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,用法不变。
// 是输出竖排的文字。
ImageDestroy($im);
?>
对于安排输出字符的位置将是多么方便的啊!PHP提供给我们了:ImageFontWidth()和
ImageFontHeight(),其参数很简单,只有一个:即字型的编号。例如ImageFontWidth(5)
就是取得5号字每个字符的宽度,ImageFontHeight(3)就是取得3号字每个字符的高度。这么
简单,就不举例了,等一下在后面的代码中还有用到。
不用——无论字符还是字符串,都用ImageString和ImageStringUp就可以了嘛!
地应用一下。因为其中涉及数据库,不能把原始代码拿过来给大家拿回去测试。只能构造一些
数据,模拟从数据库里取得的股市行情。鉴于这里懂股票K线的人可能不多,大家可能不知道K线
图应该怎么画法。然而,我也不能在这里讲K线具体是怎么回事,只是介绍这样一系列方法。等画
好以后,你肯定可以看出,以前确实见过这样的图。
$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);
// 先定义好绘各种对象所用的颜色。
for($j=60;$j ImageLine($im, $j, 20, $j, 220, $gird);
// 事先计算好位置、格子宽度,用for循环画线,省事多了。
$zzd=7.63;
$lzg=10350;
// 假设的股市数据,
// $zzg是需要分析的这一段时间的最高价,假设是10.55元。
// $zzd是需要分析的这一段时间的最低价,假设是7.63元。
// $lzg是需要分析的这一段时间的最高成交量,假设是10350手。
// 这是计算坐标网格的“刻度”的重要数据。
$bl=$zzg-$zzd;
// 最高价跟最低价的差额。根据它跟网格总高度之间的比例,
// 就可以得出一个实际的价格在网格里所处的位置。
$y=$i*25-10;
// 根据网格线的位置计算标注刻度的合适高度(纵坐标)。
$str=Number_Format($zzg-($i-1)/6*$bl,2,".",",");
// 计算每根刻度线对应的价格、并格式化该字符串。
$x=55-ImageFontWidth(2)*StrLen($str);
// 根据这个字符串将要占用的宽度,计算出合适的横坐标。
ImageString($im, 2,$x, $y,$str, $data);
// 写出这个字符串。
}
ImageString($im,2,564,164,$str,$data);
$str=Number_Format($lzg/2,0,".",",");
ImageString($im,2,564,189,$str,$data);
// 由于写成交量的刻度只有两处,用循环写就不合算了。
// 如果数量比较多,也应该用循环。
// 由于一张K线图要画无数根小K线柱,所以,把画一根小K线柱写成函数
function kline($img,$kp,$zg,$zd,$sp,$cjl,$ii)
// 参数:$img 图象;$kp $zg $zd $sp 是开盘、最高、最低、收盘;
// $cjl 成交量;$ii 计数器,表示K线柱的序号。
{
global $bl,$zzd,$lzg;
// 声明该函数里用到的$bl,$zzd,$lzg三个变量是全局变量。
$h=150; // K线柱区域高度是 150。
$hh=200; // K线柱区域、成交量柱区域总高度是 200。
if($sp $linecolor = ImageColorAllocate($img,0,175,175);
// 如果收盘价低于开盘,是阴线,用青色
else
$linecolor = ImageColorAllocate($img,255,0,0);
// 否则为阳线,用红色。
$x=58+$ii*4;
// 根据K线柱序号计算横坐标。
$y1=20+$h-($kp-$zzd)/$bl*$h;
// 根据开盘价计算对应纵坐标。
$y2=20+$h-($sp-$zzd)/$bl*$h;
// 根据收盘价计算对应纵坐标。
$y3=20+$h-($zg-$zzd)/$bl*$h;
// 根据最高价计算对应纵坐标。
$y4=20+$h-($zd-$zzd)/$bl*$h;
// 根据最低价计算对应纵坐标。
$y5=20+$hh-$cjl/$lzg*($hh-$h);
// 根据成交量计算对应纵坐标。
// 横坐标减1到加1,跨度为3。即绘宽度为3的小填充矩形。
// 高度和纵坐标则是由开盘、收盘价决定的。
// 经测试发现,这个函数必须是左上点坐标写在右下点坐标之前,
// 而非自动判断两点孰为左上,孰为右下。
ImageFilledRectangle($img,$x-1,$y5,$x+1,220,$linecolor);
// 根据成交量绘成交量柱体。
ImageLine($img,$x,$y3,$x,$y4,$linecolor);
// 根据最高价、最低价绘上下影线。

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

实现方法:1、使用“sleep(延迟秒数)”语句,可延迟执行函数若干秒;2、使用“time_nanosleep(延迟秒数,延迟纳秒数)”语句,可延迟执行函数若干秒和纳秒;3、使用“time_sleep_until(time()+7)”语句。

php字符串有下标。在PHP中,下标不仅可以应用于数组和对象,还可应用于字符串,利用字符串的下标和中括号“[]”可以访问指定索引位置的字符,并对该字符进行读写,语法“字符串名[下标值]”;字符串的下标值(索引值)只能是整数类型,起始值为0。

php除以100保留两位小数的方法:1、利用“/”运算符进行除法运算,语法“数值 / 100”;2、使用“number_format(除法结果, 2)”或“sprintf("%.2f",除法结果)”语句进行四舍五入的处理值,并保留两位小数。

在php中,可以使用substr()函数来读取字符串后几个字符,只需要将该函数的第二个参数设置为负值,第三个参数省略即可;语法为“substr(字符串,-n)”,表示读取从字符串结尾处向前数第n个字符开始,直到字符串结尾的全部字符。

判断方法:1、使用“strtotime("年-月-日")”语句将给定的年月日转换为时间戳格式;2、用“date("z",时间戳)+1”语句计算指定时间戳是一年的第几天。date()返回的天数是从0开始计算的,因此真实天数需要在此基础上加1。

方法:1、用“str_replace(" ","其他字符",$str)”语句,可将nbsp符替换为其他字符;2、用“preg_replace("/(\s|\ \;||\xc2\xa0)/","其他字符",$str)”语句。

查找方法:1、用strpos(),语法“strpos("字符串值","查找子串")+1”;2、用stripos(),语法“strpos("字符串值","查找子串")+1”。因为字符串是从0开始计数的,因此两个函数获取的位置需要进行加1处理。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Dreamweaver CS6
Visual web development tools

WebStorm Mac version
Useful JavaScript development tools