php--第二周

  本周看了一些设计模式,看到观察者模式后实在是看不下去了,以后换种方式重新学习设计模式。本周集中看了一些零碎的知识点。

  第一个,单例。

  这也是曾经在一次课程设计中,为了保证订单号唯一性用到了这种方式。单例,即无法从其自身外部来创建实例。

class Preferences{
    private $props = array();
    public static $instance;

    private function __construct(){}

    public function setProperty($key,$val){
        $this->props[$key] = $val;
    }

    public function getProperty($key){
        return $this->props[$key];
    }

    public static function getInstance(){
        if(empty(self::$instance)){
            self::$instance = new Preferences();
        }
        return self::$instance;
    }
}

  该类的构造函数为私有,即不能直接实例化,并定义一个静态变量,用来存储已经实例化的类,这里利用getInstance来获取。

$prefer1 = Preferences::getInstance();
$prefer1->setProperty('user','jey');
echo $prefer1->getProperty('user');

unset($prefer1);
$prefer2 = Preferences::getInstance();
echo $prefer2->getProperty('user');

  第二个,GD库之验证码。

  php操作图像,并输出。当然需要开展拓展php_gd2。里面包含的方法有很多,这里了解了基本的一些操作流程。

  首先我们需要设置一个图像,可以比喻为定义一个画板--resource imagecreatetruecolor(int $width,int $height),此时如果输出可以看见图像为全黑的;

  因此如果我们希望让它变为白色,则可以画一个矩形填充--imagefilledrectangle($resource,$x0,$y0,$x1,$y1,$color),分别从($x0,$y0)到($x1,$y1),填充$color;

  这里的$color,是颜色,创建的方式需要利用函数--int $color imageallocate($resource,$red,$green,$blue);

  之后如果我们想在画板上写字符可以利用--imagechar($resource,$fontsize,$x,$y,$str,$color),这里的坐标指的是左上角,这里的方向为横向,如果是竖向的话则用imagecharup,$fontsize为1到5,如果想用其他字体,其他字号则用另外一个函数,imagettftext($resource,$fontsize,$angle,$x,$y,$color,$fontfile,$str);

  之后通过imagepng($resource),输出图像,如果想保存的话则添加第二个参数,为保存地址;

  最后销毁该资源,imagedestroy($resource)。

  最后上一个实例:

  1 class VerifyCode{
  2     private $width;
  3     private $height;
     //字体文件
4 private $fontFile; 5 private $type; 6 private $size; 7 private $image; 8 private $text; 9 private $fontSize = 20; 10 private $disturbPixel = 50; 11 private $disturbLine = 2; 12 private $disturbCircle = 2; 13 const VERIFY_NUMBER = 1; 14 const VERIFY_LETTER = 2; 15 const VERIFY_MIX = 3; 16 const VERIFY_CHARACTERS = 4; 17 18 function __construct($width = 200, $height = 50, $fontFile, $type = VerifyCode::VERIFY_CHARACTERS, $size = 4){ 19 $this->width = $width; 20 $this->height = $height; 21 $this->fontFile = $fontFile; 22 $this->type = $type; 23 $this->size = $size; 24 $this->image = imagecreatetruecolor($this->width,$this->height); 25 } 26 27 //绘制验证码 28 function getImage(){ 29 //获得白色画笔 30 $whiteBack = imagecolorallocate($this->image,255,255,255); 31 //填充为白色 32 imagefilledrectangle($this->image,0,0,$this->width,$this->height,$whiteBack); 33 //绘制字符串 34 $textArr = $this->getText(); 35 session_start(); 36 $_SESSION['verifyCode'] = $this->text; 37 setcookie('verifyCode',$_SESSION['verifyCode'],time()+30); 38 //绘制字符 39 for($i=0; $i<$this->size; $i++){ 40 imagettftext($this->image,$this->fontSize,mt_rand(-30,30), 41 30+(imagefontwidth($this->fontSize)+20)*$i, 42 mt_rand(30,$this->height-10), 43 $this->getRandColor(), 44 $this->fontFile, 45 $textArr[$i] 46 ); 47 } 48 } 49 50 //添加干扰元素 51 function addDisturb(){
       //添加像素点
52 for($i=0; $i<$this->disturbPixel;$i++){ 53 imagesetpixel($this->image,mt_rand(0,$this->width),mt_rand(0,$this->height),$this->getRandColor()); 54 }
       //画直线,起点,终点
55 for($i=0; $i<$this->disturbLine;$i++){ 56 imageline($this->image,mt_rand(0,$this->width),mt_rand(0,$this->height),mt_rand(0,$this->width),mt_rand(0,$this->height),$this->getRandColor()); 57 }
       //画弧线,中心坐标,起始点坐标,旋转起点,旋转终点
58 for($i=0; $i<$this->disturbCircle;$i++){ 59 imagearc($this->image,mt_rand(0,$this->width),mt_rand(0,$this->height),mt_rand(0,$this->width),mt_rand(0,$this->height),mt_rand(0,360),mt_rand(0,360),$this->getRandColor()); 60 } 61 } 62 63 //输出 64 function printImage(){ 65 $this->getImage(); 66 $this->addDisturb();
       //告诉浏览器输出图像
