通知短信+运营短信,5秒速达,支持群发助手一键发送🚀高效触达和通知客户 广告
## gd库例子 >下面例子全部 可以直接使用woo控制台运行,简单方便 1.使用`woo -rc` 或 `woo -run_console` 来启动woo控制台交互界面 2.输入`:C` 或 `;;;` 启动多行输入模式, 3.复制下方的代码,粘贴到控制台输入 4.再输入:`:R` 或 `;;;`来运行多行输入的代码,即可看到结果 1.)gd 绘图库测试:绘制椭圆 ```lua|woo local gd = woo.gd:new() local wh = 1024 gd:create(wh, wh) -- 创建一个正方形画布: 1024 X 1024 gd:setRGBA(0, 0, 0, 0.1) for i = 0, 360, 15 do gd:push() gd:rotateAbout(i, wh / 2, wh / 2) gd:drawEllipse(wh / 2, wh / 2, wh * 7 / 16, wh / 8) gd:fill() gd:pop() end -- save png file to desktop folder local save_file = _home() .. "/Desktop/out.png" local file_quality = 80 -- 可以保存为jpg --local err = gd:saveJpg(save_file, file_quality) local err = gd:savePng(save_file) print(err) _show(save_file) -- 显示保存的图片(注意:只有gui桌面才能显示图片,服务器类不能显示图片,请下载图片后自行查看) ``` 输出: ![](https://img.kancloud.cn/ee/a2/eea20934b4f4c3cf36b392cf8ce12d6c_469x468.png) 2.)路径曲线绘画 ```lua|woo local gd = woo.gd:new() function random() return _rand() * 2 - 1 end function point() return random(), random() end function drawCurve(gd) gd:setRGBA(0, 0, 0, 0.1) gd:fillPreserve() gd:setRGB(0, 0, 0) gd:setLineWidth(12) gd:stroke() end function drawPoints(gd) gd:setRGBA(1, 0, 0, 0.5) gd:setLineWidth(2) gd:stroke() end function randomQuadratic(gd) local x0, y0 = point() local x1, y1 = point() local x2, y2 = point() gd:moveTo(x0, y0) gd:quadraticTo(x1, y1, x2, y2) drawCurve(gd) gd:moveTo(x0, y0) gd:lineTo(x1, y1) gd:lineTo(x2, y2) drawPoints(gd) end function randomCubic(gd) local x0, y0 = point() local x1, y1 = point() local x2, y2 = point() local x3, y3 = point() gd:moveTo(x0, y0) gd:cubicTo(x1, y1, x2, y2, x3, y3) -- print(x1, y1, x2, y2, x3, y3) drawCurve(gd) gd:moveTo(x0, y0) gd:lineTo(x1, y1) gd:lineTo(x2, y2) gd:lineTo(x3, y3) drawPoints(gd) end local S = 256 local W = 8 local H = 8 gd:create(S * W, S * H) gd:setRGB(1, 1, 1) gd:clear() for j = 0, H - 1, 1 do for i = 0, W - 1, 1 do local x = i * S + S / 2 local y = j * S + S / 2 print(x, y) gd:push() gd:translate(x, y) gd:scale(S / 2, S / 2) if j % 2 == 0 then randomCubic(gd) else randomQuadratic(gd) end gd:pop() end end gd:savePng("out1.png") _show("out1.png") ``` **输出(如果安装了去广告插件,这个图片不能显示,这个图片看云官方使用md5前两位ad作为目录值,被广告插件屏蔽,群里反馈无果,所以广告插件请停止此页面的拦截):** ![](https://img.kancloud.cn/ad/00/ad00991f42fdb2f71e263b66d717a0a9_689x681.png) 3.)文字描边 ```lua|woo -- bitmap 绘图库测试:meme local gd = woo.gd:new() local S = 1024 gd:create(S, S) -- 创建一个正方形画布: 1024 X 1024 gd:setRGB(1, 1, 1) gd:clear() -- 运行前请把下方字体替换下,目前是deepin系统下的字体 local e = gd:loadFontFace('/usr/share/fonts/truetype/lato/Lato-Medium.ttf', 90) print('loadFontFace:', e) gd:setRGB(0, 0, 0) local s = "ONE DOES NOT SIMPLY" local n = 5 -- // "stroke" size local times = 0 for dy = -n, n, 1 do for dx = -n, n, 1 do if dx * dx + dy * dy >= n * n then -- // give it rounded corners -- continue else times = times + 1 local x = S / 2 + (dx) local y = S / 2 + (dy) gd:drawStringAnchored(s, x, y, 0.5, 0.5) end end end gd:setRGB(1, 1, 1) gd:drawStringAnchored(s, S / 2, S / 2, 0.5, 0.5) local err = gd:savePng("out.png") print(times, err) _show("out.png") -- 显示保存的图片(注意:只有gui桌面才能显示图片,服务器类不能显示图片,请下载图片后自行查看) ``` **输出:** ![](https://img.kancloud.cn/67/88/67883c2cebe4ed8abe3e1e0980cbcd01_498x469.png) 3.)路径绘制,描边:五角星 ```lua|woo local dc=woo.gd:new() function Point(x,y) return {X=x,Y=y} end function Polygon(n) local result = {} for i = 0 ,n,1 do -- body a = (i)*2*math.pi/ (n) - math.pi/2 result[i] = Point(math.cos(a), math.sin(a)) end return result end local W = 1200 local H = 120 local S = 100 dc:create(W, H) dc:setHexColor("#FFFFFF") dc:clear() local n = 5 local points = Polygon(n) for x = S / 2, W, S do dc:push() local s = _rand()*S/4 + S/4 dc:translate( (x), H/2) dc:rotate( _rand() * 2 * math.pi) dc:scale(s, s) for i = 0, n, 1 do local index = (i * 2) % n local p = points[index] dc:lineTo(p.X, p.Y) end dc:setLineWidth(10) dc:setHexColor("#FFCC00") dc:strokePreserve() dc:setHexColor("#FFE43A") dc:fill() dc:pop() end dc:savePng("out.png") _show('out.png') ``` **输出:** ![](https://img.kancloud.cn/a9/75/a97521be7a0f75d8dfefe63a85932cdd_1200x120.png) 4.)绘制chart ```lua|woo local dc=woo.gd:new() function CreatePoints(n) local points = {} for i = 0,n, 1 do local x = 0.09+ _rand() *0.7 local y = 0.1 * _rand() + (_rand()>0.5 and x or -x) y=math.abs(y) points[i] = {X=x,Y=y} _out(_rand(),'\n') end return points end local S = 1024 local P = 64 dc:create(S, S) dc:invertY() dc:setRGB(1, 1, 1) dc:clear() local points = CreatePoints(500) dc:translate(P, P) dc:scale(S-P*2, S-P*2) -- // draw minor grid for i = 1, 10, 1 do local x = (i) / 10 dc:moveTo(x, 0) dc:lineTo(x, 1) dc:moveTo(0, x) dc:lineTo(1, x) end dc:setRGBA(0, 0, 0, 0.25) dc:setLineWidth(1) dc:stroke() -- // draw axes dc:moveTo(0, 0) dc:lineTo(1, 0) dc:moveTo(0, 0) dc:lineTo(0, 1) dc:setRGB(0, 0, 0) dc:setLineWidth(4) dc:stroke() -- // draw points dc:setRGBA(0, 0, 1, 0.5) for _, p in pairs(points) do dc:drawCircle(p.X, p.Y, 3.0/S) dc:fill() end -- // draw text dc:identity() dc:setRGB(0, 0, 0) -- 运行前请把下方字体替换下,目前是deepin系统下的字体 local font_path='/usr/share/fonts/truetype/lato/Lato-Medium.ttf' if dc:loadFontFace(font_path, 24) then print(err) end dc:drawStringAnchored("Chart Title", S/2, P/2, 0.5, 0.5) if dc:loadFontFace(font_path, 18) then print(err) end dc:drawStringAnchored("X Axis Title", S/2, S-P/2, 0.5, 0.5) dc:savePng("out.png") _show('out.png') ``` **输出:** ![](https://img.kancloud.cn/08/a3/08a35b1aef04ff680ff9e0e6f6f76377_811x810.png) 5.)色盘绘制 ```lua|woo local dc=woo.gd:new() dc:create(500, 400) grad = dc:newLinearGradient(20, 320, 400, 20) grad:addColorStop(0, 0, 255, 0, 255 ) grad:addColorStop(1, 0, 0, 255, 255 ) grad:addColorStop(0.5, 255, 0, 0, 255 ) dc:setHexColor('#fff') dc:drawRectangle(20, 20, 400-20, 300) dc:stroke() dc:setStrokeStyle(grad) dc:setLineWidth(4) dc:moveTo(10, 10) dc:lineTo(410, 10) dc:lineTo(410, 100) dc:lineTo(10, 100) dc:closePath() dc:stroke() dc:setFillStyle(grad) dc:moveTo(10, 120) dc:lineTo(410, 120) dc:lineTo(410, 300) dc:lineTo(10, 300) dc:closePath() dc:fill() dc:savePng("out.png") _show('out.png') -- 在服务器中无效 ``` **输出:** ![](https://img.kancloud.cn/98/13/9813494a77d71bcaabb42e81f8cf11fc_406x328.png) 6.)普通文本绘制 ```lua|woo local dc=woo.gd:new() local S = 1024 dc:create(S, S) dc:create() dc:setRGB(1, 1, 1) dc:clear() dc:setRGB(0, 0, 0) -- 运行前替换下方字体,绝对路径 dc:loadFontFace("/usr/share/fonts/truetype/lato/Lato-Medium.ttf", 96) dc:drawStringAnchored("Hello, world! 你好,世界!", S/2, S/2, 0.5, 0.5) dc:savePng("out.png") _show('out.png') ``` 7.)绘制表格 ```lua|woo local dc=woo.gd:new() local W = 1000 local H = 1000 local Minor = 10 local Major = 100 dc :create(W, H) dc:setRGB(1, 1, 1) dc:clear() -- // minor grid for x = Minor, W-1, Minor do fx = (x) + 0.5 dc:drawLine(fx, 0, fx, H) end for y = Minor, H-1, Minor do fy = (y) + 0.5 dc:drawLine(0, fy, W, fy) end dc:setLineWidth(1) dc:setRGBA(0, 0, 0, 0.25) dc:stroke() -- // major grid for x = Major, W-1, Major do fx = (x) + 0.5 dc:drawLine(fx, 0, fx, H) end for y = Major, H-1, Major do fy = (y) + 0.5 dc:drawLine(0, fy, W, fy) end dc:setLineWidth(1) dc:setRGBA(0, 0, 0, 0.5) dc:stroke() dc:savePng("out.png") _show('out.png') ``` **输出:** ![](https://img.kancloud.cn/b9/88/b988c1417071e80dd90aafd893df4d47_1000x1000.png) ***** 8.)画直线 ```lua local dc=woo.gd:new() local W = 1024 local H = 1024 dc:create(W, H) dc:setRGB(0, 0, 0) dc:clear() for i = 0,1000-1,1 do local x1 = _rand() * W local y1 = _rand() * H local x2 = _rand() * W local y2 = _rand() * H local r = _rand() local g = _rand() local b = _rand() local a = _rand()*0.5 + 0.5 local w = _rand()*4 + 1 dc:setRGBA(r, g, b, a) dc:setLineWidth(w) dc:drawLine(x1, y1, x2, y2) dc:stroke() end dc:savePng("out.png") _show('out.png') ``` **输出:** ![](https://img.kancloud.cn/6c/b4/6cb41cc518fabbd753c9f74385727a8b_1024x1024.png) ***** 9.)填充 ```lua|woo local dc=woo.gd:new() dc:create(1000, 1000) for j = 0, 10-1,1 do for i = 0,10-1,1 do x = (i)*100 + 50 y = (j)*100 + 50 a1 = _rand() * 2 * math.pi a2 = a1 + _rand()*math.pi + math.pi/2 dc:drawArc(x, y, 40, a1, a2) print(x, y, 40, a1, a2) -- dc:closePath() end end dc:setRGB(0, 0, 0) dc:fillPreserve() dc:setRGB(1, 1, 1) dc:setLineWidth(8) dc:strokePreserve() dc:setRGB(1, 0, 0) dc:setLineWidth(4) dc:strokePreserve() dc:savePng("out.png") _show('out.png') ``` **输出:** ![](https://img.kancloud.cn/68/07/6807bb47b8608f0c64eec70f27fcc4d1_1000x1000.png) ***** 10.)圆形填充 ```lua|woo local dc=woo.gd:new() local S = 1024 local N = 2048 dc:create(S, S) dc:setRGB(1, 1, 1) dc:clear() dc:setRGB(0, 0, 0) for i = 0, N-1,1 do local t = (i) / N local d = t*S*0.4 + 10 local a = t * math.pi * 2 * 20 local x = S/2 + math.cos(a)*d local y = S/2 + math.sin(a)*d local r = t * 8 dc:drawCircle(x, y, r) end dc:fill() dc:savePng("out.png") _show('out.png') ``` **输出:** ![](https://img.kancloud.cn/2e/85/2e85b7268779397fc583c93b16a7b73b_1024x1024.png) 11.)文本排版换行 ```lua|woo local dc=woo.gd:new() local TEXT = "Call me Ishmael. Some years ago—never mind how long precisely—having little or no money in my purse, and nothing particular to interest me on shore, I thought I would sail about a little and see the watery part of the world. It is a way I have of driving off the spleen and regulating the circulation. Whenever I find myself growing grim about the mouth; whenever it is a damp, drizzly November in my soul; whenever I find myself involuntarily pausing before coffin warehouses, and bringing up the rear of every funeral I meet; and especially whenever my hypos get such an upper hand of me, that it requires a strong moral principle to prevent me from deliberately stepping into the street, and methodically knocking people's hats off—then, I account it high time to get to sea as soon as I can. This is my substitute for pistol and ball. With a philosophical flourish Cato throws himself upon his sword; I quietly take to the ship. There is nothing surprising in this. If they but knew it, almost all men in their degree, some time or other, cherish very nearly the same feelings towards the ocean with me." local W = 1024 local H = 1024 local P = 16 dc:create(W, H) dc:setRGB(1, 1, 1) dc:clear() dc:drawLine(W/2, 0, W/2, H) dc:drawLine(0, H/2, W, H/2) dc:drawRectangle(P, P, W-P-P, H-P-P) dc:setRGBA(0, 0, 1, 0.25) dc:setLineWidth(3) dc:stroke() dc:setRGB(0, 0, 0) dc:loadFontFace("/usr/share/fonts/truetype/lato/Lato-Medium.ttf", 18) dc:drawStringWrapped("UPPER LEFT", P, P, 0, 0, 0, 1.5, woo.gd.ALIGN_LEFT) dc:drawStringWrapped("UPPER RIGHT", W-P, P, 1, 0, 0, 1.5, woo.gd.ALIGN_RIGHT) dc:drawStringWrapped("BOTTOM LEFT", P, H-P, 0, 1, 0, 1.5, woo.gd.ALIGN_LEFT) dc:drawStringWrapped("BOTTOM RIGHT", W-P, H-P, 1, 1, 0, 1.5, woo.gd.ALIGN_RIGHT) dc:drawStringWrapped("UPPER MIDDLE", W/2, P, 0.5, 0, 0, 1.5, woo.gd.ALIGN_CENTER) dc:drawStringWrapped("LOWER MIDDLE", W/2, H-P, 0.5, 1, 0, 1.5, woo.gd.ALIGN_CENTER) dc:drawStringWrapped("LEFT MIDDLE", P, H/2, 0, 0.5, 0, 1.5, woo.gd.ALIGN_LEFT) dc:drawStringWrapped("RIGHT MIDDLE", W-P, H/2, 1, 0.5, 0, 1.5, woo.gd.ALIGN_RIGHT) dc:loadFontFace("/usr/share/fonts/truetype/lato/Lato-Medium.ttf", 12) dc:drawStringWrapped(TEXT, W/2-P, H/2-P, 1, 1, W/3, 1.75, woo.gd.ALIGN_LEFT) dc:drawStringWrapped(TEXT, W/2+P, H/2-P, 0, 1, W/3, 2, woo.gd.ALIGN_LEFT) dc:drawStringWrapped(TEXT, W/2-P, H/2+P, 1, 0, W/3, 2.25, woo.gd.ALIGN_LEFT) dc:drawStringWrapped(TEXT, W/2+P, H/2+P, 0, 0, W/3, 2.5, woo.gd.ALIGN_LEFT) dc:savePng("out.png") _show('out.png') ``` ![](https://img.kancloud.cn/7c/e1/7ce1a12a0fa2874b338b63a125c4b164_1024x1024.png) 12.)图片九宫格 ``` local avatar = woo.gd:new() -- 从桌面读取avatar.jpg图片 avatar:loadImage(_home() .. '/Desktop/' .. 'avatar.jpg') -- 重新修改头像尺寸180*180像素 avatar:resize(180, 180, 0) -- 新建一个正方形画布背景 local gd = woo.gd:new() -- 创建头像,-- 初始化正方形画布背景 gd:create(180 * 3 + 5 * 3, 180 * 3 + 5 * 3) -- 开始绘制头像图片到背景 for i = 1, 3 do for j = 1, 3 do gd:drawImage(avatar, (i - 1) * 180 + (i - 1) * 5, (j - 1) * 180 + (j - 1) * 5) end end -- 保存最终到结果到 桌面 文件:9.png gd:savePng(_home() .. '/Desktop/9.png') ``` **输出:** ![](https://img.kancloud.cn/db/62/db6256b81c647a9e6dbcb618f00d7f88_600x398.jpg) 13.)绘制圆形头像 ~~~ local gd = woo.gd:new() --加载桌面到avatar.jpg文件,请自己修改对于到图片路径 gd:loadImage(_home() .. '/Desktop/avatar.jpg') -- 这里设置0表示圆角头像 gd:setBorderRadius(0) gd:savePng(_home() .. '/Desktop/r.png') ~~~ **输出:** ![](https://img.kancloud.cn/ab/a9/aba9b040836a5c6bb1b8ed50e2979195_100x100.png) 14.)绘制圆角头像 ~~~ local gd = woo.gd:new() --加载桌面到avatar.jpg文件,请自己修改对于到图片路径 gd:loadImage(_home() .. '/Desktop/avatar.jpg') -- 这里设置8表示圆角为8像素 gd:setBorderRadius(8) gd:savePng(_home() .. '/Desktop/r.png') ~~~ **输出:** ![](https://img.kancloud.cn/a0/45/a04564a32c07174d54a52ca7c16ff677_200x200.png) ***** 15.)图像合成 ~~~ local avatar = woo.gd:new() -- 加载正方形头像 avatar:loadImage(_home() .. '/Desktop/avatar.jpg') -- 重新修改头像尺寸 avatar:resize(144, 144, 0) -- 设置圆形头像 avatar:setBorderRadius(0) ---头像外框 local avatar_bord = woo.gd:new() -- 加载头像外框 avatar_bord:loadImage(_home() .. '/Desktop/avatar_bord.png') --- 背景画布 local background = woo.gd:new() -- 背景创建一个空白画布 background:create(172, 200) -- 绘制头像到空白画布 background:drawImage(avatar, 14, 13) -- 绘制 头像边框到空白画布 background:drawImage(avatar_bord, 0, 0) -- 保存最终结果到桌面p.png background:savePng(_home() .. '/Desktop/p.png') ~~~ **输出:** ![](https://img.kancloud.cn/80/58/80586e79f74dcf6ec18d0d29697cd266_1504x1106.jpg) ***** 16.)绘制验证码(可以去:[B站看视频教程](https://www.bilibili.com/video/BV1Go4y1U7CJ?from=search&seid=15377466184202674859&spm_id_from=333.337.0.0)) ``` -- woo语言生成验证码Demo local dc = woo.gd:new() local W = 160 local H = 50 dc:create(W, H) -- 红绿蓝取值 0-1(0-255) dc:setRGB(0, 0, 0) --dc:setRGB255(255,255,255) dc:clear() -- 随机数种子 _rand(_time()) -- 随机设置绘制的颜色 local rgba = function() local r = _rand() local g = _rand() local b = _rand() local a = _rand() * 0.5 + 0.5 dc:setRGBA(r, g, b, a) end for i = 0, 10 do local x1 = _rand() * W local y1 = _rand() * H local x2 = _rand() * W local y2 = _rand() * H local w = _rand() * 4 + 1 rgba() dc:setLineWidth(w) dc:drawLine(x1, y1, x2, y2) dc:stroke() end if _os() == 'windows' then dc:loadFontFace("C:\\Windows\\Fonts\\arial.ttf", 28) else -- unix系统,请设置对应的字体绝对路径 --dc:loadFontFace("***", 28) end -- 设置随机数种子 math.randomseed(_time()) local qstr = '' for i = 1, 4 do qstr = _chr(_rand() < .5 and math.random(65, 90) or math.random(97, 122)) dc:drawString(qstr, W / 5 * i, H / 2 + 10) rgba() end -- png文件保存到桌面gd.png local path = _home() .. '/Desktop/gd.png' dc:savePng(path) _show(path) ``` **输出** ![](https://img.kancloud.cn/ba/89/ba896e830ad13383b6cc95ff2c6e7826_96x41.jpg)