限时免费试用:欢迎注册 api.bigmodel.org ,快速体验大模型 API 接入服务。
当前位置:首页 >开发者 >PHP笔记 >PHP基础

PHPGD库

分类:PHP基础时间:2017-11-22浏览:4245

思路

  1. 建立画布
  2. 设置颜色
  3. 设置背景
  4. 作画(重要)
  5. 保存输出
  6. 移除画布

建立画布

只需设置宽和高
$img = imagecreatetruecolor(500,500);

设置颜色

imagecolorallocate(画布资源,R,G,B)
$white = imagecolorallocate($img,255,255,255);
$black = imagecolorallocate($img,0,0,0);
$red = imagecolorallocate($img,255,0,0);
$blue = imagecolorallocate($img,0,0,255);
$green = imagecolorallocate($img,0,255,0);

填充背景

imagefill(画布资源,0,0,填充的颜色);
imagefill($img,0,0,$black);

作图

  1. 画点(imagesetpixel)
imagesetpixel(画布资源,坐标x,坐标y,颜色);

imagesetpixel($img,255,255,$white);
  1. 划线(imageline)
imageline(画布资源,起点x,起点y,终点x,终点y,颜色);

imageline($img,100,100,200,200,$white);
  1. 画矩形(imagerectangle)
imagerectangle(画布资源,起点x,起点y,终点x,终点y,颜色);
imagefilledrectangle(画布资源,起点x,起点y,终点x,终点y,颜色);//填充(filled)

imagerectangle($img,100,100,400,400,$white);
imagefilledrectangle($img,100,100,400,400,$white);
  1. 画多边形(imagepolygon)
imagepolygon($img,array(各坐标),几边形,颜色);
imagefilledpolygon($img,array(各坐标),几边形,颜色);//填充

imagepolygon($img,array(100,100,100,400,400,400),3,$white);
imagefilledpolygon($img,array(100,100,100,400,400,400),3,$white);
  1. 画圆(imageellipse)
imageellipse(画布资源,圆心x,圆心y,x长,y长,颜色);
imagefilledellipse(画布资源,圆心x,圆心y,x长,y长,颜色);//填充

imageellipse($img,250,250,400,300,$white);
imagefilledellipse($img,250,250,400,300,$white);
  1. 画弧线

  2. 文本(imagettftext)

imagettftext(画布资源,大小,弧度,开始x,开始y,颜色,字体,文本);

imagettftext($img,30,45,100,200,$white,'./font/1.ttf','hahahhahaha');

保存

image/png image/gif image/jpeg

header("content-type:image/jpeg");
imagejpeg($img);

销毁

imagedestroy($img);

本站文章如未注明出处均为原创,转载请注明出处,如有侵权请邮件联系站长。
0/500
Share your thoughts respectfully.