如果你对该文章中的内容有疑问/不解,可以点击此处链接提问
要注明问题和此文章链接地址 点击此处跳转
思路
- 建立画布
- 设置颜色
- 设置背景
- 作画(重要)
- 保存输出
- 移除画布
建立画布
只需设置宽和高
1 2 |
$img = imagecreatetruecolor(500,500); |
设置颜色
imagecolorallocate(画布资源,R,G,B)
1 2 3 4 5 6 |
$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,填充的颜色);
1 2 |
imagefill($img,0,0,$black); |
作图
- 画点(imagesetpixel)
1 2 3 4 |
imagesetpixel(画布资源,坐标x,坐标y,颜色); imagesetpixel($img,255,255,$white); |
- 划线(imageline)
1 2 3 4 |
imageline(画布资源,起点x,起点y,终点x,终点y,颜色); imageline($img,100,100,200,200,$white); |
- 画矩形(imagerectangle)
1 2 3 4 5 6 |
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); |
- 画多边形(imagepolygon)
1 2 3 4 5 6 |
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); |
- 画圆(imageellipse)
1 2 3 4 5 6 |
imageellipse(画布资源,圆心x,圆心y,x长,y长,颜色); imagefilledellipse(画布资源,圆心x,圆心y,x长,y长,颜色);//填充 imageellipse($img,250,250,400,300,$white); imagefilledellipse($img,250,250,400,300,$white); |
- 画弧线
-
文本(imagettftext)
1 2 3 4 5 |
imagettftext(画布资源,大小,弧度,开始x,开始y,颜色,字体,文本); imagettftext($img,30,45,100,200,$white,'./font/1.ttf','hahahhahaha'); |
保存
image/png image/gif image/jpeg
1 2 3 |
header("content-type:image/jpeg"); imagejpeg($img); |
销毁
1 2 |
imagedestroy($img); |