两大功能:自己画,对于原有图片改变

PHP中GD库的使用

PHP 不仅限于只产生 HTML 的输出,还可以创建及操作多种不同格式的图像文件。PHP提供了一些内置的图像信息函数,也可以使用GD函数库创建新图像或处理已有的图像。目前GD2库支持GIF、JPEG、PNG和WBMP等格式。此外还支持一些FreeType(加载操作系统中的字体)、Type1等字体库。三种都有用的

JPEG 是一种压缩标准的名字,通常是用来存储照片或者存储具有丰富色彩和色彩层次的图像。这种格式使用了有损压缩。

PNG 是可移植的网络图像,对图像采用了无损压缩标准。

GIF 原义是“图像互换格式”,是一种基于LZW算法的连续色调的无损压缩格式 。

此外需要自行安装GD库(wamp自带,linux装好麻烦),还有一种画图的库叫做ImageMagick,可以用于大网站,功能比GD库更全,运用难度差不多,14年的时候用的不多,不知道现在咋样==

这里面的函数会用就好,不用都背

使用GD库画图

步骤:

在PHP中创建一个图像应该完成如下所示的4个步骤:

1.创建一个背景图像(也叫画布),以后的操作都基于此背景图像。

2.在背景上绘制图像轮廓或输入文本。

3.输出最终图形

4.释放资源

画布管理

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 所代表的图像中的颜色。

生成图片

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]] )

$img=imagecreatetruecolor(200,200);

$white=imagecolorallocate($img,0xFF,0xFF,0xFF);

$red=imagecolorallocate($img,255,0,0);

$blue=imagecolorallocate($img,255,255,255);

imagefill($img,0,0,$white);

imageline($img,0,0,200,200,$blue);

imageline($img,200,0,0,200,$red);

header("Content-Type:images/png");

imagePng($img);//第二个参数是输出图片到文件夹 P大写!!

imagedestroy($img);

效果

使用GD库绘制各种常见图形

弱弱的吐槽一下函数名,和c#有什么区别嘛~

imagefill -- 区域填充

语法:bool imagefill(resource image,int x,int y, int color)

imagefill() 在 image 图像的坐标 x,y(图像左上角为 0, 0)处用 color 颜色执行区域填充(即与 x, y 点颜色相同且相邻的点都会被填充)。

imagesetpixel -- 画一个单一像素

语法:bool imagesetpixel ( resource image, int x, int y, int color )

imagesetpixel() 在 image 图像中用 color 颜色在 x,y 坐标(图像左上角为 0,0)上画一个点。

imageline -- 画一条线段

语法:bool imageline ( resource image, int x1, int y1, int x2, int y2, int color )

imageline() 用 color 颜色在图像 image 中从坐标 x1,y1 到 x2,y2(图像左上角为 0, 0)画一条线段。

imagerectangle -- 画一个矩形

语法:bool imagerectangle ( resource image, int x1, int y1, int x2, int y2, int col )

imagerectangle() 用 col 颜色在 image 图像中画一个矩形,其左上角坐标为 x1, y1,右下角坐标为 x2, y2。图像的左上角坐标为 0, 0。

imagefilledrectangle -- 画一矩形并填充

语法:bool imagefilledrectangle ( resource image, int x1, int y1, int x2, int y2, int color )

imagefilledrectangle() 在 image 图像中画一个用 color 颜色填充了的矩形,其左上角坐标为 x1,y1,右下角坐标为 x2,y2。0, 0 是图像的最左上角。

imageellipse -- 画一个椭圆

语法:bool imageellipse ( resource image, int cx, int cy, int w, int h, int color )

imageellipse() 在 image 所代表的图像中画一个中心为 cx,cy(图像左上角为 0, 0)的椭圆。w 和 h 分别指定了椭圆的宽度和高度,椭圆的颜色由 color 指定。

imagefilledellipse -- 画一椭圆并填充

语法:bool imagefilledellipse ( resource image, int cx, int cy, int w, int h, int color )

imagefilledellipse() 在 image 所代表的图像中以 cx,cy(图像左上角为 0, 0)为中心画一个椭圆。w 和 h 分别指定了椭圆的宽和高。椭圆用 color 颜色填充。如果成功则返回 TRUE,失败则返回 FALSE。

imagearc -- 画椭圆弧

bool imagearc ( resource image, int cx, int cy, int w, int h, int s, int e, int color )

imagearc() 以 cx,cy(图像左上角为 0, 0)为中心在 image 所代表的图像中画一个椭圆弧。w 和 h 分别指定了椭圆的宽度和高度,起始和结束点以 s 和 e 参数以角度指定。0°位于三点钟位置,以顺时针方向绘画。

imagefilledarc -- 画一椭圆弧且填充

bool imagefilledarc ( resource image, int cx, int cy, int w, int h, int s, int e, int color, int style )

