[Lpp]PHP的GD库

本文介绍了PHP的GD库,包括如何创建画布、分配颜色、绘画操作如填充、线条、矩形、椭圆、文字等,并展示了如何输出图像、进行图像处理以及最后销毁图像资源。提供了一些关键函数的使用示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<?php
====================
PHP--GD库
====================


一、支持:需要php支持gd库


二、绘画步骤:
1. 创建一个画布(画板)、画笔、色彩。
2. *开始绘画
3. 输出图像(复制型)
4. 销毁图像资源(释放内存)


示例:
<?php
//1.创建一个画布,颜色
$im = imagecreate(200,200);
$red = imagecolorallocate($im,255,0,0);  //创建一个颜色:红色
$blue = imagecolorallocate($im,0,0,255);  //创建一个颜色:蓝色 
$c1 = imagecolorallocate($im,200,200,200);


//2.开始绘画
imagefill($im,0,0,$c1); //填充背景
//....


//3.输出图像
header("Content-Type: image/jpeg");//设置响应头信息为一个jpeg的图片
imagejpeg($im);//输出一个jpeg图片
 
//4.销毁
imagedestroy($im); //

三、图片的具体绘制:
3.1 创建一个画布:
imagecreate(宽,高)--创建一个基于256色的画布
imagecreatetruecolor( int x_size, int y_size ) -- 新建一个真彩色图像
//还有基于某个图片的画布
imagecreatefromgif( string filename )
imagecreatefrompng( string filename )
imagecreatefromjpeg( string filename )
3.2 绘画:
//分配定义颜色
$red = imagecolorallocate($im,255,0,0); //分配一个颜色

//填充背景
bool imagefill(resource image,int x,int y, int color ); //填充背景

//画点
bool imagesetpixel(resource image,int x,int y,int color );//画一个像素点

//画一个线段的函数
bool imageline ( resource image, int x1, int y1, int x2, int y2, int color )


//画矩形框(不填充)
bool imagerectangle ( resource image, int x1, int y1, int x2, int y2, int color )
//画矩形框(填充)
bool imagefilledrectangle ( resource image, int x1, int y1, int x2, int y2, int color )


//绘制多边形
bool imagepolygon ( resource image, array points, int num_points, int color )
bool imagefilledpolygon ( resource image, array points, int num_points, int color )

//绘制椭圆(正圆)
imageellipse ( resource image, int cx, int cy, int w, int h, int color )
imagefilledellipse ( resource image, int cx, int cy, int w, int h, int color )


//绘制圆弧(可填充)
imagearc ( resource image, int cx, int cy, int w, int h, int s, int e, int color)
imagefilledarc ( resource image, int cx, int cy, int w, int h, int s, int e, int color, int style )

//绘制字串
bool imagestring ( resource image, int font, int x, int y, string s, int col )
bool imagestringup ( resource image, int font, int x, int y, string s, int col )

//绘制文本:
*array imagettftext ( resource image, float size, float angle, int x, int y, int color, string fontfile, string text )
当上面的字体出现乱码时,使用下面的函数转换编码
string iconv ( string in_charset, string out_charset, string str )

$name="张三";
$str = iconv("ISO8859-1","UTF-8",$name);
$str = iconv("GBK","UTF-8",$name);
$str = iconv("GB2312","UTF-8",$name);


//图片的裁剪、合成、缩放
**bool imagecopyresampled ( resource dst_image, resource src_image, int dst_x, int dst_y, int src_x, int src_y, int dst_w, int dst_h, int src_w, int src_h )

* imagesx — 取得图像宽度
* imagesy — 取得图像高度
* array getimagesize ( string $filename [, array &$imageinfo ] )  取得图像大小





3.3.输出图
header("Content-Type: image/jpeg");//设置响应头信息为一个jpeg的图片
imagejpeg($im);//输出一个jpeg图片
 
header("Content-Type: image/png");//设置响应头信息为一个png的图片
imagepng($im);//输出一个png图片
 
 //输出到指定文件中(另存为)
 imagepng($im,"**.png");


3.4.销毁
imagedestroy($im); //













评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值