日常整理PHP中簡單的圖形處理(經(jīng)典)
1.加載GD庫
GD庫是一個開放的動態(tài)創(chuàng)建圖像、源代碼公開的函數(shù)庫,可以從官方網(wǎng)站http://www.boutell.com/gd處下載。目前,GD庫支持GIF、PNG、JPEG、WBMP和XBM等多種圖像格式,用于對圖像的處理。
GD庫在PHP 5中是默認(rèn)安裝的,但要激活GD庫,必須修改php.ini文件。將該文件中的“;extension=php_gd2.dll”選項前的分號“;”刪除,保存修改后的文件并重新啟動Apache服務(wù)器即可生效。
2.創(chuàng)建一個簡單的圖像
使用GD2函數(shù)庫可以實現(xiàn)各種圖形圖像的處理。創(chuàng)建畫布是使用GD2函數(shù)庫來創(chuàng)建圖像的第一步,無論創(chuàng)建什么樣的圖像,首先都需要創(chuàng)建一個畫布,其他操作都將在這個畫布上完成。在GD2函數(shù)庫中創(chuàng)建畫布,可以通過imagecreate()函數(shù)實現(xiàn)。
使用imagecreate()函數(shù)創(chuàng)建一個寬度為200像素,高度為60像素的畫布,并設(shè)置畫布顏色RGB(225,66,159),最后輸出一個GIF格式的圖像,代碼如下:
<?php $im = imagecreate(200,60); //創(chuàng)建一個畫布 $white = imagecolorallocate($im, 225,66,159); //設(shè)置畫布的背景顏色為淺綠色 imagegif($im); //輸出圖像 ?>
3.使用GD2函數(shù)在照片上添加文字
PHP中的GD庫支持中文,但必須要以UTF-8格式的參數(shù)來進(jìn)行傳遞,如果使用imageString()函數(shù)直接繪制中文字符串就會顯示亂碼,這是因為GD2對中文只能接收UTF-8編碼格式,并且默認(rèn)使用英文字體,所以要輸出中文字符串,必須對中文字符串進(jìn)行轉(zhuǎn)碼,并設(shè)置中文字符使用的字體。否則,輸出的只能是亂碼。
使用imageTTFText()函數(shù)將文字“這是一個測試”輸出到圖像中,代碼如下:
<?php header("content-type:image/jpeg"); //定義輸出為圖像類型 $im=imagecreatefromjpeg("images/photo.jpg"); //載入照片 $textcolor=imagecolorallocate($im,56,73,136);//設(shè)置字體顏色為藍(lán)色,值為RGB顏色值 $fnt="c:/windows/fonts/simhei.ttf"; //定義字體 $motto=iconv("gb2312","utf-8","這是一個測試"); //定義輸出字體串 imageTTFText($im,220,0,480,340,$textcolor,$fnt,$motto); //寫TTF文字到圖中 imagejpeg($im); //建立JPEG圖形 imagedestroy($im); //結(jié)束圖形,釋放內(nèi)存空間 ?>
4.PHP生成驗證碼
創(chuàng)建一個checks.php文件在文件中使用GD2函數(shù)創(chuàng)建一個4位的驗證碼,并將生成的驗證碼保存到session中:
<?php session_start(); header("content-type:image/png"); //設(shè)置創(chuàng)建圖像的格式 $image_width=70; //設(shè)置圖像寬度 $image_height=18; //設(shè)置圖像高度 srand(microtime()*100000); //設(shè)置隨機(jī)數(shù)的種子 for($i=0;$i<4;$i++){ //循環(huán)輸出一個4位的隨機(jī)數(shù) $new_number.=dechex(rand(0,15)); } $_SESSION[check_checks]=$new_number; //將獲取的隨機(jī)數(shù)驗證碼寫入到SESSION變量中 $num_image=imagecreate($image_width,$image_height); //創(chuàng)建一個畫布 imagecolorallocate($num_image,255,255,255); //設(shè)置畫布的顏色 for($i=0;$i<strlen($_SESSION[check_checks]);$i++){ //循環(huán)讀取SESSION變量中的驗證碼 $font=mt_rand(3,5); //設(shè)置隨機(jī)的字體 $x=mt_rand(1,8)+$image_width*$i/4; //設(shè)置隨機(jī)字符所在位置的X坐標(biāo) $y=mt_rand(1,$image_height/4); //設(shè)置隨機(jī)字符所在位置的Y坐標(biāo) $color=imagecolorallocate($num_image,mt_rand(0,100),mt_rand(0,150),mt_rand(0,200)); //設(shè)置字符的顏色 imagestring($num_image,$font,$x,$y,$_SESSION[check_checks][$i],$color); //水平輸出字符 } imagepng($num_image); //生成PNG格式的圖像 imagedestroy($num_image); //釋放圖像資源 ?>
創(chuàng)建一個用戶登錄的表單并調(diào)用checks.php在表單中輸出圖像的內(nèi)容:
<?php session_start(); if($_POST["Submit"]!=""){ $checks=$_POST["checks"]; if($checks==""){ echo "<script> alert('驗證碼不能為空');window.location.href='index.php';</script>"; } if($checks==$_SESSION[check_checks]){ echo "<script> alert('用戶登錄成功!');window.location.href='index.php';</script>"; }else{ echo "<script> alert('您輸入的驗證碼不正確!');window.location.href='index.php';</script>"; } } ?> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=gb2312"> <title>rand函數(shù)的應(yīng)用</title> <style type="text/css"> <!-- .STYLE1 { font-size: 12px; color: #FFFFFF; font-weight: bold; } .style2 {font-weight: bold; font-size: 12px;} --> </style> </head> <body> <form name="form" method="post" action=""> <table width="1003" border="0" cellspacing="0" cellpadding="0"> <tr> <td width="168" height="169" background="images/index_01.gif"> </td> <td width="685" background="images/index_02.gif"> </td> <td width="150" background="images/index_03.gif"> </td> </tr> <tr> <td width="168" height="311" background="images/index_04.gif"> </td> <td background="images/index_05.gif"><table width="675" height="169" border="0" cellpadding="0" cellspacing="0"> <tr> <td height="43" align="center" valign="baseline"> </td> <td align="center" valign="middle"> </td> <td align="center" valign="baseline"> </td> </tr> <tr> <td width="382" height="24" align="center" valign="baseline"> </td> <td width="207" height="24" valign="middle"><span class="style2">用戶名</span><span class="STYLE1"> <input name="txt_user" id="txt_user" style="height:20px " size="10"> </span></td> <td width="86" height="24" align="center" valign="baseline"> </td> </tr> <tr> <td height="24" align="center" valign="baseline"> </td> <td height="24" valign="middle"><span class="style2">密碼</span><span class="STYLE1"> <input name="txt_pwd" type="password" id="txt_pwd" style="FONT-SIZE: 9pt; height:20px" size="10"> </span></td> <td height="24" align="center" valign="baseline"> </td> </tr> <tr> <td height="24" align="center" valign="baseline"> </td> <td height="24" valign="middle"><span class="style2">驗證碼</span><span class="STYLE1"> <input name="checks" size="6" style="height:20px "> <img src="checks.php" width="70" height="18" border="0" align="bottom"></span> </td> <td height="24" align="center" valign="baseline"> </td> </tr> <tr> <td height="40" align="center" valign="baseline"> </td> <td align="center" valign="baseline"> <input type="submit" name="Submit" value="登錄"></td> <td align="center" valign="baseline"> </td> </tr> </table></td> <td background="images/index_06.gif"> </td> </tr> <tr> <td height="100"> </td> <td> </td> <td> </td> </tr> </table> </form> </body> </html>
以上內(nèi)容是小編給大家分享的有關(guān)php中簡單的圖形處理,希望大家喜歡。
- PHP圖片處理之使用imagecopyresampled函數(shù)實現(xiàn)圖片縮放例子
- PHP圖片處理之圖片旋轉(zhuǎn)和圖片翻轉(zhuǎn)實例
- PHPThumb圖片處理實例
- PHP圖片處理之圖片背景、畫布操作
- PHP圖片處理類 phpThumb參數(shù)用法介紹
- php 從數(shù)據(jù)庫提取二進(jìn)制圖片的處理代碼
- php圖片處理:加水印、縮略圖的實現(xiàn)(自定義函數(shù):watermark、thumbnail)
- PHP使用GIFEncoder類處理gif圖片實例
- php圖片處理函數(shù)獲取類型及擴(kuò)展名實例
- PHP實現(xiàn)的曲線統(tǒng)計圖表示例
- php利用gd庫為圖片添加水印
- php使用高斯算法實現(xiàn)圖片的模糊處理功能示例
相關(guān)文章
從零開始學(xué)YII2框架(六)高級應(yīng)用程序模板
這篇文章主要介紹了YII2框架學(xué)習(xí)筆記之高級應(yīng)用程序模板,深入淺出從安裝,配置到使用方法都做了介紹,希望對大家有所幫助2014-08-08Thinkphp 框架基礎(chǔ)之源碼獲取、環(huán)境要求與目錄結(jié)構(gòu)分析
這篇文章主要介紹了Thinkphp 框架基礎(chǔ)之源碼獲取、環(huán)境要求與目錄結(jié)構(gòu),簡單分析了Thinkphp源碼的獲取方法、下載地址、安裝環(huán)境要求以及目錄結(jié)構(gòu),需要的朋友可以參考下2020-04-04Yii2隱藏frontend/web和backend/web的方法
這篇文章主要介紹了Yii2隱藏frontend/web和backend/web的方法,需要的朋友可以參考下2015-12-12支持中文和其他編碼的php截取字符串函數(shù)分享(截取中文字符串)
這篇文章主要介紹了支持中文和其他編碼的php截取字符串函數(shù)示例(截取中文字符串),需要的朋友可以參考下2014-03-03