1.开启GD库扩展
去掉注释:
extension=php_gd2.dll
extension_dir='ext目录所在位置'
2.检测GD库是否开启
phpinfo();
//检测扩展是够开启
extension_loaded();
//检测是否有gd库中的方法
function_exists();
//获取所有已经定义的函数,查看是否有gd库中的函数
get_defined_functions();
3.GD库操作流程
(1)创建画布
(2)创建颜色
(3)开始绘画
(4)输出或保存图像
注意:php文件的BOM头一定要去除。当然也不能有输出。
(5)销毁资源
例子:
1 //1.创建画布 2 3 // imagecreatetruecolor(width, height)创建画布,返回一个图像标识符 4 $width = 100; 5 $height = 50; 6 $image = imagecreatetruecolor($width, $height); 7 //2.创建颜色 8 // imagecolorallocate(image, red, green, blue)创建颜色 9 $red = imagecolorallocate($imange, 255, 0, 0); 10 $white = imagecolorallocate($image, 255, 255, 255) 11 //3.开始绘画 12 // imagechar(image, font, x, y, c, color)水平绘制一个字符 13 imagechar($image, 5, 50, 50, 'Y', $red) 14 // imagecharup(image, font, x, y, c, color)垂直绘画一个字符 15 imagecharup($image, 5, 20, 70, c, $white) 16 // imagestring(image, font, x, y, string, color)水平绘画一个字符串 17 imagestring($image, 5, 80, 20, 'ykw', $white) 18 // imagestringup(image, font, x, y, string, color)垂直绘画一个字符串 19 //4.告诉浏览器以图片形式来显示 20 header('content-type:image/jpeg');//image/gif image/png 21 //5.imagejpeg($image)输出图像 22 imagejpeg($image); 23 //6.销毁资源 24 imagedestroy($image);
4.GD库填充画布颜色,设置系统字体
/** *填充画布颜色 *选择系统字体 **/ //创建画布 $image = imagecreatetruecolor(500, 500); //创建颜色 $red = imagecolorallocate($image, 255, 0, 0); $white = imagecolorallocate($image, 255, 255, 255); $randColor = imagecolorallocate($image, mt_rand(0,255),mt_rand(0,255) ,mt_rand(0,255)); //绘制填充矩形 // imagefilledrectangle(image, x1, y1, x2, y2, color) imagefilledrectangle($image, 0, 0, 500, 500, $white); //绘画 //windows系统找到字体文件 运行->fonts // 设置系统字体 imagettftext(image, size, angle, x, y, color, fontfile, text) imagettftext($image, 20, 0, 100, 100, $randColor, 'fonts/msyhbd.ttf', 'you are a sb'); //告诉浏览器以图像显示 header('content-type:image/png'); //输出图像 imagepng($image); //保存图像 imagepng($image,'images/1.png'); //销毁资源 imagedestroy($image);