php-gd庫的使用——跟招財圓一起玩php(1),php-gd
原本我是做c#的,因為單位原因轉行做了php。一直處於自己摸索,有問題百度,各種群解決自己問題的狀態。最近職業進入倦怠期,也恰巧比較閒。准備成體系的更新和復習下自己的知識技能。這是這個分類的第一篇文章,希望按照每周兩篇的博文速度。 編輯總結php各種知識點。
今天我們先來玩圖片。本系列文章提綱如下
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);
?>
運行結果如下圖所示,創建了黑色背景的畫布,畫了一條紅色的線
為什麼要銷毀? 雖然當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
今天就寫到這裡,下一篇加個水印旋轉下