Home  >  Article  >  php教程  >  使用PHP3建立动态图象

使用PHP3建立动态图象

WBOY
WBOYOriginal
2016-06-21 09:02:07921browse

 

大多数人并不了解php3也可以处理非HTML类型的数据,例如图象,我们可以用它来建立条型、柱状、饼图来反应数据库的数据,我们也可以用PHP3来建立好看的图形按纽。 大多数图形按是用图象编辑工具完成的,对于对图象无经验的程序员来说,这可是一件苦差事,在建立一个站点的时候,按的风格大多是统一的,区别在于按上的字,为了解除苦恼,我们可以使用TTF字体和PHP3的图形函数库建立一个按函数,需要按的时候调用一下即可,下面就是程序button.php3

本函数传递两个参数,$s是字体大小,$text是字,如有空格用+号代替


Header( "Content-type: image/gif");
//
缺省字体大小,未设的话为11
if(!isset($s)) $s=11;
/*
计算ttf文字text所占区域
函数imagettfbbox(字体大小,旋转角度,字体路径,)
传回一个数组,有八个数组元素
size[0]=
左下X坐标
size[1]=
左下Y坐标
size[2]=
右下X坐标
size[3]=
右下Y坐标
size[4]=
右上X坐标
size[5]=
右上Y坐标
size[4]=
左上X坐标
size[5]=
左上Y坐标
*/
$size = imagettfbbox($s,0, "/fonts/TIMES.TTF",$text);
//
取字串的长度和高度绝对值,如是doubleabs后还是double,其它abs后变成int类型
$dx = abs($size[2]-$size[0]);
$dy = abs($size[5]-$size[3]);
//
为上下左右留出各4个象素左右的空隙,加上一个象素的阴影,共4*2+1=9
$xpad=9;
$ypad=9;
//
建立图象区域
$im = imagecreate($dx+$xpad,$dy+$ypad);
//
设置颜色ImageColorAllocate(图象句柄,red,green,blue)三原色
$blue = ImageColorAllocate($im, 0x2c,0x6D,0xAF);
$black = ImageColorAllocate($im, 0,0,0);
$white = ImageColorAllocate($im, 255,255,255);
//
绘制矩形ImageRectangle(图象句柄,左上X,左上Y,右下X,右下Y,颜色)
//
绘出阴影
ImageRectangle($im,1,1,$dx+$xpad,$dy+$ypad,$black); ImageRectangle($im,0,0,$dx+$xpad-1,$dy+$ypad-1,$white); ImageRectangle($im,1,1,$dx+$xpad-1,$dy+$ypad-1,$blue);
//
写字体到图象
//ImageTTFText(
图象句柄,字体大小,旋转角度,字左上X,字左上Y,颜色,字体路径,字)
//
绘出阴影
ImageTTFText($im, $s, 0, (int)($xpad/2)+1, $dy+(int)($ypad/2), $black, "/fonts/TIMES.TTF", $text);
ImageTTFText($im, $s, 0, (int)($xpad/2), $dy+(int)($ypad/2)-1, $white, "/fonts/TIMES.TTF", $text);
//
转化为gif
ImageGif($im);
ImageDestroy($im);
?>

注意:不要在这个文件中包含任何HTML的代码,同时在 ?>之间也不能有空行,否则程序将无法正常运行,本函数最终将建立一副图象

 



Statement:
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
Previous article:正规表达式Next article:引用文件