imagefilledarc() 在 image 所代表的图像中以 cx,cy(图像左上角为 0, 0)画一椭圆弧。如果成功则返回 TRUE,失败则返回 FALSE。w 和 h 分别指定了椭圆的宽和高,s 和 e 参数以角度指定了起始和结束点。style 可以是下列值按位或(OR)后的值:

IMG_ARC_PIE        IMG_ARC_CHORD

IMG_ARC_NOFILL        IMG_ARC_EDGED

imagestring -- 水平地画一行字符串

语法:bool imagestring ( resource image, int font, int x, int y, string s, int col )

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

imagestringup -- 垂直地画一行字符串

语法:bool imagestringup ( resource image, int font, int x, int y, string s, int col )

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

imagechar -- 水平地画一个字符 (用来做验证码)

语法:bool imagechar ( resource image, int font, int x, int y, string c, int color )

imagechar() 将字符串 c 的第一个字符画在 image 指定的图像中,其左上角位于 x,y(图像左上角为 0, 0),颜色为 color。如果 font 是 1,2,3,4 或 5,则使用内置的字体(更大的数字对应于更大的字体)。

imagecharup -- 垂直地画一个字符

语法:bool imagecharup ( resource image, int font, int x, int y, string c, int color )

imagecharup() 将字符 c 垂直地画在 image 指定的图像上,位于 x,y(图像左上角为 0, 0),颜色为 color。如果 font 为 1,2,3,4 或 5,则使用内置的字体。

imagettftext -- 用 TrueType 字体向图像写入文本

语法 :array imagettftext ( resource image, float size, float angle, int x, int y, int color, string fontfile, string text )

$img=imagecreatetruecolor(200,200);

$white=imagecolorallocate($img,0xFF,0xFF,0xFF);

$red=imagecolorallocate($img,255,0,0);

$blue=imagecolorallocate($img,0,0,0xFF);

$pink=imagecolorallocate($img,0xFF,0,0xFF);

$green=imagecolorallocate($img,0,0xFF,0);

imagefill($img,0,0,$white);

imageline($img,0,0,200,200,$blue);

imageline($img,200,0,0,200,$red);

imagerectangle($img,50,50,150,150,$blue);

imagefilledrectangle($img,75,75,125,125,$pink);

imageellipse($img,50,50,150,150,$red);

imagefilledellipse($img,50,50,150,150,$pink);

imagearc($img,150,50,100,100,-90,0,$red);

imagestring($img,5,100,150,"hello world",$red);

imagestringup($img,5,150,100,"hello world",$red);

imagettftext($img,20,0,20,100,$green,"./simkai.ttf","MissZhou加油啊");

//字体的名字必须是英文的,我用汉仪雪君体就不成功==

header("Content-Type:images/png");

imagePng($img);//第二个参数是输出图片到文件夹 P大写!!

imagedestroy($img);

综合案例:画时钟

setInterval(function(){

document.getElementById("time").src="test3.php?"+Math.random();

},1000);

//获取系统时间

date_default_timezone_set("PRC");

$h = date("H");

$i = date("i");

$s = date("s");

//1 创建资源(画布的大小)

$img = imagecreatetruecolor(200, 250);

//设置画布的颜色

$white = imagecolorallocate($img, 0xFF, 0xFF, 0xFF);

$red = imagecolorallocate($img, 255, 0, 0);

$blue = imagecolorallocate($img, 0, 0, 0XFF);

$pink = imagecolorallocate($img, 0XFF, 0, 0XFF);

$green = imagecolorallocate($img, 0, 0xFF, 0);

imagefill($img, 0, 0, $white);

//2. 制作各种颜色

imageellipse($img, 100, 100, 190, 190, $blue);

imagefilledellipse($img, 100, 100, 4, 4, $blue);

imagestring($img, 3, 95, 8, "12", $blue);

imagestring($img, 3,180, 95, "03", $blue);

imagestring($img, 3, 95, 180, "06", $blue);

imagestring($img, 3, 11, 95, "09", $blue);

//秒针

$len = 80;

$a = $len*sin(pi()/30*$s);

$b = $len*cos(pi()/30*$s);

$x = 100 + $a;

$y = 100 - $b;

imageline($img, 100, 100, $x, $y, $red);

//数字的时间

imagestring($img, 5, 20, 230, "NOW: {$h}:{$i}:{$s}", $red);

//4保存,或输出给浏览, 写第二个参数就是保存

header("Content-Type:images/gif");

imagegif($img);

//5. 释放资源

imagedestroy($img);

验证码类~:

