GD库使用

定义:GD库提供了一系列用来处理图片的API ,使用GD库可以处理图片,或者生成图片。在网站上GD库通常用来生成缩略图或者用来对图片加水印或者对网站数据生成报表。

使用步骤:

只需要在php.ini配置文件中开启GD扩展即可使用image开头的函数。

extension=php_gd2.dll            解开此句注释即可

具体使用:

1:创建最简单的图片
$img=imagecreate(200,100);              //创建图片,默认背景无色
imagecolorallocate($img,255,0,0);       //给图片分配颜色
header('content-type:image/png');   //告知浏览器通过png格式解析图片
imagepng($img);                     //将图片以png格式输出
imagedestroy($img);                 //销毁图片资源

1、  imagegif():将图片资源以gif格式输出
2、  imagejpeg():将图片资源以jpg格式输出
imagepng($img,'路径');          //保存图片
注:保存图片前面不能添加“header('content-type:image/png')”这句代码。
2:画圆弧
bool imagearc(图片资源,原点x,原点y,宽度,高度,起始角度,结束角度,颜色)
$img=imagecreate(200,100);        //创建图片,默认背景无色
imagecolorallocate($img,255,0,0);       //给图片分配颜色
$color=imagecolorallocate($img,255,255,255); //前景色
imagearc($img,100,50,100,100,0,360,$color);   //正圆
header('content-type:image/png');   //告知浏览器通过png格式解析图片
imagepng($img);              //将图片以png格式输出 
3:画直线
bool imageline(图片资源$img,起始点x,起始点y,结束点x,结束点y,颜色)
$img=imagecreate(200,100);        //创建图片,默认背景无色
imagecolorallocate($img,255,0,0);       //给图片分配颜色
$color=imagecolorallocate($img,255,255,255); //前景色
imageline($img,50,20,150,80,$color);   //直线
header('content-type:image/png');   //告知浏览器通过png格式解析图片
imagepng($img);              //将图片以png格式输出 
4:画矩形
bool imagerectangle(图片资源$img,左上点x,左上点y,右下点x,右下点y,颜色)
$img=imagecreate(200,100);        //创建图片,默认背景无色
imagecolorallocate($img,255,0,0);       //给图片分配颜色
$color=imagecolorallocate($img,255,255,255); //前景色
imagerectangle($img,50,30,180,80,$color);   //矩形
header('content-type:image/png');   //告知浏览器通过png格式解析图片
imagepng($img);              //将图片以png格式输出 
5:验证码制作
用到的函数:
imagesx()            // 获取图片宽度
imagesy()            // 获取图片高度
imagefontwidth()     // 获取字体的宽度
imagefontheight()    // 获取字符高度
imagestring($img,字体,起始位置x,起始位置y,字符串,颜色)

验证码(数字字母型)代码:

$array=array_merge(range('a','z'),range('A','Z'),range(0,9)); //拼接多个数组
$array1=array_rand($array,6);  //从数组中随机抽取6个元素,注意,这里取出的是元素对应下标
shuffle($array1);        //打乱数组
$str='';
foreach($array1 as $k){
   $str.=$array[$k];
};
$img=imagecreate(300,200);     //创建画布
imagecolorallocate($img,0,0,200);
$color=imagecolorallocate($img,255,255,255);
$font=5;
$x=(imagesx($img)-imagefontwidth($font)*strlen($str))/2;
$y=(imagesy($img)-imagefontheight($font))/2;
imagestring($img,$font,$x,$y,$str,$color);
header('content-type:image/png');
imagepng($img);

中文验证码代码:

$array=['你','是','猪','狗','牛','羊','鸡','龙','虎'];
$index=array_rand($array,4);
shuffle($index);
$str='';
foreach($index as $k){
    $str.=$index[$k];
};
$img=imagecreate(400,300);
imagecolorallocate($img,30,100,109);
$color=imagecolorallocate($img,255,255,255);
$font=20;
$angle=10;
$fontfile='./ttf/simsun.ttc';
$info=imagettfbbox($font,$angle,$fontfile,$str);
$x=(imagesx($img)-($info[4]-$info[6]))/2;
$y=(imagesy($img)+($info[4]-$info[7]))/2;
imagettftext($img,$size,$angle,$x,$y,$color,$fontfile,$str);
header('content-type:image/jpeg');
imagejpeg($img);

打开图片创建验证码:
只需要将自己创建图片资源img用以下方式获得img。

    imagecreatefromjpeg(图片路径):打开jpg图片创建资源
    imagecreatefrompng(图片路径): 打开png图片创建资源
    imagecreatefromgif(图片路径): 打开gif图片创建资源
6:添加文字水印
7:添加图片水印
//将源图拷贝到目标图上
imagecopy($dst_img,$src_img,$dst_x,$dst_y,0,0,$src_w,$src_h);
//显示
header('content-type:image/png');
imagepng($dst_img); 
8:生成缩略图、裁切图片

知识点

imagecopyresampled():   拷贝图片变调整尺寸
imagecreatetruecolor():   创建真彩色的图像
imagecopyresampled();     重采样拷贝部分图像并调整大小

问题:imagecreate()和imagecreatetruecolor()区别?
      Imagecreate()创建的图片支持256种颜色,没有背景。
      Imagecreatetruecolor()创建图片支持2563种颜色,黑色背景。

代码实现

//制作缩略图
$dst_img=imagecreatetruecolor(300,300);     //创建目标图
$src_img=imagecreatefromjpeg('./images/4.jpg');  //创建源图资源
$src_w=imagesx($src_img);
$src_h=imagesy($src_img);
imagecopyresampled($dst_img,$src_img,0,0,0,0,300,300,$src_w,$src_h);
imagepng($dst_img,'./images/small.png');

//裁切
$dst_img=imagecreatetruecolor(300,300);     //创建目标图
$src_img=imagecreatefromjpeg('./images/4.jpg');  //创建源图资源
imagecopyresampled($dst_img,$src_img,0,0,160,75,300,300,300,300);
imagepng($dst_img,'./images/small02.png');
9:GD库改错思路

如果图片无法显示
第一招:注释掉header()函数再测试(注释掉之后,错误信息就会显示);
第二招:如果注释掉以后没有报错,看图片代码前面是否有其他字符的存在,如有就删除。
第三招:如果看不到字符,检查源码
第四招:如果上面都不管用,在header()前面添加ob_clear();

你可能感兴趣的:(GD库使用)