## 創建圖像
1. 設定header,告訴瀏覽器你要生成的mime類型
~~~
header('Content-Type:image/png');
~~~
2. 創建一個圖像區域
默認黑色背景
~~~
$png=imagecreatetruecolor(300, 300);
~~~
3. 在空白圖片區域繪制填充背景
~~~
$blue=imagecolorclosest($png, 0, 102, 255);
imagefill($png, 0, 0, $blue);
~~~
4. 在背景上繪制圖像輪廓,輸入文本
~~~
$white=imagecolorclosest($png, 255,255,255);
imageline($png, 0,0, 100, 50, $white);
imageline($png, 0,0, 50, 100, $white);
imageline($png, 0,0, 200, 200, $white);
imagestring($png, 5, 100, 100, 'tianwei', $white);
~~~
5. 輸出圖形
~~~
imagepng($png);
~~~
6. 清楚所有資源
~~~
imagedestroy($png);
~~~
7. 其他頁面調用圖像
~~~
header('Content-Type:text/html;charset="utf-8');
echo '<img src="index.php" title="圖形">';
~~~
### 簡單驗證碼
~~~
for ($i=0; $i <4 ; $i++) {
@$imgs.=dechex(mt_rand(0,15));
}
header('Content-Type:image/png');
$im=imagecreatetruecolor(100, 100);
$blue=imagecolorclosest($im, 0, 100, 100);
$white=imagecolorclosest($im, 255,255,255);
imagefill($im, 0, 0, $blue);
imagestring($im, 4, 20, 20, $imgs, $white);
imagepng($im);
imagedestroy($im);
~~~
### 中文轉碼
$text=iconv('gbk','utf-8','田偉');
### 已有圖片加水印
~~~
header('Content-Type:image/png');
$im=imagecreatefrompng('1.png');
$white=imagecolorclosest($im, 0, 0, 0);
$ft='C:\Windows\Fonts\BAUHS93.TTF';
imagettftext($im,20,0,30,30,$white,$ft,'hollo');
imagepng($im);
imagedestroy($im);
~~~
### 縮略圖
1. 取得原圖的尺寸
2. 創建新圖
3. 把原圖載入新圖
4. 執行復制,重新采樣
5. 將圖像輸出