搜尋
首頁後端開發php教程PHP利用GD库画图跟生成验证码图片

PHP利用GD库画图和生成验证码图片

首先得确定php.ini设置有没有打开GD扩展功能,测试如下

print_r(gd_info());

如果有打印出内容如下,则说明GD功能有打开:

Array(    [GD Version] => bundled (2.0.34 compatible)    [FreeType Support] => 1    [FreeType Linkage] => with freetype    [T1Lib Support] => 1    [GIF Read Support] => 1    [GIF Create Support] => 1    [JPG Support] => 1    [PNG Support] => 1    [WBMP Support] => 1    [XPM Support] =>     [XBM Support] => 1    [JIS-mapped Japanese Font Support] => )

GD画图一般步骤如下:

1.创建一张画布资源

2.创建颜色画笔

3.画图

4.保存图片或输出图片

5.销毁内存画布资源


测试代码如下:

<?phpheader ("Content-type: image/jpeg");$width = 400;   //宽,高$height = 400;  $image = imagecreate($width, $height); //第一步:创建空白图像$white = imagecolorallocate($image, 0, 0, 0);  //第一次对 imagecolorallocate() 的调用会给基于调色板的图像填充背景色,即用 imagecreate() 建立的图像。 $green = imagecolorallocate($image, 0, 255, 0); //第二步:为图像分配颜色imageline($image, 0, 20, 400, 20, $green);  //第三步:画线imagerectangle($image,100,40,300,100,$green);  //画矩形imagearc($image, 200, 150, 90, 90, 0, 360, $green); //画圆imagestring($image, 14, 100, 240, "PHP is NiuBi HongHong!", $green); //写字符串$str="abcdefghjklmnpqrstuvwxyz23456789";$randstr = substr(str_shuffle($str), 0,4);imagestring($image, 14, 100, 260, $randstr, $green); //验证码imagettftext($image, 14, 0, 100, 300, $green, &#39;./MSJHBD.TTF&#39;, "中文vsEnglish");  //中文验证// imagejpeg($image,&#39;./test.jpg&#39;);   //在当前路径下保存图片为test.jpgimagejpeg($image);  //第四步:不加文件名,直接输出到网页     imagedestroy($image);   //第五步:销毁,回收资源?>

测试图片如下:



注:GD库强大的可以画各种报表(如柱状图,饼状图等)、缩略图、加水印图和股票走势图

缩略图功能例子:

<?phpheader ("Content-type: image/png");$width = 300;   //原图宽,高$height = 210;  $thumb_width = (int)$width/2;$thumb_height = (int)$height/2;$dst = imagecreate($thumb_width,$thumb_height); //创建缩略图画布$gray = imagecolorallocate($dst, 100, 100, 100);$src = imagecreatefrompng(&#39;./me.png&#39;); //读取原图//把原图copy到缩略图画布上imagecopyresampled($dst, $src, 0, 0, 0, 0, $thumb_width, $thumb_height, $width, $height); imagepng($dst,&#39;./me_thumb.png&#39;);imagedestroy($dst);imagedestroy($src);?>


陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
可以在PHP會話中存儲哪些數據?可以在PHP會話中存儲哪些數據?May 02, 2025 am 12:17 AM

phpsessionscanStorestrings,數字,數組和原始物。

您如何開始PHP會話?您如何開始PHP會話?May 02, 2025 am 12:16 AM

tostartaphpsession,usesesses_start()attheScript'Sbeginning.1)placeitbeforeanyOutputtosetThesessionCookie.2)useSessionsforuserDatalikeloginstatusorshoppingcarts.3)regenerateSessiveIdStopreventFentfixationAttacks.s.4)考慮使用AttActAcks.s.s.4)

什麼是會話再生,如何提高安全性?什麼是會話再生,如何提高安全性?May 02, 2025 am 12:15 AM

會話再生是指在用戶進行敏感操作時生成新會話ID並使舊ID失效,以防會話固定攻擊。實現步驟包括:1.檢測敏感操作,2.生成新會話ID,3.銷毀舊會話ID,4.更新用戶端會話信息。

使用PHP會話時有哪些性能考慮?使用PHP會話時有哪些性能考慮?May 02, 2025 am 12:11 AM

PHP会话对应用性能有显著影响。优化方法包括:1.使用数据库存储会话数据,提升响应速度;2.减少会话数据使用,只存储必要信息;3.采用非阻塞会话处理器,提高并发能力;4.调整会话过期时间,平衡用户体验和服务器负担;5.使用持久会话,减少数据读写次数。

PHP會話與Cookie有何不同?PHP會話與Cookie有何不同?May 02, 2025 am 12:03 AM

PHPsessionsareserver-side,whilecookiesareclient-side.1)Sessionsstoredataontheserver,aremoresecure,andhandlelargerdata.2)Cookiesstoredataontheclient,arelesssecure,andlimitedinsize.Usesessionsforsensitivedataandcookiesfornon-sensitive,client-sidedata.

PHP如何識別用戶的會話?PHP如何識別用戶的會話?May 01, 2025 am 12:23 AM

phpIdentifiesauser'ssessionSessionSessionCookiesAndSessionId.1)whiwsession_start()被稱為,phpgeneratesainiquesesesessionIdStoredInacookInAcookInAcienamedInAcienamedphpsessIdontheuser'sbrowser'sbrowser.2)thisIdallowSphptpptpptpptpptpptpptpptoretoreteretrieetrieetrieetrieetrieetrieetreetrieetrieetrieetrieetremthafromtheserver。

確保PHP會議的一些最佳實踐是什麼?確保PHP會議的一些最佳實踐是什麼?May 01, 2025 am 12:22 AM

PHP會話的安全可以通過以下措施實現:1.使用session_regenerate_id()在用戶登錄或重要操作時重新生成會話ID。 2.通過HTTPS協議加密傳輸會話ID。 3.使用session_save_path()指定安全目錄存儲會話數據,並正確設置權限。

PHP會話文件默認存儲在哪裡?PHP會話文件默認存儲在哪裡?May 01, 2025 am 12:15 AM

phpsessionFilesArestoredIntheDirectorySpecifiedBysession.save_path,通常是/tmponunix-likesystemsorc:\ windows \ windows \ temponwindows.tocustomizethis:tocustomizEthis:1)useession_save_save_save_path_path()

See all articles

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

VSCode Windows 64位元 下載

VSCode Windows 64位元 下載

微軟推出的免費、功能強大的一款IDE編輯器

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

Dreamweaver Mac版

Dreamweaver Mac版

視覺化網頁開發工具

SublimeText3 Linux新版

SublimeText3 Linux新版

SublimeText3 Linux最新版