php-GD2-图形绘制

基本图形

1.绘制线条:

bool imageline(resource img, int x1,int y1,int x2,int y2,resource color);

参数说明:(图像对象,起始x坐标,起始y坐标,结束x坐标,结束y坐标,颜色对象)

<?php
header("Content-type:image/png");
$im=imagecreate(200,200);
//设置画布颜色
$bg=imagecolorallocate($im, 255, 45, 26);
$white=imagecolorallocate($im, 255, 255, 255);
imageline($im, 1, 12, 150, 150, $white);
imagepng($im);
imagedestroy($im);
?>



2.绘制圆

bool imagearc(resource image,int cx,int cy,int w,int h,int s,int e,source color);

参数说明:(图像对象,起点坐标x,起点坐标y,水平轴长w,水平轴长h,起始角度s,结束角度e,颜色对象)

<?php
header("Content-type:image/png");
$im=imagecreate(200,200);
//设置画布颜色
$bg=imagecolorallocate($im, 255, 45, 26);
$white=imagecolorallocate($im, 255, 255, 255);
imagearc($im, 90, 90, 100, 100, 0, 360, $white);
imagepng($im);
imagedestroy($im);
?>


3.绘制矩形

bool imagerectangle(resource img,int x1,int y1,int x2,int y2,resource color);

参数说明:(图像对象,左上角x坐标,左上角y坐标,右下角x坐标,右下角y坐标,颜色对象)

<?php
header("Content-type:image/png");
$im=imagecreate(200,200);
//设置画布颜色
$bg=imagecolorallocate($im, 255, 45, 26);
$white=imagecolorallocate($im, 255, 255, 255);
imagerectangle($im, 50, 40, 100, 150, $white);
imagepng($im);
imagedestroy($im);
?>

4.绘制像素点

bool imagesetpixel(resource img, int x, int y, resource color);

<?php
header("Content-type:image/png");
$im=imagecreate(200,200);
$bg=imagecolorallocate($im, 255, 45, 26);
$white=imagecolorallocate($im, 255, 255, 255);
imagesetpixel($im, 50, 50, $white);
imagepng($im);
imagedestroy($im);
?>

填充图形

1.圆形填充

bool imagefilledellipse(resource img,int x ,int y,int w,int h,resource color)

<?php
header("Content-type:image/png");
$im=imagecreate(200,200);
//设置画布颜色
$bg=imagecolorallocate($im, 255, 45, 26);
$white=imagecolorallocate($im, 255, 255, 255);
imagefilledellipse($im, 64, 64,50,90,$white);
imagepng($im);
imagedestroy($im);
?>

2.矩形填充

imagefilledrectangle(resource,int cx, int cy, int ex, int ey, resource color)

<?php
header("Content-type:image/png");
$im=imagecreate(200,200);
//设置画布颜色
$bg=imagecolorallocate($im, 255, 45, 26);
$white=imagecolorallocate($im, 255, 255, 255);
imagefilledrectangle($im, 50, 50, 150, 150, $white);
imagepng($im);
imagedestroy($im);
?>


输出文字

1.输出文字

bool imagestring(resource img,int font,int x,int y,resource color)

<?php
header("Content-type:image/png");
$im=imagecreate(200,200);
$bg=imagecolorallocate($im, 255, 45, 26);
$white=imagecolorallocate($im, 255, 255, 255);
imagestring($im, 5, 50,10, "ABCD", $white);
imagepng($im);
imagedestroy($im);
?>


2.输出文字---指定字体

imagettftext(resource img, int fontsize, int angel, int x, int y, resource color, string fontFilePath, string);

如果要输出的文字中有中文存在,需要在文字写入画布之前用iconv()函数将字符转换成utf-8

$text=iconv("gb2312","utf-8","中华人民共合国");

参数说明:(图像对象,字体大小,角度,起始X坐标,起始Y坐标,颜色对象,字体文件路径,要输出的字符串)

<?php  
header("Content-type:image/png");  
$im=imagecreate(200,200);  
$bg=imagecolorallocate($im, 255, 45, 26);  
$white=imagecolorallocate($im, 255, 255, 255);
imagettftext($im, 20, 14, 50, 50, $white, 'couri.ttf', 'yutr');
imagepng($im);
imagedestroy($im);
?>















评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值