php-gd库的使用——跟招财圆一起玩php(1)

  原本我是做c#的,因为单位原因转行做了php。一直处于自己摸索,有问题百度,各种群解决自己问题的状态。最近职业进入倦怠期,也恰巧比较闲。准备成体系的更新和复习下自己的知识技能。这是这个分类的第一篇文章,希望按照每周两篇的博文速度。 编辑总结php各种知识点。

 

  今天我们先来玩图片。本系列文章提纲如下

  1. 画条直线写点字,顺便生成验证码
  2. 画画曲线画画图,加个水印旋个转
  3. 图要好看,样式来配
  4. 总结

1. 画条直线写点字,顺便生成验证码

  画直线应该是画图中最基本的功能,因此我们从这里开始玩。  首先学习一下基本的画图步骤,认识几个函数。  本文就不把gd库里那一大堆的函数列举了,选最具代表性的来玩以节省篇幅。画图步骤简化为如下

  看起来好奇怪,那就来说人话

<?php
    header("Content-type: image/png");
    //1. 创建
    $im = @imagecreate(300, 300) or die("Cannot Initialize new GD image stream");
    $background_color  = imagecolorallocate($im, 0, 0, 0);
    $red = imagecolorallocate($im, 255, 0, 0);
    //2. 画图
    imageline($im, 0, 0, 150, 150, $red);
    //3. 输出
    imagepng($im);
    //4. 销毁
    imagedestroy($im);
?>

运行结果如下图所示,创建了黑色背景的画布,画了一条红色的线

  用imagecreate创建画布,imagecolorallocate设置颜色,imageline画直线,imagepng输出,最后imagedestroy销毁。  这里有如果对php有了解的同学应该会有疑问:为什么要销毁? 虽然当php脚本结束的时候,会自动销毁所有资源。但是在脚本中我们手动释放资源,可以保证脚本运行过程中不占用不必要的资源。

 接着来写几个字。在原有代码的基础上,加入一行代码。写几个字
 1     header("Content-type: image/png;charset=UTF-8");
 2     //1. 创建
 3     $im = @imagecreate(300, 300) or die("Cannot Initialize new GD image stream");
 4     $background_color  = imagecolorallocate($im, 0, 0, 0);
 5     $red    = imagecolorallocate($im, 255, 0, 0);
 6     //2.1 写字  为什么我不用汉字玩?
 7     imagestring($im, 3, 150, 150, 'why not use chinese?', $red);
 8     //2.2 画直线
 9     imageline($im, 0, 0, 150, 150, $red);
10     //3. 输出
11     imagepng($im);
12     //4. 销毁
13     imagedestroy($im);

  运行效果如下

  为什么不用汉字呢?imagestring使用点阵字库,如果你真的要用他的话需要自己构造中文的点阵字库,然后用imageloadfont来载入。
更推荐和常用的做法是使用imagettftext函数

  第一个应用 来个验证码 思路 创建一个固定长宽的画布 ,把随机数画上画布然后输出即可。 为了避免被很容易的程序识别,加入噪点,再画几条颜色的乱线。这里有一个不错的验证码类。放在这里

 1 <?php
 2 //验证码类
 3 class ValidateCode {
 4      private $charset = 'abcdefghkmnprstuvwxyzABCDEFGHKMNPRSTUVWXYZ23456789';//随机因子
 5      private $code;//验证码
 6      private $codelen = 4;//验证码长度
 7      private $width = 130;//宽度
 8      private $height = 50;//高度
 9      private $img;//图形资源句柄
10      private $font;//指定的字体
11      private $fontsize = 20;//指定字体大小
12      private $fontcolor;//指定字体颜色
13 
14      //构造方法初始化
15      public function __construct() {
16       $this->font = dirname(__FILE__).'/Elephant.ttf';//注意字体路径要写对,否则显示不了图片
17      }
18 
19      //生成随机码
20      private function createCode() {
21       $_len = strlen($this->charset)-1;
22       for ($i=0;$i<$this->codelen;$i++) {
23        $this->code .= $this->charset[mt_rand(0,$_len)];
24       }
25      }
26 
27      //生成背景
28      private function createBg() {
29       $this->img = imagecreatetruecolor($this->width, $this->height);
30       $color = imagecolorallocate($this->img, mt_rand(157,255), mt_rand(157,255), mt_rand(157,255));
31       imagefilledrectangle($this->img,0,$this->height,$this->width,0,$color);
32      }
33  //生成文字
34  private function createFont() {
35   $_x = $this->width / $this->codelen;
36   for ($i=0;$i<$this->codelen;$i++) {
37    $this->fontcolor = imagecolorallocate($this->img,mt_rand(0,156),mt_rand(0,156),mt_rand(0,156));
38    imagettftext($this->img,$this->fontsize,mt_rand(-30,30),$_x*$i+mt_rand(1,5),$this->height / 1.4,$this->fontcolor,$this->font,$this->code[$i]);
39   }
40  }
41  //生成线条、雪花
42  private function createLine() {
43       //线条
44       for ($i=0;$i<6;$i++) {
45        $color = imagecolorallocate($this->img,mt_rand(0,156),mt_rand(0,156),mt_rand(0,156));
46        imageline($this->img,mt_rand(0,$this->width),mt_rand(0,$this->height),mt_rand(0,$this->width),mt_rand(0,$this->height),$color);
47       }
48       //雪花
49       for ($i=0;$i<100;$i++) {
50        $color = imagecolorallocate($this->img,mt_rand(200,255),mt_rand(200,255),mt_rand(200,255));
51        imagestring($this->img,mt_rand(1,5),mt_rand(0,$this->width),mt_rand(0,$this->height),'*',$color);
52       }
53      }
54      //输出
55      private function outPut() {
56       header('Content-type:image/png');
57       imagepng($this->img);
58       imagedestroy($this->img);
59      }
60 
61      //对外生成
62      public function doimg() {
63       $this->createBg();
64       $this->createCode();
65       $this->createLine();
66       $this->createFont();
67       $this->outPut();
68      }
69      
70      //获取验证码
71      public function getCode() {
72       return strtolower($this->code);
73      }
74 }
75 ?>
View Code

  今天就写到这里,下一篇加个水印旋转下

posted @ 2017-01-08 11:45  招财圆  阅读(349)  评论(0编辑  收藏  举报