php課程 8-28 php如何繪制生成顯示圖片
一、總結
一句話總結:gd庫輕松解決
?
1、php圖片操作生成的圖的兩種去向是什么?
一種在頁面直接輸出,一種存進本地磁盤
?
2、php操作圖片的庫有哪些?
PHP: Image Processing and Generation - Manual
http://php.net/manual/en/refs.utilspec.image.php
Cairo
Exif
GD
Gmagick
ImageMagick
?
3、如何防止網站強刷注冊?
a、驗證碼
b、ip限制,比如這個ip只能注冊30個用戶,多了就讓你明天再來
?
4、如何防止網站暴力破解用戶名?
記錄用戶名,如果輸入錯5次,那只能明天再來登錄
第一次沒有驗證碼,輸入出錯之后驗證碼就出來
?
5、網站上面圖片的縮放、裁剪、加水印是怎么實現的?
交給的php,gd庫輕松解決
?
6、php中常見的資源類型有哪些?
數據庫連接資源
gd庫畫布資源
?
7、php變量或者說資源的釋放是什么樣的?
一個腳本執行完,執行到最后之后,里面的所有變量或者說資源都會被刪除,所以多個php腳本的重復變量是不會相互影響的。
?
8、html如何聲明本文檔是圖片?
header頭中content-type
header('content-type:image/jpeg');
?
9、gd庫的使用需要額外引包么?
不需要,直接進去就開始干就行了,比如說創建畫布資源
1 <?php
2 // 1.創建畫布資源
3 $img=imagecreatetruecolor(500,300); 4 5 // 2.準備顏色 6 $black=imagecolorallocate($img,0,0,0); 7 $white=imagecolorallocate($img,255,255,255); 8 $red=imagecolorallocate($img,255,0,0); 9 $green=imagecolorallocate($img,0,255,0); 10 $blue=imagecolorallocate($img,0,0,255); 11 12 // 3.填充畫布 13 imagefill($img,0,0,$black); 14 15 // 4.在畫布上畫圖像或文字 16 imagefilledellipse($img,250,150,200,200,$white); 17 18 // 5.輸出最終圖像或保存最終圖像 19 header('content-type:image/jpeg'); 20 21 // 圖片從瀏覽器上輸出 22 imagejpeg($img); 23 24 // 把圖片保存到本地 25 // imagejpeg($img,'jin.jpg'); 26 27 // 6.釋放畫布資源 28 imagedestroy($img); 29 30 ?>
?
10、php中gd畫圖的場景有哪些?
1.驗證碼
2.縮放
3.裁剪
4.水印
?
11、php中創建圖像的六個步驟是什么?
1.創建畫布資源
$img=imagecreatetruecolor(500,300);
2.準備顏色
$black=imagecolorallocate($img,0,0,0);
$white=imagecolorallocate($img,255,255,255);
$red=imagecolorallocate($img,255,0,0);
$green=imagecolorallocate($img,0,255,0);
$blue=imagecolorallocate($img,0,0,255);
3.填充畫布
imagefill($img,0,0,$black);
4.在畫布上畫圖像或文字
imagefilledellipse($img,250,150,200,200,$white);
5.輸出最終圖像或保存最終圖像
header('content-type:image/jpeg');
1)圖片從瀏覽器上輸出
imagejpeg($img);
2)把圖片保存到本地
imagejpeg($img,'jin.jpg');
6.釋放畫布資源
imagedestroy($img);
1 <?php
2 // 1.創建畫布資源
3 $img=imagecreatetruecolor(500,300); 4 5 // 2.準備顏色 6 $black=imagecolorallocate($img,0,0,0); 7 $white=imagecolorallocate($img,255,255,255); 8 $red=imagecolorallocate($img,255,0,0); 9 $green=imagecolorallocate($img,0,255,0); 10 $blue=imagecolorallocate($img,0,0,255); 11 12 // 3.填充畫布 13 imagefill($img,0,0,$black); 14 15 // 4.在畫布上畫圖像或文字 16 imagefilledellipse($img,250,150,200,200,$white); 17 18 // 5.輸出最終圖像或保存最終圖像 19 header('content-type:image/jpeg'); 20 21 // 圖片從瀏覽器上輸出 22 imagejpeg($img); 23 24 // 把圖片保存到本地 25 // imagejpeg($img,'jin.jpg'); 26 27 // 6.釋放畫布資源 28 imagedestroy($img); 29 30 ?>
?
?
二、php如何繪制生成顯示圖片
1、相關知識
php中gd畫圖的場景:
1.驗證碼
2.縮放
3.裁剪
4.水印
php中創建圖像的六個步驟:
1.創建畫布資源
$img=imagecreatetruecolor(500,300);
2.準備顏色
$black=imagecolorallocate($img,0,0,0);
$white=imagecolorallocate($img,255,255,255);
$red=imagecolorallocate($img,255,0,0);
$green=imagecolorallocate($img,0,255,0);
$blue=imagecolorallocate($img,0,0,255);
3.填充畫布
imagefill($img,0,0,$black);
4.在畫布上畫圖像或文字
imagefilledellipse($img,250,150,200,200,$white);
5.輸出最終圖像或保存最終圖像
header('content-type:image/jpeg');
1)圖片從瀏覽器上輸出
imagejpeg($img);
2)把圖片保存到本地
imagejpeg($img,'jin.jpg');
6.釋放畫布資源
imagedestroy($img);
繪制圖像:
? imagefill();?? //區域填充
? imagesetpixel();? //畫一個像素
? imageline();??? //畫一條線
? imagerectangle();?? //畫一個矩形
? imagefilledrectangle();?? //畫一矩形并填充
? imagepolygon();???? //畫一個多邊形
? imagefilledpolygon();? //畫一個多邊形并填充
? imageellipse();? //畫一個橢圓
? imagefilledellipse();??? //畫一個橢圓并填充
? imagearc();??? //畫一個橢圓弧
? imagefilledarc();? //畫一個橢圓弧并填充
? imagestring();?? //水平地畫一行字符串
? imagestringup();? //垂直地畫一行字符串
? imagechar();?? //水平地畫一個字符
? imagecharup();?? //垂直地畫一個字符
? imagettftext();? //用truetype字符向圖像畫一個字符串
?
2、代碼
1 <?php 2 // 1.創建畫布資源 3 $img=imagecreatetruecolor(500,300); 4 5 // 2.準備顏色 6 $black=imagecolorallocate($img,0,0,0); 7 $white=imagecolorallocate($img,255,255,255); 8 $red=imagecolorallocate($img,255,0,0); 9 $green=imagecolorallocate($img,0,255,0); 10 $blue=imagecolorallocate($img,0,0,255); 11 12 // 3.填充畫布 13 imagefill($img,0,0,$black); 14 15 // 4.在畫布上畫圖像或文字 16 imagefilledellipse($img,250,150,200,200,$white); 17 18 // 5.輸出最終圖像或保存最終圖像 19 header('content-type:image/jpeg'); 20 21 // 圖片從瀏覽器上輸出 22 imagejpeg($img); 23 24 // 把圖片保存到本地 25 // imagejpeg($img,'jin.jpg'); 26 27 // 6.釋放畫布資源 28 imagedestroy($img); 29 30 ?>
?
?
?
?