GD库提供了一系列用来处理图片的API,使用GD库可以处理图片,或者生成图片。
在网站上GD库通常用来生成缩略图或者用来对图片加水印或者对网站数据生成报表。
通过php.ini 启用GD库
;extension=php_gd2.dll
取消最前边的;号即可启动
GD库中简单的图片操作函数
1、 imagecreatetruecolor 新建一个真彩色图像
imagecreatetruecolor ( int x_size, int y_size ) // x就是宽 ,y就是高
2、imagecolorallocate 为一幅图像分配颜色
imagecolorallocate ( resource image, int red, int green, int blue )
第一次调用为图像设置背景色,第二次获得颜色编码
3、imagestring 绘图函数
imagestring ( resource image, font, int x, int y, 内容 , 颜色 )
font为1-6
//生成四位随机数
for ($i = 0; $i < 4; $i++) {
$rand .= dechex(rand(1, 15));
}
//生成随机字体
$randfont = rand(1, 6);
//生成随机x坐标
$randx = rand(2, 70);
//生成随机y坐标
$randy = rand(2, 15);
//创建100x30大小的图片
$im = imagecreatetruecolor(100, 30);
//设置图片背景颜色为RGB 0,0,0
$bg = imagecolorallocate($im, 0, 0, 0);
//获得RGB 255,255,255 的颜色编码
$color = imagecolorallocate($im, 255, 255, 255);
//在图片中写入随机数
imagestring($im, $randfont, $randx, $randy, $rand, $color);
//设置内容格式为images/jpeg
header("Content-Type: image/jpeg");
//输出jpeg图片
imagejpeg($im);