PHP图像基础
GD库
GD库是PHP处理图像的扩展库,他提供了一系列用来处理图像的函数,可以用来实现验证码、缩略图和图像水印等功能
在PHP中想要使用GD库,需要在PHP配置文件到"php.ini=extension=php_gd2.dll"配置项,去掉前面的分号注释,然后保存文件并重启Apache,通过phpinfo()函数可以查看GD库是否开启成功
phpinfo();
常见图片格式
JPEG、GIF、PNG、WBMP、webP
通过输出gd_info()可以获得一个用来描述GD库信息的关联数组
var_dump(gd_info());
图像的常见操作
创建画布
//创建空白画布,仅支持256色
imagecreate($width, $height)
//创建一个真彩色的画布,支持的色彩丰富,但不支持GIF格式
imagecreatetruecolor($width, $height)
//从给定的文件路径,创建指定格式的图像
imagecreatefromgif($filename)
imagecreatefrompng($filename)
imagecreatefromjpeg($filename)
返回值:返回图像资源标识符
颜色处理
/*
*$image:是由画布创建函数返回的图像资源标识符
*$red, $green, $blue:rgb三原色,其值是0~255的整数或16进制的数
*/
imagecolorallocate($image, $red, $green, $blue)
在使用imagecolorallocate函数为画布分配颜色时,对于使用 imagecreate函数创建的画布,第一次调用imagecolorallocate函数为画布添加背景颜色
对于使用 imagecreatetruecolor创建的画布还需要使用imagefill函数为画布添加背景色
imagecolorallocatealpha($image, $red, $green, $blue, $alpha)
与之前的函数相似,只是可以设置图像的透明度,0表示完全透明,127表示完全不透明
输出图像
输出不同格式的图像
$image:是由画布创建函数返回的图像资源标识符
执行成功返回true,失败返回false
此外,在调用输出图像的相关函数前,需要使用header函数发送HTTP响应头给浏览器,告知输出内容的MIME类型,查看
imagejpeg($image)
imagegif($image)
imagepng($image)
imagewbmp($image)
绘制基本形状
绘制基本图像
//在$image所代表的画布上绘制图像
//绘制点
imagesetpixel($image, $x, $y, $color)
//绘制线条
imageline($image, $x1, $y1, $x2, $y2, $color)
//绘制矩形
imagerectangle($image, $x1, $y1, $x2, $y2, $color)
//绘制多边形,$points包含多边形各点的数组,$num_points多边形点的个数
imagepolygon($image, $points, $num_points, $color)
//绘制$cx, $cy为中心的椭圆弧;$width, $height为圆弧的宽度和高度;
//$start, $end圆弧的起点与终点的角度;0°位于三点钟位置,以顺时针绘画
imagearc($image, $cx, $cy, $width, $height, $start, $end, $color)
//绘制$cx, $cy为中心的为中心的椭圆
//$width, $height为椭圆的宽度和高度,若相等则为正圆
imageellipse($image, $cx, $cy, $width, $height, $color)
绘制并填充颜色
//在$imag图像的($x, $y)出执行区域填充,$x, $y为填充的起始坐标
imagefill($image, $x, $y, $color)
//画一个矩形并填充;坐标点分别为左上角与右下角
imagefilledrectangle($image, $x1, $y1, $x2, $y2, $color)
//画一个椭圆弧并填充
imagefilledarc($image, $cx, $cy, $width, $height, $start, $end, $color, $style)
//画一个椭圆并填充
imagefilledellipse($image, $cx, $cy, $width, $height, $color)
//画一个多边形并填充
imagefilledpolygon($image, $points, $num_points, $color)
绘制文本
/*
* $image:创建的画布标识
* $size:文字的大小
* $angle:文字倾斜的角度
* $x, $y:绘制位置的x,y坐标
* $color:文字颜色
* $fontfile:文字字体文件(即.ttf字体文件的保存路径)
* $text:文字内容
*/
imagettftext($image, $size, $angle, $x, $y, $color, $fontfile, $text)
其它函数
//$font:字体,值越大,字体越大
imagechar($image, $font, $x, $y, $c, $color) //将字符串$c的第一个字符绘制在画布中
imagecharup($image, $font, $x, $y, $c, $color) //将字符串$c的第一个字符垂直绘制在画布中
imagestring($image, $font, $x, $y, $string, $color) //将字符串$s绘制在画布中
imagestringup($image, $font, $x, $y, $string, $color) //将字符串$s垂直绘制在画布中
图像的复制与处理
常用复制
//将$src_im图像中坐标($src_x, $src_y)开始,宽度和高度为 $src_w, $src_h的一部分复制到$dst_im中坐标为 ($dst_x, $dst_y)的位置上
imagecopy($dst_im, $src_im, $dst_x, $dst_y, $src_x, $src_y, $src_w, $src_h)
//功能与imagecopy相同,$pct决定合并程度,范围0~100,0不显示,100与imagecopy效果相同
imagecopymerge($dst_im, $src_im, $dst_x, $dst_y, $src_x, $src_y, $src_w, $src_h, $pct)
//将一部分图片复制到另一图片指定位置,指定大小的地方,若两者大小不同,则进行相应的收缩和拉伸
imagecopyresized($dst_image, $src_image, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h)
获取图像信息
getimagesize($filename)
用于获取图像的信息,该函数的返回值是一个数组,数组的前两个元素是图像的宽高值