• php gd库使用


    gd常见函数

    imagecreatetruecolor() 返回一个图像标识符,代表了一幅大小为 x_sizey_size 的黑色图像。  

    成功后返回图象资源,失败后返回 FALSE

     

    int imagecolorallocate ( resource $image , int $red , int $green , int $blue )
     
    第一次对 imagecolorallocate() 的调用会给基于调色板的图像填充背景色,即用 imagecreate() 建立的图像。
     
    bool imagefill ( resource $image , int $x , int $y , int $color )
    imagefill()image 图像的坐标 xy(图像左上角为 0, 0)处用 color 颜色执行区域填充(即与 x, y 点颜色相同且相邻的点都会被填充)
     
    imagefilledrectangle ( resource $image , int $x1 , int $y1 , int $x2 , int $y2 , int $color

    image 图像中画一个用 color 颜色填充了的矩形,其左上角坐标为 x1y1,右下角坐标为 x2y2。0, 0 是图像的最左上角。 

     

    imageellipse ( resource $image , int $cx , int $cy , int $width , int $height , int $color )

    在指定的坐标上画一个椭圆。 

     

    imagefilledellipse ( resource $image , int $cx , int $cy , int $width , int $height , int $color )

    画一椭圆并填充到指定的 image

     

    bool imagepng ( resource $image [, string $filename ] )

    imagepng() 将 GD 图像流(image)以 PNG 格式输出到标准输出(通常为浏览器),

    如果用 filename 给出了文件名则将其输出到该文件。

    <?php
    $im = imagecreatefrompng("test.png");
    imagepng($im);
    ?>
     
    imagedestroy() 释放与 image 关联的内存。
     
    array getimagesize ( string $filename [, array &$imageinfo ] ) 取得图片大小
     
    imagefilledarc ( resource $image , int $cx , int $cy , int $width , int $height , int $start , int $end , int $color , int $style )
     
    bool imagestring ( resource $image , int $font , int $x , int $y , string $s , int $col )

    imagestring()col 颜色将字符串 s 画到 image 所代表的图像的 xy 坐标处(这是字符串左上角坐标,整幅图像的左上角为 0,0)。如果 font 是 1,2,3,4 或 5,则使用内置字体。 

    imageline ( resource $image , int $x1 , int $y1 , int $x2 , int $y2 , int $color )

    imageline()color 颜色在图像 image 中从坐标 x1y1x2y2(图像左上角为 0, 0)画一条线段。  

    在指定的 image 上画一椭圆弧且填充。

     

    <?php
    header ('Content-Type: image/png');
    $im = @imagecreatetruecolor(120, 20)
          or die('Cannot Initialize new GD image stream');
    $text_color = imagecolorallocate($im, 233, 14, 91);
    imagestring($im, 1, 5, 5,  'A Simple Text String', $text_color);
    imagepng($im);
    imagedestroy($im);
    ?>
     
     
     
    实际应用:
    绘制一个饼状图:
     
    <?php
    header("Content-type:image/png");
    //创建画布
    $img = imagecreatetruecolor(800, 600);
    //添加颜色
    $blue = imagecolorallocate($img,0, 0, 255);
    imagefill($img, 0, 0, $blue); //填充颜色 // /imagefill($img, 0, 0, $blue); $red = imagecolorallocate($img,255, 0, 0); //添加filled填充效果 $green = imagecolorallocate($img,0, 255, 0); //画圆弧 //imagearc($img, 400, 300, 300, 300, 270, 0, $green); //画圆弧并且填充颜色 /* 1IMG_ARC_PIE 2.IMG_ARC_CHORD 3.IMG_ARC_NOFILL 4.IMG_ARC_EDGED IMG_ARC_PIE 和 IMG_ARC_CHORD 是互斥的;IMG_ARC_CHORD 只是用直线连接了起始和结束点,IMG_ARC_PIE 则产生圆形边界。IMG_ARC_NOFILL 指明弧或弦只有轮廓,不填充。IMG_ARC_EDGED 指明用直线将起始和结束点与中心点相连,和 I MG_ARC_NOFILL 一起使用是画饼状图轮廓的好方法(而不用填充)。 */ //imagefill 根据所在位置进行填充 如果是在圆形内部就填充圆形 否则填充外部 除开圆形 //600 300 $gray = imagecolorallocate($img, 0xC0, 0xC0, 0xC0); $darkgray = imagecolorallocate($img, 0x90, 0x90, 0x90); $navy = imagecolorallocate($img, 0x00, 0x00, 0x80); $darknavy = imagecolorallocate($img, 0x00, 0x00, 0x50); $red = imagecolorallocate($img, 0xFF, 0x00, 0x00); $darkred = imagecolorallocate($img, 0x90, 0x00, 0x00); for($i = 300;$i>250;$i--){
    //绘制椭圆形 资源、x轴 y轴 长半轴 短半轴 颜色 imagefilledarc(
    $img, 400, $i, 400, 350, 0, 45, $darkgray,0); imagefilledarc($img, 400, $i, 400, 350, 45, 270, $darkred,0); imagefilledarc($img, 400, $i, 400, 350, -90, 0, $darknavy,0); }
    //这里先用比较昏暗的颜色绘制 这样可以有阴影效果 imagefilledarc(
    $img, 400, 250, 400, 350, 0, 45, $gray,0); imagefilledarc($img, 400, 250, 400, 350, 45, 270, $red,0); imagefilledarc($img, 400, 250, 400, 350, -90, 0, $navy,0);
    //明亮颜色绘制 imagepng(
    $img);
    //以png格式输出图片到浏览器上 imagedestroy(
    $img);
    //销毁图片资源

    效果:

    验证码:

    <?php
    $img = imagecreatetruecolor(200, 50);
    $str = "abcdefg";
    $f = imagecolorallocate($img, 0, 0, 0);
    imagefill($img, 0, 0, $f);
    $s = str_shuffle($str);//搅乱成无序的数组
    for($i=0;$i<4;$i++){
            $c = substr($str, rand(0,strlen($s)),1);
            $color = imagecolorallocate($img, 50, 50, 50);
            imagestring($img, 6, 10+$i*40, rand(10,30), $c, $color);
            imageline($img, rand(0,30), rand(0,50), rand(150,200), rand(0,50), $color);
    }
    $color = imagecolorallocate($img, 0, 0, 255);
    
    header("Content-type:image/png;charset=utf8");
    imagepng($img);
    imagedestroy($img);
    

    效果图:

  • 相关阅读:
    Effective C++ 条款7 关于基类的virtual析构和nonvirtual析构
    关于git 子模块
    存储类别关键字
    rm 配合正则表达式使用
    左值引用,右值引用,通用引用
    Effective C++ 条款4 使用前先初始化
    Selfgrowth
    学习随记贪心
    关于JS中Object对象的key及key的排序
    Troubles and Obstacles
  • 原文地址:https://www.cnblogs.com/webcyh/p/11300657.html
Copyright © 2020-2023  润新知