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

<?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,理解扩展安装原理
ubuntu:通过封装验证码类库一步步安装php的gd扩展的更多相关文章
- 将当前的Ubuntu系统封装成为可以安装(发布)的iso镜像
将当前的Ubuntu系统封装成为可以安装(发布)的iso镜像 在使用以上方法安装依赖的时候xresprobe 会找不到安装地址,采用下面的方式: Package xresprobe is not in ...
- Ubuntu学习总结-01 用VMware 8安装Ubuntu 12.04详细过程
1 Ubuntu 下载地址 http://www.ubuntu.com/download/desktop 2 安装Ubuntu 转载用VMware 8安装Ubuntu 12.04详细过程 http:/ ...
- Ubuntu安装MongoDB和PHP扩展
MongoDB是一个可伸缩的,高性能的开源NoSQL 文档数据库.主要用C++开发完成.面向文档存储,全索引支持,可复制和高可用性,自动分片等特征.其在非关系型数据库中是功能最丰富,最像关系型数据库 ...
- 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- ...
- ubuntu常用命令及操作,包括安装CUDA
chmod Document 这里Document是一个文件夹,文件夹中还有好多子文件,可以发现执行了这条指令以后,其子文件夹的权限并没有改变. 要想改变其子文件夹的权限,应该执行 Document/ ...
- ubuntu 用remastersys 备份系统并且安装
sudo add-apt-repository ppa:mutse-young/remastersys 2.更新系统软件源 sudo apt-get update 3.更新完了,先安装remaster ...
- Linux(Ubuntu)使用 sudo apt-get install 命令安装软件的目录在哪?(已解决)
Linux(Ubuntu)使用 sudo apt-get install 命令安装软件的目录在哪? bin文件路径: /usr/bin 库文件路径: /usr/lib/ 其它的图标啊什么的路径 ...
- ubuntu 12.04及12.10无法安装 ia32-libs
administrator@ubuntu:~$ sudo apt-get install ia32-libs [sudo] password for administrator: 正在读取软件包列表 ...
- Ubuntu 16.04下EasyOpenJTAG+OpenOCD的安装和使用【转】
本文转载自:http://www.linuxdiyf.com/linux/24086.html Ubuntu 16.04下EasyOpenJTAG+OpenOCD的安装和使用 发布时间:2016-09 ...
随机推荐
- 邓_thinkphp口试
描述php框架开发 通过提供一个开发Web程序的基本架构,PHP开发框架把PHPWeb程序开发摆到了流水线上.换句话说,PHP开发框架有助于促进快速软件开发(RAD),这节约了你的时间,有助于创建更为 ...
- Shell中$X的含义
$0 表示这个程序的执行名字,包含输入参数$n 表示这个程序的第n个参数值$* 表示这个程序的所有参数,此选项参数可超过9个.$# 表示这个程序的参数个数$$ 表示这个程序的PID(脚本运行的当 ...
- 语句、变量等js最基本知识
JavaScript的最为基本知识 1语法 js是区分大小写的:标识符就是指变量.函数.属性的名字或者是参数,标识符可以是字母,下划线,美元符号,数字,注意第一个不能是数字:js采用的是驼峰大小格式: ...
- IOS学习:隐藏键盘方法
1.点击界面的其它空白地方隐藏 由于UIViewController是继承自UIResponder的,所以可以覆写- (void)touchesBegan:(NSSet *)touches ...
- [原创]消灭eclipse中运行启动的错误:“找不到或无法加载主类”问题
最近一直遇到这个问题且根据网上的文章做法基本无法通过,故将自己的解决步骤记录及分享给大家. 一:环境必须要配置好. 试试在dos界面输入:java.javac 分别这两个命令是否能执行,如果都能执行恭 ...
- spring 事务隔离级别配置
声明式的事务处理中,要配置一个切面,即一组方法,如 其中就用到了propagation,表示打算对这些方法怎么使用事务,是用还是不用,其中propagation有七种配置,REQUIRED.SUPPO ...
- iOS 的ipa 包重新签名
https://www.evernote.com/l/As7sxCnA85JCs7bn5Tg5St003gXYYslAk3k
- 【原创】区分png图片格式和apng图片格式的解决办法
最近公司有个项目,要抓取客户微信公众号的文章,以及文章内容中的图片,并且在图片加上客户自己的水印.我们使用阿里云OSS存储图片和加水印,发现真心好用,提升了我们的开发效率,阿里云现在是越来越强大了.. ...
- 006-接收键盘的输入(read)
read -ptns 变量名 -p 在等待read输入的时候,显示的提示信息 -t 秒数,read等待用户输入的时间 -n read接收用户输入的字符数,只接收指定字符数,就会执行 -s 隐藏输 ...
- 在 ios 中的日期格式
var d="2017-1-1" ; new Date(d) //生成一个日期对象 这样写在 Android 中没有问题,但是在 ios 中,d 的格式不对,应该设为 2017- ...