php中gd库的使用,php GD库的使用相关推荐

  1. gd mysql错误_php编译gd出错!(已解决)

    php编译gd出错!(已解决) 在32位ubuntu9.04上编译php5.2.12,到gd时make出错: ext/gd/libgd/.libs/gd_png.o: In function `php ...

  2. bpexpdate – 更改映像目录库中备份的截止日期以及介质目录库中介质的截止日期nbu...

    1.根据bpdbjobs查找backupid bpdbjobs -jobid xxx -all_columns|grep backupid 2.查看数据保留时间 [root@backup]# bpim ...

  3. python调用c++动态库 linux_linux中使用boost.python调用c++动态库的方法

    前言 最近开始使用 robot framework 测试c++的动态库,robot framework 是跑在 windows 上面,c++动态库是跑在远程linux主机上面.测试办法是让 robot ...

  4. 解决 Android 中出现依赖多个版本支持库的问题

    解决 Android 中出现依赖多个版本支持库的问题 参考文章: (1)解决 Android 中出现依赖多个版本支持库的问题 (2)https://www.cnblogs.com/lshare/p/1 ...

  5. android项目中导入opencv库,将第二个JNI库包含到我的Android项目(OpenCV)后...

    我试图将OpenCV添加到我现有的Android项目中,但是在合并它们时遇到了以下错误: 12-08 16:15:21.951 22052-22052/ai.inbi.face_recognition ...

  6. 【C 语言】动态库封装与设计 ( 动态库调用环境搭建 | 创建应用 | 拷贝动态库相关文件到源码路径 | 导入头文件 | 配置动态库引用 | 调用动态库中的函数 )

    文章目录 一.在 Visual Studio 2019 中创建 " 控制台应用 " 程序 二.拷贝 xxx.lib.xxx.dll.xxx.h 到源码路径 三.导入 xxx.h 头 ...

  7. 【Android 逆向】Android 进程注入工具开发 ( 注入代码分析 | 获取 远程 目标进程 中的 /system/lib/libc.so 动态库中的 mmap 函数地址 )

    文章目录 一.获取 远程 目标进程 中的 /system/lib/libc.so 动态库中的 mmap 函数地址 二.从 /proc/pid/maps 文件中获取 指定 进程 中的 /system/l ...

  8. 面试官:Mysql 中主库跑太快,从库追不上怎么整?

    作者|莱乌 写这篇文章是因为之前有一次删库操作,需要进行批量删除数据,当时没有控制好删除速度,导致产生了主从延迟,出现了一点小事故. 今天我们就来看看为什么会产生主从延迟以及主从延迟如何处理等相关问题 ...

  9. 转:在 C# 中使用 P/Invoke 调用 Mupdf 函数库显示 PDF 文档

    在 C# 中使用 P/Invoke 调用 Mupdf 函数库显示 PDF 文档 一直以来,我都想为 PDF 补丁丁添加一个 PDF 渲染引擎.可是,目前并没有可以在 .NET 框架上运行的免费 PDF ...

最新文章

  1. SAP MM 采购信息记录里的Automatic Sourcing
  2. Selenium3自动化测试——1. 新建第一个Selenium自动化测试脚本
  3. 基于发电厂知识问答库的检索式问答系统(python有代码)
  4. 共享数据库、独立 Schema
  5. 是什么故障码_大众途观报P2187、P0101故障,差点就换发动机了
  6. 俄罗斯被指为 SolarWinds 供应链事件元凶,技术公司受制裁,常用5大漏洞遭曝光...
  7. adroid intent使用
  8. sort colors 三色排序
  9. 分支程序设计03 - 零基础入门学习C语言12
  10. cuda-gdb 调试python中的module/cu文件
  11. 那智机器人调试步骤总结
  12. android WPS中设置目录标题和目录引用
  13. 大数据--论文读后感
  14. firefox(火狐浏览器)插件的应用
  15. linux 卸载nexus,Linux下安装maven和nexus
  16. Kotlin-面向对象
  17. Vue3 - props
  18. t3系统总显示得不到服务器,用友T3不能连接服务器你好,用友T3总是说连接不到服务...
  19. Java开发遇到的Bug
  20. 突破微信小程序模板消息限制,实现无限制主动推送

热门文章

  1. 链上信息推送服务EPNS,让你第一时间知道自己被爆仓……
  2. dllhost.exe进程消耗内存解决方案
  3. 导出GMS计算结果,并进行分类汇总
  4. 智慧成铁显示无法连接服务器是怎么回事,智慧成铁是什么软件?智慧成铁职工app功能详解[图]...
  5. w8计算机主程序在哪里,Win8怎么打开运行窗口_Win8运行在哪里打开?-192路由网
  6. Windows电脑网线直连iperf测试
  7. 新加坡科技巨头Sea亏损小于预期,外资“清算”阿里只为加大赌注
  8. XStream转换器: 处理xml节点中既有属性又有值
  9. 机器人上单神装_lol机器人上单出装肿么出,先出什么,以什么为主不要眼石
  10. 通过地址获取经纬度和通过经纬度获取地址