13.1.2 使用GD库画图

13.1.2 使用GD库画图

GD库图像绘制的步骤

在PHP中创建一个图像应该完成如下所示的4个步骤:
1.创建一个背景图像(也叫画布),以后的操作都基于此背景图像。
2.在背景上绘制图像轮廓或输入文本。
3.输出最终图形
4.释放资源

08.png
<?php       
    //1. 创建画布 
    $im = imageCreateTrueColor(200, 200);           //建立空白背景
    $white = imageColorAllocate ($im, 255, 255, 255);   //设置绘图颜色
    $blue  = imageColorAllocate ($im, 0, 0, 64);
    //2. 开始绘画
    imageFill($im, 0, 0, $blue);                        //绘制背景
    imageLine($im, 0, 0, 200, 200, $white);         //画线
    imageString($im, 4, 50, 150, 'Sales', $white);      //添加字串
    //3. 输出图像
    header('Content-type: image/png');
    imagePng ($im);     //以 PNG 格式将图像输出
    //4. 释放资源
    imageDestroy($im);  

画布管理

imagecreate -- 新建一个基于调色板的图像
resource imagecreate ( int x_size, int y_size )
本函数用来建立空新画布,参数为图片大小,单位为像素 (pixel)。支持256色。
imagecreatetruecolor -- 新建一个真彩色图像
resource imagecreatetruecolor ( int x_size, int y_size )
新建一个真彩色图像画布 ,需要 GD 2.0.1 或更高版本,不能用于 GIF 文件格式。
imagedestroy -- 销毁一图像
bool imagedestroy ( resource image )
imagedestroy() 释放与 image 关联的内存。

设置颜色

imagecolorallocate -- 为一幅图像分配颜色
语法:int imagecolorallocate ( resource image, int red, int green, int blue )
imagecolorallocate() 返回一个标识符,代表了由给定的 RGB 成分组成的颜色。red,green 和 blue 分别是所需要的颜色的红,绿,蓝成分。这些参数是 0 到 255 的整数或者十六进制的 0x00 到 0xFF。imagecolorallocate() 必须被调用以创建每一种用在 image 所代表的图像中的颜色。

$im = imagecreatetruecolor(100, 100);   //创建画布的大小为100x100
$red   = imagecolorallocate($im,255,0,0); //由十进制整数设置一个颜色
$white = imagecolorallocate($im, 0xFF, 0xFF, 0xFF);// 十六进制方式

生成图片

imagegif -- 以 GIF 格式将图像输出到浏览器或文件
语法:bool imagegif (resource image [,string filename] )
imagejpeg -- 以 JPEG 格式将图像输出到浏览器或文件
语法:bool imagejpeg (resource image [,string filename [, int quality]] )
imagepng -- 以 PNG 格式将图像输出到浏览器或文件
语法:bool imagepng (resource image [,string filename] )
imagewbmp -- 以 WBMP 格式将图像输出到浏览器或文件
语法:bool imagewbmp (resource image [, string filename [, int foreground]] )

demo.html

<img src="test.php" />

test.php

<?php
    //1 创建资源(画布的大小)
    $img = imagecreatetruecolor(200, 200);  
    //设置画布的颜色
    $white =  imagecolorallocate($img, 0xFF, 0xFF, 0xFF);
    $red =  imagecolorallocate($img, 255, 0, 0);
    $blue =  imagecolorallocate($img, 0, 0, 0XFF);
    
    
    imagefill($img, 0, 0, $white);
    //2. 制作各种颜色
    
    imageline($img, 0,0, 200,200, $blue);
    imageline($img, 200, 0, 0, 200, $red);

    //3. 画出各种图形,和写(画出)字


    //4保存,或输出给浏览, 写第二个参数就是保存
    header("Content-Type:images/gif");

    imagegif($img);

    //5. 释放资源
    imagedestroy($img);
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • PHP中GD库的使用 GD简介 PHP 不仅限于只产生 HTML 的输出,还可以创建及操作多种不同格式的图像文件。...
    dptms阅读 4,599评论 0 2
  • 本文讲解常用GD函数的应用 1.一个简单的图像 我们先看一个例子: 运行结果: 这段代码中我们用了一下几个函数: ...
    老马的春天阅读 5,426评论 2 8
  • ¥开启¥ 【iAPP实现进入界面执行逐一显】 〖2017-08-25 15:22:14〗 《//首先开一个线程,因...
    小菜c阅读 11,728评论 0 17
  • 背景 验证码就是把一串随机产品的数字动态生成一幅图片,再加上干扰元素。此时用户可以通过肉眼能识别里面的数字或者字符...
    dy2903阅读 6,547评论 0 7
  • 13.1.3 绘制各种图形 imagefill -- 区域填充语法:bool imagefill(resource...
    曹渊说创业阅读 1,748评论 0 0