67 header('Content-Type:image/png'); 68 imagepng($this->image); 69 imagedestroy($this->image); 70 } 71 72 //获取验证码字符串 73 function getText(){ 74 switch ($this->type) { 75 case VerifyCode::VERIFY_NUMBER: 76 $textArr = array_rand(array_flip(range(0, 9)), $this->size); 77 break; 78 case VerifyCode::VERIFY_LETTER: 79 $textArr = array_rand(array_flip(array_merge(range('a', 'z'), range('A', 'Z'))), $this->size); 80 break; 81 case VerifyCode::VERIFY_CHARACTERS: 82 $characters = '我们感觉不到地球转动的原因同上述事件类似如果地球自转突然加速或减速那么你一定会感觉到地球的恒速旋转旋转使我们的祖先对于宇宙的真相感到很困惑他们注意到星星太阳和月亮似乎都在地球上空移动因为感觉不到地球运动所以逻辑地推理地球是静止的整个天空都在围绕着地球转动早期的希腊科学家阿利斯塔克在公元前几百年前首先提出了日心说宇宙模型那个时候世界上伟大的思想家坚持宇宙地心说已经好几个世纪了直到世哥白尼的日心模型才开始被讨论和理解虽然也有错误之处哥白尼的模型最终证明了在这个世界地球是在恒星下面依照自己的轴线转动当然也在太阳周围的轨道内转动'; 83 $arr = $this->ch2arr($characters); 84 $textArr = array_rand(array_flip($arr), $this->size); 85 break; 86 case VerifyCode::VERIFY_MIX: 87 $textArr = array_rand(array_flip(array_merge(range(0, 9), range('a', 'z'), range('A', 'Z'))), $this->size); 88 break; 89 default: 90 $textArr = array_rand(array_flip(range(0, 9)), $this->size); 91 break; 92 } 93 shuffle($textArr); 94 $this->text = join('',$textArr); 95 return $textArr; 96 } 97 98 //获取随机颜色 99 function getRandColor(){ 100 return imagecolorallocate($this->image,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255)); 101 } 102 103 private function ch2arr($str){ 104 $length = mb_strlen($str, 'utf-8'); 105 for($i=0; $i<$length; $i++){ 106 $arr[] = mb_substr($str,$i,1,'utf-8'); 107 } 108 return array_unique($arr); 109 } 110 } 111 $verifyCode = new VerifyCode(200,50,__DIR__.DIRECTORY_SEPARATOR.'codefont.ttc',VerifyCode::VERIFY_MIX,5); 112 $verifyCode->printImage();
浏览器输出:

  第三个,获取文件。有一个场景就是我们一个文件目录下的所有文件目录,有这个函数--scandir。

//递归获取目录下所有层级文件
function
getAll($mainDir){ $result = scandir($mainDir); $result = array_filter($result,function($item){ return $item != '.' && $item != '..'; }); echo "<pre>"; print_r ($result); echo "</pre>"; foreach($result as $item){ if($item=='.' || $item=='..') continue; if(is_dir($mainDir.DIRECTORY_SEPARATOR.$item)){ getAll($mainDir.DIRECTORY_SEPARATOR.$item); } } }

  第四个,关于文件fopen(),经常混淆打开方式。

  r,只读方式打开;

  r+,读写方式打开,指针指向文件头,将覆盖写入;

  w,写入方式打开,指针指向文件头并直接将文件大小截至为0,文件不存在则创建;

  w+,读写方式打开,其他与w相同;

  a,与w相同,只是文件指针指向末尾,并不截至文件大小为0;

  a+,读写方式打开,其他与a相同;

  

posted @ 2017-02-27 15:43  jeyfang  阅读(125)  评论(0编辑  收藏  举报