GD库(一)

PHP动态图像处理
  一、支持:需要php支持gd库
    phpinfo()
  二、绘画步骤:
    0.一定要添加header()函数送content-type通知浏览器这次发送的是图片而不是文本
    1. 创建一个画布(画板)、画笔、色彩。
    2. *开始绘画
    3. 输出图像(复制型)
    4. 销毁图像资源(释放内存)

代码:

1 header("Content-Type:image/png");

    1.创建一个画布,颜色

1 $im = imagecreate(200,200);
2 $red = imagecolorallocate($im,255,0,0); //创建一个颜色:红色
3 $blue = imagecolorallocate($im,0,0,255); //创建一个颜色:蓝色 
4 $c1 = imagecolorallocate($im,200,200,200);

    2.开始绘画

1 imagefill($im,0,0,$c1); //填充背景
2 //....

    3.输出图像

1 header("Content-Type: image/jpeg");//设置响应头信息为一个jpeg的图片
2 imagejpeg($im);//输出一个jpeg图片

    4.销毁

1 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); //
画一个验证码:

 1 <?php
 2 
 3 $type=1;
 4 $len=4;
 5 
 6 header("content-type:image/png");
 7 //测试
 8 newCode($type,$len);
 9 
10 /**
11 * 验证码函数    打印一个图片
12 * @param    int    $type    验证码类型1:纯数字;2:数字+小写;3:数字+小写+大写 
13 * @param    int    $len    验证码长度
14 */
15 function newCode($type=1,$len=4){
16 
17 $str="0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
18 
19 if($type==1){
20 $m=9;
21 }elseif($type==2){
22 $m=35;
23 }else{
24 $m=61;
25 }
26 $code="";
27 for($i=0;$i<$len;$i++){
28 $code .= $str[rand(0,$m)];    //获取验证码内容
29 }
30 
31 
32 $im=imagecreatetruecolor(50*$len,50);    //画布
33 
34 $bg=imagecolorallocate($im,230,230,230);    //背景
35 $hb=imagecolorallocate($im,rand(0,255),rand(0,255),rand(0,255));    //画笔
36 
37 imagefill($im,0,0,$bg);    //背景填充
38 
39 //添加噪点
40 for($i=0;$i<50*$len;$i++){
41 $hb=imagecolorallocate($im,rand(0,255),rand(0,255),rand(0,255));
42 imagesetpixel($im,rand(0,50*$len),rand(0,50),$hb);
43 }
44 
45 //添加干扰线
46 for($i=0;$i<$len;$i++){
47 $hb=imagecolorallocate($im,rand(0,255),rand(0,255),rand(0,255));
48 imageline($im,rand(0,50*$len),rand(0,50),rand(0,50*$len),rand(0,50),$hb);
49 }
50 
51 //画入验证码
52 for($i=0;$i<$len;$i++){
53 $hb=imagecolorallocate($im,rand(0,255),rand(0,255),rand(0,255));
54 imagettftext($im,40,rand(-30,30),20+40*$i,44,$hb,"msyh.ttf",$code[$i]);
55 }
56 
57 //输出图像
58 imagepng($im);
59 //释放资源
60 imagedestroy($im);
61 
62 }
63 
64 ?>
原文地址:https://www.cnblogs.com/yexiang520/p/5548198.html