php GD库简单使用和封装

GD库创建图像步骤

 1 <?php
 2 //1.创建画布
 3 $width = 300;
 4 $height= 200;
 5 $image=imagecreatetruecolor($width,$height);
 6 
 7 //2.创建颜色 [RGB红绿蓝]
 8 $white=imagecolorallocate($image,255,255,255);//白色
 9 $black=imagecolorallocate($image,0,0,0);//黑色
10 $red=imagecolorallocate($image,255,0,0);//红色
11 $green=imagecolorallocate($image,0,255,0);//绿色
12 $blue=imagecolorallocate($image,0,0,255);//蓝色
13 
14 //3.进行绘画
15 imagefill($image,0,0,$white);//将背景设置为白色,默认黑色
16 //水平绘制字符
17 imagechar($image,2,40,40,'R',$red);
18 //垂直绘制字符
19 imagecharup($image,3,80,80,'G',$green);
20 
21 //水平绘制字符串
22 imagestring($image,4,120,120,"BLUE",$blue);
23 //垂直绘制字符
24 imagestringup($image,5,160,160,'BLACK',$black);
25 
26 //画出一条红色的线
27 imageline($image,20,15,200,150,$red);
28 
29 //4.输出或保存
30 header('content-type:image/png');
31 imagejpeg($image);
32 
33 if(imagejpeg($image,'./gd.png')) {
34     echo '保存成功';
35 } else {
36     echo '保存失败';
37 }
38 
39 //5.销毁画布
40 imagedestroy($image);

猜你喜欢

转载自www.cnblogs.com/cxx8181602/p/10634617.html
今日推荐