文章主要介紹了PHP圖像處理技術(shù),結(jié)合實(shí)例形式總結(jié)分析了php繪圖、水印、驗(yàn)證碼、圖像壓縮等相關(guān)函數(shù)、功能與圖形繪制實(shí)現(xiàn)技巧,需要的朋友可以參考下:
1、繪圖
場景: 驗(yàn)證碼、圖像水印、圖像壓縮處理
php繪圖坐標(biāo)體系是從0,0點(diǎn)越向右值越大,越向下值越大
需要開啟php的gd2擴(kuò)展 php.ini 中
參數(shù)1:圖像資源(畫布)
參數(shù)2:開始的x軸坐標(biāo)
參數(shù)3:開始的y軸坐標(biāo)
參數(shù)4:結(jié)束的x軸坐標(biāo)
參數(shù)5:結(jié)束的y軸坐標(biāo)
參數(shù)6:線條的顏色
(1)繪制線條:imageline($p1, $p2, $p3, $p4, $p5, $6)
(2)繪制三角形:imageline($p1, $p2, $p3, $p4, $p5, $6)// 需要3次
(3)繪制矩形:imagerectangle($p1, $p2, $p3, $p4, $p5, $6)
(3.1)繪制并填充矩形:imagefilledrectangle($p1, $p2, $p3, $p4, $p5, $6)
(4)繪制橢圓:imageellipse($p1, $p2, $p3, $p4, $p5, $6)
(4.1)繪制并填充橢圓:imagefilledellipse($p1, $p2, $p3, $p4, $p5, $6)
參數(shù)1:目標(biāo)圖像
參數(shù)2:原始圖像
參數(shù)3:目標(biāo)圖像坐標(biāo)x
參數(shù)4:目標(biāo)圖像坐標(biāo)y
參數(shù)5:原始圖像開始坐標(biāo)x
參數(shù)6:原始圖像開始坐標(biāo)y
參數(shù)7:原始圖像寬度
參數(shù)8:原始圖像高度
(5)將圖片繪制到畫布上:imagecopy ( $p1, $p2, $p3, $p4, $p5, $6, $7, $8)
參數(shù)1:目標(biāo)圖像
參數(shù)2:字體 1,2,3,4 或 5,則使用內(nèi)置字體
參數(shù)3:目標(biāo)圖像坐標(biāo)x
參數(shù)4:目標(biāo)圖像坐標(biāo)y
參數(shù)5:字符,文字
參數(shù)6:顏色
(6)繪制字符串:imagestring( $p1, $p2, $p3, $p4, $p5, $6)// 向畫布寫入字符,文字
參數(shù)1:圖像資源
參數(shù)2:字體大小
參數(shù)3:傾斜角度
參數(shù)4:x軸坐標(biāo)
參數(shù)5:y軸坐標(biāo)
參數(shù)6:字體顏色
參數(shù)7:字體文件
參數(shù)8:文字
(7)繪制中文:imagettftext($p1, $p2, $p3, $p4, $p5, $6, $7, $8)
參數(shù)1:圖像資源
參數(shù)2:弧形開始x坐標(biāo)
參數(shù)3:弧形開始y坐標(biāo)
參數(shù)4:弧形寬度
參數(shù)5:弧形高度
參數(shù)6:弧形開始角度
參數(shù)7:弧形結(jié)束角度
參數(shù)8:繪圖顏色
(8)繪制弧形:imagearc($p1, $p2, $p3, $p4, $p5, $6, $7, $8)// 三點(diǎn)鐘的位置是起點(diǎn)(0度), 順時(shí)針方向繪畫
實(shí)例 - 弧形?
// 創(chuàng)建一個(gè) 200X200 的圖像
$img
= imagecreatetruecolor(200, 200);
// 分配顏色
$white
= imagecolorallocate(
$img
, 255, 255, 255);
$black
= imagecolorallocate(
$img
, 0, 0, 0);
// 畫一個(gè)黑色的圓
imagearc(
$img
, 100, 100, 150, 150, 0, 360,
$black
);
// 將圖像輸出到瀏覽器
header(
"Content-type: image/png"
);
imagepng(
$img
);
// 釋放內(nèi)存
imagedestroy(
$img
);
參數(shù)1:圖像資源
參數(shù)2:弧形開始x坐標(biāo)
參數(shù)3:弧形開始y坐標(biāo)
參數(shù)4:弧形寬度
參數(shù)5:弧形高度
參數(shù)6:弧形開始角度
參數(shù)7:弧形結(jié)束角度
參數(shù)8:繪圖顏色
參數(shù)9:填充樣式
- IMG_ARC_PIE: 用直線連接產(chǎn)生圓形邊界
- IMG_ARC_CHORD: 用直線連接了起始和結(jié)束點(diǎn)
- IMG_ARC_NOFILL: 明弧或弦只有輪廓,不填充
- IMG_ARC_EDGED:用直線將起始和結(jié)束點(diǎn)與中心點(diǎn)相連,和 IMG_ARC_NOFILL 一起使用是畫餅狀圖輪廓的好方法(而不用填充)
(9)繪制弧形并填充:imagefilledarc($p1, $p2, $p3, $p4, $p5, $6, $7, $8, $9)// 三點(diǎn)鐘的位置是起點(diǎn)(0度), 順時(shí)針方向繪畫
實(shí)例 - 弧形填充?
// 創(chuàng)建圖像
$image
= imagecreatetruecolor(100, 100);
// 分配一些顏色
$white
= imagecolorallocate(
$image
, 0xFF, 0xFF, 0xFF);
$gray
= imagecolorallocate(
$image
, 0xC0, 0xC0, 0xC0);
$darkgray
= imagecolorallocate(
$image
, 0x90, 0x90, 0x90);
$navy
= imagecolorallocate(
$image
, 0x00, 0x00, 0x80);
$darknavy
= imagecolorallocate(
$image
, 0x00, 0x00, 0x50);
$red
= imagecolorallocate(
$image
, 0xFF, 0x00, 0x00);
$darkred
= imagecolorallocate(
$image
, 0x90, 0x00, 0x00);
// 創(chuàng)建 3D 效果
for
(
$i
= 60;
$i
> 50;
$i
--) {
imagefilledarc(
$image
, 50,
$i
, 100, 50, 0, 45,
$darknavy
, IMG_ARC_PIE);
imagefilledarc(
$image
, 50,
$i
, 100, 50, 45, 75 ,
$darkgray
, IMG_ARC_PIE);
imagefilledarc(
$image
, 50,
$i
, 100, 50, 75, 360 ,
$darkred
, IMG_ARC_PIE);
}
imagefilledarc(
$image
, 50, 50, 100, 50, 0, 45,
$navy
, IMG_ARC_PIE);
imagefilledarc(
$image
, 50, 50, 100, 50, 45, 75 ,
$gray
, IMG_ARC_PIE);
imagefilledarc(
$image
, 50, 50, 100, 50, 75, 360 ,
$red
, IMG_ARC_PIE);
// 輸出圖像
header(
'Content-type: image/png'
);
imagepng(
$image
);
imagedestroy(
$image
);
效果
2、水印
使用imagestring()或者imagettftext()
實(shí)例 - 圖片加字?
// 建立一幅 100X30 的圖像
$im
= imagecreate(100, 30);
// 白色背景和藍(lán)色文本
$bg
= imagecolorallocate(
$im
, 255, 255, 255);
$textcolor
= imagecolorallocate(
$im
, 0, 0, 255);
// 把字符串寫在圖像左上角
imagestring(
$im
, 5, 0, 0,
"Hello world!"
,
$textcolor
);
// 輸出圖像
header(
"Content-type: image/png"
);
imagepng(
$im
);
- PHP實(shí)現(xiàn)高清晰度無損圖片壓縮功能的代碼
- 用PHP處理png圖片白色背景色改為透明色的實(shí)例代碼
- 關(guān)于PHP往mysql數(shù)據(jù)庫中批量插入數(shù)據(jù)實(shí)例教程
- Php兩點(diǎn)地理坐標(biāo)距離的計(jì)算方法和具體代碼
- PHP獲取HTTP body內(nèi)容的方法
- PHP面向?qū)ο蟪绦蛟O(shè)計(jì)中獲取對(duì)象屬性的3種方法實(shí)例分析
- php5.5新增的yield關(guān)鍵字功能與相關(guān)使用技巧
- Windows7下IIS+php配置教程詳細(xì)介紹
- PHP序列化的四種實(shí)現(xiàn)方法與橫向?qū)Ρ冉坛?/a>
- PHP基于Redis消息隊(duì)列實(shí)現(xiàn)的消息推送的方法