PHP图像处理之画图

PHP图像处理

画图

验证码,统计图

安装GD库-----LAMP

安装后在D:\APMServ5.2.6\PHP\ext文件中有php_gd2.dll文件

在php.ini中extension=php_gd2.dll

(1)创建画布---创建资源类型---高度 宽度

resource imagecreate ( int $x_size , int $y_size )新建一个基于调色板的图像

resource imagecreatetruecolor ( int $width , int $height ) 新建一个真彩色图像

(2)绘制图像

制定各种颜色

imagecolorallocate();为一幅图像分配颜色

(使用imagecolorallocate()要使用imagecreate()创建图像)

imagefill()区域填充

矩形,圆,点,线段,扇形,画字(字符,字符串,freeType)

每一个图像对应一个函数

imagefilledrectangle()画一矩形并填充

imagerectangle()画一个矩形

imageline()画一条线段

imagesetpixel()画一个单一像素

imageellipse() 画一个椭圆

    imagefilledellipse() 画一椭圆并填充

<?php
    //1.创建图片资源
    $img=imagecreatetruecolor(200,200);

//    $img=imagecreate(200,200);

    $red=imagecolorallocate($img, 255, 0, 0);
    $yellow=imagecolorallocate($img,255,255,0);
    $green=imagecolorallocate($img,0,255,0);
    $blue=imagecolorallocate($img,0,0,255);
    $white=imagecolorallocate($img,255,255,255);
    //区域填充
    imagefill($img,0,0,$white);
    
    //2.画各种图像
    //画一矩形并填充
    imagefilledrectangle($img, 10, 10, 50, 30, $blue);
    //画一个矩形
    imagerectangle($img,100,100,190,80,$green);

    //画一条线段
    imageline($img,0,0,200,200,$red);
    //画点
    imagesetpixel($img,200,90,$yellow);
    //画一个椭圆
    imageellipse($img,100,100,100,100,$green);
    //画一个椭圆并填充
    imagefilledellipse($img,100,100,10,10,$red);
    //3.输出或保存图像
    header("Content-Type:image/gif");
    imagegif($img);
    
    //4.释放资源
    imagedestroy($img);

?>

imagefilledarc()画一椭圆弧且填充

<?php
    //1.创建图片资源
    $img=imagecreatetruecolor(200,200);

    $white=imagecolorallocate($img,255,255,255);  //白
    $gray=imagecolorallocate($img,0xc0,0xc0,0xc0); //灰
    $darkgray=imagecolorallocate($img,0x90,0x90,0x90);  //淡灰
    $navy=imagecolorallocate($img,0,0,0x80);    //
    $darknavy=imagecolorallocate($img,0,0,0x50);
    $red=imagecolorallocate($img,255,0,0);     //红
    $darkred=imagecolorallocate($img,0x90,0,0);  //淡红

    //背景设为白色
    imagefill($img, 0, 0, $white);

    //2.制作3D的效果
    for($i=60;$i>50;$i--){
        //imagefilledarc() 画一椭圆弧且填充
        imagefilledarc($img,50,$i,100,50,-160,40,$darknavy,ING_ARC_PIE);
        imagefilledarc($img,50,$i,100,50,40,75,$darkgray,ING_ARC_PIE);
        imagefilledarc($img,50,$i,100,50,75,200,$darkred,ING_ARC_PIE);
    }
    imagefilledarc($img,50,$i,100,50,-160,40,$navy,ING_ARC_PIE);
    imagefilledarc($img,50,$i,100,50,40,75,$gray,ING_ARC_PIE);
    imagefilledarc($img,50,$i,100,50,75,200,$red,ING_ARC_PIE);
    //3.输出或保存图像
    header("Content-Type:image/gif");
    imagegif($img);
    
    //4.释放资源
    imagedestroy($img);

?>

    imagechar() 水平地画一个字符

imagefttext()使用 FreeType 2 字体将文本写入图像

<?php
    //创建图片资源
    $img=imagecreatetruecolor(200,200);

    $white=imagecolorallocate($img,255,255,255);
    $gray=imagecolorallocate($img,0xc0,0xc0,0xc0);
    $darkgray=imagecolorallocate($img,0x90,0x90,0x90);
    $navy=imagecolorallocate($img,0,0,0x80);
    $darknavy=imagecolorallocate($img,0,0,0x50);
    $red=imagecolorallocate($img,255,0,0);
    $darkred=imagecolorallocate($img,0x90,0,0);

    //背景设为白色
    imagefill($img, 0, 0, $gray);
    //水平画字符
    imagechar($img,5,100,100,"A",$red);
    imagechar($img,5,120,120,"B",$red);
    //垂直画字符
    imagecharup($img,5,60,60,"C",$red);
    imagecharup($img,5,80,80,"D",$red);
    //画字符串
    imagestring($img, 3, 10, 10, "hello", $navy);
    imagestringup($img,3,150,150,"hello",$navy);
    
    //imagefttext()使用 FreeType 2 字体将文本写入图像
    imagettftext($img, 25, 60, 160, 160, $red, "simkai.ttf", "你好");
    //输出或保存图像
    header("Content-Type:image/gif");
    imagegif($img);
    
    //释放资源
    imagedestroy($img);

?>

(3)输出图像/保存处理好的图像

输出各种类型(gif,png,jpeg)

imagegif();

imagejpeg();

imagepng();

(4)释放资源

imagedestroy();