ubuntu:通过封装验证码类库一步步安装php的gd扩展
我相信很多人的lamp環(huán)境都是直接復(fù)制一堆參數(shù)安裝進去的,這里有可能成功,也有可能失敗,如果是新手,估計要碰到各種錯誤,就算安裝成功,也未必知道那些參數(shù)是干嘛的,反正裝進去能用就行。
我當(dāng)初開始的時候也是這樣, 完全一臉懵逼,直到我后來進修了( C語言,Linux,Linux系統(tǒng)編程,計算機原理等 )才能玩轉(zhuǎn)服務(wù)器配置,理解原理。
本文通過一個經(jīng)典的驗證碼功能,告訴你如何按需安裝擴展!!!
要封裝一個php的驗證碼類庫,那么需要gd擴展的支持,由于我之前的php是精簡編譯安裝的,沒有g(shù)d庫的支持,所以要先安裝php的gd庫
要想成功安裝gd庫,需要安裝很多的依賴: zlib, png, jpeg, libiconv, freetype
1,zlib我之前在安裝 php memcache的擴展時候,已經(jīng)安裝過了
2,安裝png
wget http://prdownloads.sourceforge.net/libpng/libpng-1.5.4.tar.gz?download
mv libpng-1.5.4.tar.gz\?download libpng-1.5.4.tar.gz
tar xf libpng-1.5.4.tar.gz
cd libpng-1.5.4/
./configure
make && sudo make install
3,安裝jpeg
wget http://www.ijg.org/files/jpegsrc.v9b.tar.gz
tar xf jpegsrc.v9b.tar.gz
cd jpeg-9b/
./configure
make && sudo make install
4,安裝libiconv
wget https://ftp.gnu.org/pub/gnu/libiconv/libiconv-1.15.tar.gz
tar xf libiconv-1.15.tar.gz
cd libiconv-1.15/
./configure
make && sudo make install
5,安裝freeType
wget https://download.savannah.gnu.org/releases/freetype/freetype-2.9.tar.gz
tar xf freetype-2.9.tar.gz
cd freetype-2.9/
./configure
make && sudo make install
6,到了最關(guān)鍵的地方了,安裝php gd擴展?
切換到php原碼包下? ext/gd? 這個目錄
>先執(zhí)行phpize外掛模塊(?/usr/local/php54/bin/phpize? )
>./configure --with-php-config=/usr/local/php54/bin/php-config -with-png-dir --with-freetype-dir --with-jpeg-dir -with-zlib-dir --with-gd?
>make && sudo make install
?
開始封裝php驗證碼庫,最后效果
1 <?php 2 3 class Captcha { 4 //驗證碼字符個數(shù) 5 protected $num; 6 //寬度 7 protected $width; 8 //高度 9 protected $height; 10 //資源 11 protected $im; 12 //類型: 1:純數(shù)字, 2:純字母 3:數(shù)字與字母混合 13 protected $codeType; 14 //生成的驗證碼字符串 15 protected $codeString; 16 17 public function __construct( $_num = 4, $_width = 150, $_height = 50, $_codeType = 3 ){ 18 $this->num = $_num; 19 $this->width = $_width; 20 $this->height = $_height; 21 $this->codeType = $_codeType; 22 $this->codeString = $this->createCodeString(); 23 } 24 25 public function __get( $name ) { 26 if( $name == 'codeString' ) { 27 return $this->codeString; 28 } 29 return false; 30 } 31 32 protected function createCodeString(){ 33 switch( $this->codeType ){ 34 case 1: 35 $code = $this->createNumCode(); 36 break; 37 case 2: 38 $code = $this->createCharCode(); 39 break; 40 case 3: 41 $code = $this->createNumCharCode(); 42 break; 43 default: 44 die( "暫時沒有其他的驗證碼類型,你可以繼承這個類來添加額" ); 45 } 46 return $code; 47 } 48 49 protected function createNumCode(){ 50 $numString = join( "", range( 0, 9 ) ); 51 return substr( str_shuffle( $numString ), 0, $this->num ); 52 } 53 54 protected function createCharCode(){ 55 $charString = join( "", range( 'a', 'z' ) ); 56 $charString .= strtoupper( $charString ); 57 return substr( str_shuffle( $numString ), 0, $this->num ); 58 } 59 60 protected function createNumCharCode(){ 61 $numCharString = join( "", range( 0, 9 ) ); 62 $charString = join( "", range( 'a', 'z' ) ); 63 $charString .= strtoupper( $charString ); 64 $mixedString = $numCharString . $charString; 65 return substr( str_shuffle( $mixedString ), 0, $this->num ); 66 } 67 68 protected function createCanvas(){ 69 $this->im = imagecreatetruecolor( $this->width, $this->height ); 70 } 71 72 protected function fillCanvas( $type ){ 73 switch( $type ) { 74 case 1: 75 $color = imagecolorallocate( $this->im, mt_rand( 0, 150 ), mt_rand( 0, 150 ), mt_rand( 0, 150 ) ); 76 break; 77 case 2: 78 $color = imagecolorallocate( $this->im, mt_rand( 150, 255 ), mt_rand( 150, 255 ), mt_rand( 150, 255 ) ); 79 break; 80 default: 81 die( "不支持的填充類型" ); 82 break; 83 } 84 imagefill( $this->im, 0, 0, $color ); 85 } 86 87 protected function drawCodeString(){ 88 for( $i = 0; $i < $this->num; $i++ ){ 89 $fontColor = imagecolorallocate( $this->im, mt_rand( 0, 255 ), mt_rand( 0, 255 ), mt_rand( 0, 255 ) ); 90 $char = $this->codeString[$i]; 91 $x = ( $i * ( ($this->width) / $this->num ) ) + mt_rand( 5, 10 ); 92 $y = mt_rand( 0, $this->height / 2 ) + mt_rand( 5, 10 ); 93 imagestring( $this->im, 5, $x, $y, $char, $fontColor ); 94 } 95 } 96 97 protected function renderImage(){ 98 header( "Content-type:image/png" ); 99 imagepng( $this->im ); 100 } 101 102 protected function fillDisturb(){ 103 for( $i = 0; $i < 100; $i++ ){ 104 $color = imagecolorallocate( $this->im, mt_rand( 0, 255 ), mt_rand( 0, 255 ), mt_rand( 0, 255 ) ); 105 imagesetpixel( $this->im, mt_rand( 0, $this->width ), mt_rand( 0, $this->height ), $color ); 106 } 107 } 108 109 public function render(){ 110 //創(chuàng)建畫布/背景 111 $this->createCanvas(); 112 //填充畫布顏色 113 $this->fillCanvas( 1 ); 114 //填充干擾元素 115 $this->fillDisturb(); 116 //填充驗證碼 117 $this->drawCodeString(); 118 //顯示驗證碼 119 $this->renderImage(); 120 } 121 } 122 123 $capt = new Captcha(); 124 //echo $capt->codeString . PHP_EOL; 125 $capt->render(); 126 ?>?
小結(jié):
1,復(fù)習(xí)類庫的封裝
2,理解擴展安裝原理?
??
轉(zhuǎn)載于:https://www.cnblogs.com/ghostwu/p/8472125.html
總結(jié)
以上是生活随笔為你收集整理的ubuntu:通过封装验证码类库一步步安装php的gd扩展的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 毛泽东军事思想的内容包括战争观和方法论
- 下一篇: Asiacensoredsection