PHP学习笔记08——GD绘图基础
1 <?php 2 //1. 使用GD库绘制图形 3 /* 使用GD库画图分为四个基础步骤 4 * 创建图像->绘制图像->输出图像->释放资源 5 */ 6 /* 创建图像 7 * imagecreate(int sx, int sy) 基于调色板的图像(256色) 8 * imagecreatetruecolor(int sx, int sy) 基于真彩色图像,不能用于gif图像 9 */ 10 $image = imagecreatetruecolor(800, 800); 11 /* 设置颜色 12 * imagecolorallocate(resource image, int red, int green, int blue) 13 */ 14 $col_white = imagecolorallocate($image, 0xFF, 0xFF, 0xFF); 15 $col_gray = imagecolorallocate($image, 0xC0, 0xC0, 0xC0); 16 $col_darkgray = imagecolorallocate($image, 0x90, 0x90, 0x90); 17 $col_navy = imagecolorallocate($image, 0x00, 0x00, 0x80); 18 $col_darknavy = imagecolorallocate($image, 0x00, 0x00, 0x50); 19 $col_red = imagecolorallocate($image, 0xFF, 0x00, 0x00); 20 $col_darkred = imagecolorallocate($image, 0x90, 0x00, 0x00); 21 22 23 /* 简单绘制图像的函数举例 24 * imagefill($image,$sx,$sy,$color) 区域填充,与(sx,sy)相邻且颜色相同的电都会被填充 25 * imagesetpixel($image,$sx,$sy,$color) 画点 (sx,sy) 26 * imageline($image,$sx,$sy,$ex,$ey,$color) 画线 (sx,sy)->(ex,ey) 27 * imagerectangle($image,$sx,$sy,$ex,$ey,$color) 画空心矩形 28 * imagefilledrectagle($image,$sx,$sy,$ex,$ey,$color) 画填充矩形 29 * imagepolygon($image,$array,$arraynum,$colors) 画多边形 30 * imagefilledpolygon($image,$array,$arraynum,$colors) 画填充多边形 31 * imageellipse($image,$cx,$cy,$w,$h,$color) 画椭圆,(cx,cy)为圆心,w,h分别是宽高 32 * imagefilledellipse($image,$cx,$cy,$w,$h,$color) 画填充椭圆 33 * imagearc($image,$cx,$cy,$w,$h,$s,$e,$color) 画弧,s,e指的是起始和结束度数 34 * imagefilledarc($image,$cx,$cy,$w,$h,$s,$e,$color,$style) 画填充弧,多了一个style参数 35 */ 36 imagefill($image, 0, 0, $col_white); 37 38 imagesetpixel($image, 150, 20, $col_red); 39 imageline($image, 120, 50, 180, 50, $col_red); 40 41 imagerectangle($image, 200, 30, 300, 130, $col_red); 42 imagefilledrectangle($image, 225, 55, 275, 105, $col_red); 43 44 $array_pts = array(350, 30, 400, 40, 430, 70, 370, 100); 45 $array_ptsfilled = array(450, 30, 500, 40, 530, 70, 470, 100); 46 imagepolygon($image, $array_pts, 4, $col_red); 47 imagefilledpolygon($image, $array_ptsfilled, 4, $col_red); 48 49 imageellipse($image, 650, 80, 150, 100, $col_red); 50 imagefilledellipse($image, 650, 80, 75, 50, $col_red); 51 52 imagearc($image, 100, 200, 150, 100, 0, 180, $col_red); 53 imagefilledarc($image, 100, 200, 150, 100, 180, 210, $col_red, IMG_ARC_PIE); //饼状 54 imagefilledarc($image, 100, 200, 150, 100, 215, 245, $col_red, IMG_ARC_CHORD); //扇形对应的三角 55 imagefilledarc($image, 100, 200, 150, 100, 250, 280, $col_red, IMG_ARC_EDGED); 56 imagefilledarc($image, 100, 200, 150, 100, 285, 315, $col_red, IMG_ARC_NOFILL); 57 imagefilledarc($image, 100, 200, 150, 100, 320, 350, $col_red, IMG_ARC_ROUNDED); 58 59 //以下绘制饼形图,画出3D效果 60 for ($i = 60; $i > 50; $i--) { 61 imagefilledarc($image, 50, $i, 100, 50, -160, 40, $col_darknavy, IMG_ARC_PIE); 62 imagefilledarc($image, 50, $i, 100, 50, 40, 75, $col_darkgray, IMG_ARC_PIE); 63 imagefilledarc($image, 50, $i, 100, 50, 75, 200, $col_darkred, IMG_ARC_PIE); 64 } 65 66 imagefilledarc($image, 50, 50, 100, 50, -160, 40, $col_navy, IMG_ARC_PIE); 67 imagefilledarc($image, 50, 50, 100, 50, 40, 75, $col_gray, IMG_ARC_PIE); 68 imagefilledarc($image, 50, 50, 100, 50, 75, 200, $col_red, IMG_ARC_PIE); 69 70 imagestring($image, 1, 15, 55, '34.7%', $col_white); 71 imagestring($image, 1, 45, 35, '55.5%', $col_white); 72 73 /* 绘制文字函数 74 * imagestring($image,$font,$x,$y,$s,$color) 画水平字符串,font取1~5越大尺寸越大 75 * imagestringup($image,$font,$x,$y,$s,$color) 画竖直字符串 76 * imagechar($image,$font,$x,$y,$c,$color) 画水平字符 77 * imagecharup($image,$font,$x,$y,$c,$color) 画竖直字符 78 * 以上函数都只能输出内置字体,使用imagettftext输出与设备无关的truetype字体 79 * imagettftext($image,$size,$angle,$x,$,$color,$fontfile,$text) 80 * size字体大小(GD1是像素,GD2是点数,$angle表示角度,fontfile是字体路径,text是字符串,注意要使用UTF8编码) 81 */ 82 $array_str = "string"; 83 imagestring($image, 4, 250, 150, $array_str, $col_red); 84 imagestringup($image, 4, 250, 250, $array_str, $col_red); 85 86 for ($i = 0; $i < strlen($array_str); $i++) { 87 imagechar($image, 5, 350+$i*10, 150+$i*10, $array_str{$i}, $col_red); 88 imagecharup($image, 5, 350+$i*10, 150+$i*10, $array_str{$i}, $col_red); 89 } 90 91 $text = iconv("GB2312", "UTF-8", "无兄弟,不编程"); 92 imagettftext($image, 20, 0, 450, 200, $col_red, "msyh.ttc", $text); 93 94 95 96 /* 生成图像 97 * 支持GIF,JPEG,PNG,WBMP格式,分别对应四个函数imagepng,imagegif,imagejpeg,imagewbmp 98 * 这四个函数第一个参数都是资源名,参数二是文件名,将图片保存到文件;不提供的话会直接输出到浏览器,但是要设置header类型 99 * imagejpeg第三个参数指定图像质量(0~100),imagewbmp第三个参数指定背景色 100 * 以下程序自动监测GD库支持的图像类型以生成对应格式的图像 101 */ 102 if (function_exists("imagegif")) { 103 header('Content-type: image/gif'); 104 imagegif($image); 105 } elseif (function_exists("imagejpeg")) { 106 header('Content-type: image/jpeg'); 107 imagejpeg($image); 108 } elseif (function_exists("imagepng")) { 109 header('Content-type: image/png'); 110 imagepng($image); 111 } elseif (function_exists("imagewbmp")) { 112 header('Content-type: image/vnd.wap.wbmp'); 113 imagewbmp($image); 114 } 115 /* 销毁资源 116 */ 117 imagedestroy($image); 118 119 120 ?>
执行结果
转载于:https://www.cnblogs.com/swm8023/p/3261853.html
PHP学习笔记08——GD绘图基础相关推荐
- MATLAB学习笔记5:绘图基础与数据可视化(中)
阅读前请注意: 1. 该学习笔记是华中师范大学HelloWorld程序设计协会2021年寒假MATLAB培训的学习记录,是基于培训课堂内容的总结归纳.拓展阅读.博客内容由 @K2SO4钾 撰写.编辑, ...
- MATLAB学习笔记2:MATLAB基础知识(下)
阅读前请注意: 1. 该学习笔记是华中师范大学HelloWorld程序设计协会2021年寒假MATLAB培训的学习记录,是基于培训课堂内容的总结归纳.拓展阅读.博客内容由 @K2SO4钾 撰写.编辑, ...
- 《Go语言圣经》学习笔记 第三章 基础数据类型
<Go语言圣经>学习笔记 第三章 基础数据类型 目录 整型 浮点数 复数 布尔型 字符串 常量 注:学习<Go语言圣经>笔记,PDF点击下载,建议看书. Go语言小白学习笔记, ...
- ESP32 单片机学习笔记 - 08 - WebSocket客户端
前言,终于要到网络模型的最后一层,第四层,应用层,http.websocket的实践了. 文章目录 ESP32 单片机学习笔记 - 08 - WebSocket客户端 一.应用层协议 科普概念 二.编 ...
- J2EE学习笔记三:EJB基础概念和知识 收藏
J2EE学习笔记三:EJB基础概念和知识 收藏 EJB正是J2EE的旗舰技术,因此俺直接跳到这一章来了,前面的几章都是讲Servlet和JSP以及JDBC的,俺都懂一些.那么EJB和通常我们所说的Ja ...
- java学习笔记15--多线程编程基础2
本文地址:http://www.cnblogs.com/archimedes/p/java-study-note15.html,转载请注明源地址. 线程的生命周期 1.线程的生命周期 线程从产生到消亡 ...
- Linux 学习笔记之超详细基础linux命令 Part 3
Linux学习笔记之超详细基础linux命令 by:授客 QQ:1033553122 ---------------------------------接Part 2----------------- ...
- JavaWeb黑马旅游网-学习笔记08【旅游线路详情】
Java后端 学习路线 笔记汇总表[黑马程序员] JavaWeb黑马旅游网-学习笔记01[准备工作] JavaWeb黑马旅游网-学习笔记02[注册功能] JavaWeb黑马旅游网-学习笔记03[登陆和 ...
- XML学习笔记01【xml_基础、xml_约束】
Java后端 学习路线 笔记汇总表[黑马程序员] XML学习笔记01[xml_基础.xml_约束][day01] XML学习笔记02[xml_解析][day01] 目录 01 xml_基础 今日内容 ...
- 36篇博文带你学完opencv :python3+opencv学习笔记汇总目录(基础版)
经过几天的学习,opencv基础部分学习完啦.整理出来. OpenCV opencv学习笔记1:图片读入,显示与保存(有代码) opencv学习笔记2:图像处理基础 opencv学习笔记3:像素处理 ...
最新文章
- 如何在WordPress中自定义PHP页面并操作数据库?
- IIS6.0限制上传文件大小的解决办法
- python中不同类型的数据不能相互运算_PyTorch中Tensor的数据类型和运算的使用
- C++中const——由一个例子想到的
- OpenYurt 深度解读:如何构建 Kubernetes 原生云边高效协同网络?
- Electron学习-创建一个程序
- java自动转换_java类型转换详解(自动转换和强制转换)
- 区别 (function($){...})(jQuery)、$(function(){ })和$.fn
- Building designing UVA - 11039
- 开发了那么多项目,你能自己手写个健壮的链表出来吗?
- 网络七层协议_IT人计算机网络浅析
- Linux主进程退出,主进程创建的线程会退出吗?
- paip.html 及css调试工具---debugbar
- 2020MathorCup数学建模比赛A题D题思路
- sap服务器安装双系统教程,Ghost安装双系统安装使用图文教程
- Android基础--ListView的刷新
- java中的前加加 和 后加加
- Silvaco TCAD仿真学习Lesson1——Atlas仿真
- 简单了解下什么是中台?
- 凤凰卫视:专业、互动、持续的云服务助力凤凰新媒体转型
热门文章
- python线程池的使用
- 帆软关于排名公式、分组排名问题开窗函数
- python拥有丰富的第三方库_Python第三方库的安装方法
- 电脑分辨率设置工具_打印不求人:我猜你并不会设置“分辨率”!
- Git Extensions 2.33出现unhandled exception has ……解决方法
- Weka--Explorer基本流程
- org manual翻译--2.7 纯文本列表
- UNIX-LINUX编程实践教程-第五章-实例代码注解-echostate.c
- matlab使用webcam获取摄像头图像,camList = webcamlist
- Windows安装nginx服务