ubuntu:通过封装验证码类库一步步安装php的gd扩展

我相信很多人的lamp环境都是直接复制一堆参数安装进去的,这里有可能成功,也有可能失败,如果是新手,估计要碰到各种错误,就算安装成功,也未必知道那些参数是干嘛的,反正装进去能用就行。

我当初开始的时候也是这样, 完全一脸懵逼,直到我后来进修了( C语言,Linux,Linux系统编程,计算机原理等 )才能玩转服务器配置,理解原理。

本文通过一个经典的验证码功能,告诉你如何按需安装扩展!!!

要封装一个php的验证码类库,那么需要gd扩展的支持,由于我之前的php是精简编译安装的,没有gd库的支持,所以要先安装php的gd库

要想成功安装gd库,需要安装很多的依赖: zlib, png, jpeg, libiconv, freetype

1,zlib我之前在安装 php memcache的扩展时候,已经安装过了

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,到了最关键的地方了,安装php gd扩展 

切换到php原码包下  ext/gd  这个目录

>先执行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验证码库,最后效果

ubuntu:通过封装验证码类库一步步安装php的gd扩展

 <?php

 class Captcha {
//验证码字符个数
protected $num;
//宽度
protected $width;
//高度
protected $height;
//资源
protected $im;
//类型: 1:纯数字, 2:纯字母 3:数字与字母混合
protected $codeType;
//生成的验证码字符串
protected $codeString; public function __construct( $_num = 4, $_width = 150, $_height = 50, $_codeType = 3 ){
$this->num = $_num;
$this->width = $_width;
$this->height = $_height;
$this->codeType = $_codeType;
$this->codeString = $this->createCodeString();
} public function __get( $name ) {
if( $name == 'codeString' ) {
return $this->codeString;
}
return false;
} protected function createCodeString(){
switch( $this->codeType ){
case 1:
$code = $this->createNumCode();
break;
case 2:
$code = $this->createCharCode();
break;
case 3:
$code = $this->createNumCharCode();
break;
default:
die( "暂时没有其他的验证码类型,你可以继承这个类来添加额" );
}
return $code;
} protected function createNumCode(){
$numString = join( "", range( 0, 9 ) );
return substr( str_shuffle( $numString ), 0, $this->num );
} protected function createCharCode(){
$charString = join( "", range( 'a', 'z' ) );
$charString .= strtoupper( $charString );
return substr( str_shuffle( $numString ), 0, $this->num );
} protected function createNumCharCode(){
$numCharString = join( "", range( 0, 9 ) );
$charString = join( "", range( 'a', 'z' ) );
$charString .= strtoupper( $charString );
$mixedString = $numCharString . $charString;
return substr( str_shuffle( $mixedString ), 0, $this->num );
} protected function createCanvas(){
$this->im = imagecreatetruecolor( $this->width, $this->height );
} protected function fillCanvas( $type ){
switch( $type ) {
case 1:
$color = imagecolorallocate( $this->im, mt_rand( 0, 150 ), mt_rand( 0, 150 ), mt_rand( 0, 150 ) );
break;
case 2:
$color = imagecolorallocate( $this->im, mt_rand( 150, 255 ), mt_rand( 150, 255 ), mt_rand( 150, 255 ) );
break;
default:
die( "不支持的填充类型" );
break;
}
imagefill( $this->im, 0, 0, $color );
} protected function drawCodeString(){
for( $i = 0; $i < $this->num; $i++ ){
$fontColor = imagecolorallocate( $this->im, mt_rand( 0, 255 ), mt_rand( 0, 255 ), mt_rand( 0, 255 ) );
$char = $this->codeString[$i];
$x = ( $i * ( ($this->width) / $this->num ) ) + mt_rand( 5, 10 );
$y = mt_rand( 0, $this->height / 2 ) + mt_rand( 5, 10 );
imagestring( $this->im, 5, $x, $y, $char, $fontColor );
}
} protected function renderImage(){
header( "Content-type:image/png" );
imagepng( $this->im );
} protected function fillDisturb(){
for( $i = 0; $i < 100; $i++ ){
$color = imagecolorallocate( $this->im, mt_rand( 0, 255 ), mt_rand( 0, 255 ), mt_rand( 0, 255 ) );
imagesetpixel( $this->im, mt_rand( 0, $this->width ), mt_rand( 0, $this->height ), $color );
}
} public function render(){
//创建画布/背景
$this->createCanvas();
//填充画布颜色
$this->fillCanvas( 1 );
//填充干扰元素
$this->fillDisturb();
//填充验证码
$this->drawCodeString();
//显示验证码
$this->renderImage();
}
} $capt = new Captcha();
//echo $capt->codeString . PHP_EOL;
$capt->render();
?>

小结:

1,复习类库的封装

2,理解扩展安装原理

上一篇:Atitit mac os 版本 新特性 attilax大总结


下一篇:js json 对象相互转换