PHP構造驗證碼

來源:互聯網
上載者:User

標籤:php   驗證碼   

代碼如下:

<?phpheader('Content-type:image/jpeg');$width=120;$height=40;$element=array('a','b','c','d','e','f','g','h','i','j','k','m','n','o','p','q','r','s','t','u','v','w','x','y','z');$string='';for ($i=0;$i<5;$i++){<span style="white-space:pre"></span>$string.=$element[rand(0,count($element)-1)];}$img=imagecreatetruecolor($width, $height);//設定圖片大小$colorBg=imagecolorallocate($img,rand(200,255),rand(200,255),rand(200,255));//隨機產生背景色$colorBorder=imagecolorallocate($img,rand(200,255),rand(200,255),rand(200,255));//隨機產生背景色$colorString=imagecolorallocate($img,rand(10,100),rand(10,100),rand(10,100));imagefill($img,0,0,$colorBg);//設定圖片背景色imagerectangle($img,0,0,$width-1,$height-1,$colorBorder);//構建矩形邊框for($i=0;$i<100;$i++){//畫一百個小像素<span style="white-space:pre"></span>imagesetpixel($img,rand(0,$width-1),rand(0,$height-1),imagecolorallocate($img,rand(100,200),rand(100,200),rand(100,200)));//畫一個單一的像素}for($i=0;$i<3;$i++){//畫三條隨機線<span style="white-space:pre"></span>imageline($img,rand(0,$width/2),rand(0,$height),rand($width/2,$width),rand(0,$height),imagecolorallocate($img,rand(100,200),rand(100,200),rand(100,200)));<span style="white-space:pre"></span>//前兩個為起點座標,後兩個為終點座標,起點控制在左半邊,終點控制在右半邊}//imagestring($img,5,0,0,'abcd',$colorString);imagettftext($img,14,rand(-5,5),rand(5,15),rand(30,35),$colorString,'font/SketchyComic.ttf',$string);//繪製文字,可以選擇豐富的字型的方法//說明:array imagettftext ( resource $image , float $size , float $angle , int $x , int $y , int $color , string $fontfile , string $text )imagejpeg($img);imagedestroy($img);

重點介紹 imagettftext()方法:

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

image
映像資源。見 imagecreatetruecolor()。
size
字型大小。根據 GD 版本不同,應該以像素大小指定(GD1)或點大小(GD2)。
angle
角度製表示的角度,0 度為從左向右讀的文本。更高數值表示逆時針旋轉。例如 90 度表示從下向上讀的文本。
x
xy 所表示的座標定義了第一個字元的基本點(大概是字元的左下角)。這和imagestring() 不同,其 x,y 定義了第一個字元的左上方。例如 "top left" 為 0, 0。
y
Y 座標。它設定了字型基準的位置,不是字元的最底端。
color
色彩索引。使用負的色彩索引值具有關閉防鋸齒的效果。見 imagecolorallocate()。
fontfile
是想要使用的 TrueType 字型的路徑。 根據 PHP 所使用的 GD 庫的不同,當 fontfile 沒有以 / 開頭時則 .ttf 將被加到檔案名稱之後並且會在庫定義字型路徑中嘗試搜尋該檔案名稱。當使用的 GD 庫版本低於 2.0.18 時,一個空格字元 而不是分號將被用來作為不同字型檔的“路徑分隔字元”。不小心使用了此特性將會導致一條警告資訊: Warning: Could not find/open font。對受影響的版本來說唯一解決方案就是將字型移動到不包含空格的路徑中去。

很多情況下字型都放在指令碼的同一個目錄下。下面的小技巧可以減輕包含的問題。

<?php
// Set the enviroment variable for GD
putenv(‘GDFONTPATH=‘ . realpath(‘.‘));

// Name the font to be used (note the lack of the .ttf extension)
$font = ‘SomeFont‘;
?>

text
文本字串。 可以包含十進位數字化字元表示(形式為:&#8364;)來訪問字型中超過位置 127 的字元。UTF-8 編碼的字串可以直接傳遞。如果字串中使用的某個字元不被字型支援,一個空心矩形將替換該字元。

imagettftext() 返回一個含有 8 個單元的數組表示了文本外框的四個角,順序為坐下角,右下角,右上方,左上方。這些點是相對於文本的而和角度無關,因此“左上方”指的是以水平方向看文字時其左上方。

Example #1 imagettftext() 例子

本例中的指令碼將產生一個白色的 400x30 像素 PNG 映像,其中有黑色(帶灰色陰影)Arial 字型寫的“Testing...”。

<?php
// Set the content-type
header("Content-type: image/png");

// Create the image
$im = imagecreatetruecolor(400, 30);

// Create some colors
$white = imagecolorallocate($im, 255, 255, 255);
$grey = imagecolorallocate($im, 128, 128, 128);
$black = imagecolorallocate($im, 0, 0, 0);
imagefilledrectangle($im, 0, 0, 399, 29, $white);

// The text to draw
$text = ‘Testing...‘;
// Replace path by your own font path
$font = ‘arial.ttf‘;

// Add some shadow to the text
imagettftext($im, 20, 0, 11, 21, $grey, $font, $text);

// Add the text
imagettftext($im, 20, 0, 10, 20, $black, $font, $text);

// Using imagepng() results in clearer text compared with imagejpeg()
imagepng($im);
imagedestroy($im);
?>

PHP構造驗證碼

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.