我相信很多人的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验证码库,最后效果

  1. <?php
  2.  
  3. class Captcha {
  4. //验证码字符个数
  5. protected $num;
  6. //宽度
  7. protected $width;
  8. //高度
  9. protected $height;
  10. //资源
  11. protected $im;
  12. //类型: 1:纯数字, 2:纯字母 3:数字与字母混合
  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. //创建画布/背景
  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. ?>

小结:

1,复习类库的封装

2,理解扩展安装原理

ubuntu:通过封装验证码类库一步步安装php的gd扩展的更多相关文章

  1. 将当前的Ubuntu系统封装成为可以安装(发布)的iso镜像

    将当前的Ubuntu系统封装成为可以安装(发布)的iso镜像 在使用以上方法安装依赖的时候xresprobe 会找不到安装地址,采用下面的方式: Package xresprobe is not in ...

  2. Ubuntu学习总结-01 用VMware 8安装Ubuntu 12.04详细过程

    1 Ubuntu 下载地址 http://www.ubuntu.com/download/desktop 2 安装Ubuntu 转载用VMware 8安装Ubuntu 12.04详细过程 http:/ ...

  3. Ubuntu安装MongoDB和PHP扩展

    MongoDB是一个可伸缩的,高性能的开源NoSQL 文档数据库.主要用C++开发完成.面向文档存储,全索引支持,可复制和高可用性,自动分片等特征.其在非关系型数据库中是功能最丰富,最像关系型数据库 ...

  4. Thinkpad W520 + Ubuntu 12.04LTS, 13.10, 14.04LTS安装Nvidia显卡驱动设置

    Thinkpad W520 + Ubuntu 12.04LTS, 13.10, 14.04LTS安装Nvidia显卡驱动设置 http://henzhai.com/tech/2012/07/w520- ...

  5. ubuntu常用命令及操作,包括安装CUDA

    chmod Document 这里Document是一个文件夹,文件夹中还有好多子文件,可以发现执行了这条指令以后,其子文件夹的权限并没有改变. 要想改变其子文件夹的权限,应该执行 Document/ ...

  6. ubuntu 用remastersys 备份系统并且安装

    sudo add-apt-repository ppa:mutse-young/remastersys 2.更新系统软件源 sudo apt-get update 3.更新完了,先安装remaster ...

  7. Linux(Ubuntu)使用 sudo apt-get install 命令安装软件的目录在哪?(已解决)

    Linux(Ubuntu)使用   sudo apt-get install  命令安装软件的目录在哪? bin文件路径: /usr/bin 库文件路径: /usr/lib/  其它的图标啊什么的路径 ...

  8. ubuntu 12.04及12.10无法安装 ia32-libs

    administrator@ubuntu:~$ sudo apt-get install ia32-libs [sudo] password for administrator:  正在读取软件包列表 ...

  9. Ubuntu 16.04下EasyOpenJTAG+OpenOCD的安装和使用【转】

    本文转载自:http://www.linuxdiyf.com/linux/24086.html Ubuntu 16.04下EasyOpenJTAG+OpenOCD的安装和使用 发布时间:2016-09 ...

随机推荐

  1. React源码解析:ReactElement

    ReactElement算是React源码中比较简单的部分了,直接看源码: var ReactElement = function(type, key, ref, self, source, owne ...

  2. 深入浅出docker

    笔者在海外工作多年,所以文中多用英文单词,有些时候是为了更精准的描述,请见谅.希望这篇随笔能帮大家入门docker.由于在海外连博客园有些慢,所以我图片用的比较少,以后再考虑一下如何更好的解决图片上传 ...

  3. 解决vue路径中#号

    在router文件夹下的js文件中,更改配置增加 mode: 'history'; vue-router官方文档:https://router.vuejs.org/zh-cn/essentials/h ...

  4. PID算法笔记2

    总所周知,PID算法是个很经典的东西.而做自平衡小车,飞行器PID是一个必须翻过的坎.因此本节我们来好好讲解一下PID,根据我在学习中的体会,力求通俗易懂.并举出PID的形象例子来帮助理解PID.一. ...

  5. System包含的信息

    System类中的属性值 System.getProperty()方法大全 System.out.println("java版本号:" + System.getProperty(& ...

  6. Servlet和web服务器关系

    前面的博客我详细的罗列了下Servlet的常用的类和接口,然后在前面的前面我类似tomcat模拟了一套web服务器,这里来做一个统一的整理,这样子可以更好的把握Servlet,也可以更好的了解下web ...

  7. SQL Server中的变更捕获技术--简单部署

    ------准备------ CREATE DATABASE db_test_cdc ,) ,name )); INSERT INTO t1(name)VALUES('test') ------开始- ...

  8. 常用Nagios配置命令

    cd /usr/local/nagios/etc vim nagios.cfg /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios ...

  9. HTML——filedset和legend标签

    1.<filedset>定义围绕表单中元素的边框. 2.legend 元素表示作为 legend 元素的父元素的 fieldset 元素的其余内容的标题(caption). 使用案例: & ...

  10. typedef如何显示变量类型名

    dsa typedef unsigned long int NUM; typedef unsigned short int Data; //为已经存在的类型起一个别名 //1.定义一个变量 unsig ...