PHP圖像處理技術(shù)實(shí)例總結(jié)【繪圖、水印、驗(yàn)證碼、圖像壓縮】
本文實(shí)例總結(jié)了PHP圖像處理技術(shù)。分享給大家供大家參考,具體如下:
1、繪圖
場(chǎng)景: 驗(yàn)證碼、圖像水印、圖像壓縮處理
php繪圖坐標(biāo)體系是從0,0點(diǎn)越向右值越大,越向下值越大
需要開(kāi)啟php的gd2擴(kuò)展 php.ini 中
參數(shù)1:圖像資源(畫(huà)布)
參數(shù)2:開(kāi)始的x軸坐標(biāo)
參數(shù)3:開(kāi)始的y軸坐標(biāo)
參數(shù)4:結(jié)束的x軸坐標(biāo)
參數(shù)5:結(jié)束的y軸坐標(biāo)
參數(shù)6:線(xiàn)條的顏色
(1)繪制線(xiàn)條: 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:原始圖像開(kāi)始坐標(biāo)x
參數(shù)6:原始圖像開(kāi)始坐標(biāo)y
參數(shù)7:原始圖像寬度
參數(shù)8:原始圖像高度
(5)將圖片繪制到畫(huà)布上: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)
// 向畫(huà)布寫(xiě)入字符,文字
參數(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:弧形開(kāi)始x坐標(biāo)
參數(shù)3:弧形開(kāi)始y坐標(biāo)
參數(shù)4:弧形寬度
參數(shù)5:弧形高度
參數(shù)6:弧形開(kāi)始角度
參數(shù)7:弧形結(jié)束角度
參數(shù)8:繪圖顏色
(8)繪制弧形:imagearc($p1, $p2, $p3, $p4, $p5, $6, $7, $8)
// 三點(diǎn)鐘的位置是起點(diǎn)(0度), 順時(shí)針?lè)较蚶L畫(huà)
實(shí)例 - 弧形
// 創(chuàng)建一個(gè) 200X200 的圖像 $img = imagecreatetruecolor(200, 200); // 分配顏色 $white = imagecolorallocate($img, 255, 255, 255); $black = imagecolorallocate($img, 0, 0, 0); // 畫(huà)一個(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:弧形開(kāi)始x坐標(biāo)
參數(shù)3:弧形開(kāi)始y坐標(biāo)
參數(shù)4:弧形寬度
參數(shù)5:弧形高度
參數(shù)6:弧形開(kāi)始角度
參數(shù)7:弧形結(jié)束角度
參數(shù)8:繪圖顏色
參數(shù)9:填充樣式
【
IMG_ARC_PIE : 用直線(xiàn)連接產(chǎn)生圓形邊界
IMG_ARC_CHORD : 用直線(xiàn)連接了起始和結(jié)束點(diǎn)
IMG_ARC_NOFILL : 明弧或弦只有輪廓,不填充
IMG_ARC_EDGED :用直線(xiàn)將起始和結(jié)束點(diǎn)與中心點(diǎn)相連,和 IMG_ARC_NOFILL 一起使用是畫(huà)餅狀圖輪廓的好方法(而不用填充)
】
(9)繪制弧形并填充:imagefilledarc($p1, $p2, $p3, $p4, $p5, $6, $7, $8, $9)
// 三點(diǎn)鐘的位置是起點(diǎn)(0度), 順時(shí)針?lè)较蚶L畫(huà)
實(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); // 把字符串寫(xiě)在圖像左上角 imagestring($im, 5, 0, 0, "Hello world!", $textcolor); // 輸出圖像 header("Content-type: image/png"); imagepng($im);
3、驗(yàn)證碼
封裝的驗(yàn)證碼類(lèi)
<?php /* * 生成驗(yàn)證碼 */ class Captcha { private $_width = 100; private $_height = 25; private $_number = 4; //顯示的驗(yàn)證碼的字符個(gè)數(shù) private $_font = 15; //驗(yàn)證碼字體大小 private $_fontfile = 'STXINWEI.TTF'; //創(chuàng)建驗(yàn)證碼圖像 public function makeImage() { # 1. 創(chuàng)建圖像資源(畫(huà)布) $image = imagecreatetruecolor($this->_width,$this->_height); //隨機(jī)填充顏色 //mt_rand(0,255) 生成一個(gè)更具有唯一性的隨機(jī)數(shù) #000 255 $color = imagecolorallocate($image,mt_rand(100,255),mt_rand(100,255),mt_rand(100,255)); imagefill($image,0,0,$color); # 2.繪制文字 $code = $this -> makeCode(); //隨機(jī)生成驗(yàn)證碼文字 ab3g $color = imagecolorallocate($image,mt_rand(0,100),mt_rand(0,100),mt_rand(0,100)); for($i=0;$i<$this->_number;$i++){ imagettftext($image,$this->_font,mt_rand(-30,30),$i*($this->_width/$this->_number)+5,20,$color,$this->_fontfile,$code[$i]); } # 3.繪制15條干擾線(xiàn)條 for($i=0;$i<10;$i++){ $color = imagecolorallocate($image,mt_rand(100,150),mt_rand(100,150),mt_rand(100,150)); imageline($image,mt_rand(0,$this->_width),mt_rand(0,$this->_height),mt_rand(0,$this->_width),mt_rand(0,$this->_height),$color); } # 4.設(shè)置100個(gè)干擾像素點(diǎn) for($i=0;$i<100;$i++){ imagesetpixel($image,mt_rand(0,$this->_width),mt_rand(0,$this->_height),$color); } # 5.將驗(yàn)證碼保存起來(lái)嗎,便于后面再其他地方使用 //只能使用session來(lái)存儲(chǔ),session明天就會(huì)講到 session_start(); $_SESSION['captcha'] = $code; //在瀏覽器輸出、顯示一下 header("Content-Type:image/png"); imagepng($image); imagedestroy($image); } /** * 隨機(jī)產(chǎn)生隨機(jī)數(shù) */ public function makeCode() { # 獲得字母的范圍(大寫(xiě)字母、小寫(xiě)字母) $lower = range('a','z'); //創(chuàng)建從小a到小z字符范圍的數(shù)組 $upper = range('A','Z'); //創(chuàng)建從大A到大Z范圍的數(shù)組 $number = range(3,9); //創(chuàng)建從3到9之間的數(shù)字 //將上面的三個(gè)數(shù)組合并成一個(gè)數(shù)組 $code = array_merge($lower,$upper,$number); # 打亂數(shù)組元素的順序 shuffle($code); //隨機(jī)從上面的數(shù)組中篩選出n個(gè)字符,需要通過(guò)下標(biāo)來(lái)取數(shù)組的元素 $str = ''; for($i=0;$i<$this->_number;$i++){ $str .= $code[$i]; } return $str; } /** * 驗(yàn)證用戶(hù)輸入的驗(yàn)證碼和我們生產(chǎn)的驗(yàn)證碼是否一致 * @param [str] $input [輸入驗(yàn)證碼值] * @return */ public function checkCode($input) { session_start(); if(strtolower($code) == strtolower($_SESSION['captcha'])){ //說(shuō)明驗(yàn)證碼正確 //echo '驗(yàn)證碼正確'; return true; }else{ //echo '驗(yàn)證碼錯(cuò)誤'; return false; } } } ?>
實(shí)例 - 驗(yàn)證碼驗(yàn)證(結(jié)合上面的驗(yàn)證類(lèi))
html頁(yè)面
<form action="captcha.php?act=verify" method="post"> 驗(yàn)證碼:<input type="text" name="captcha"> <img src="captcha.php?act=show"> <br> <input type="submit" value="提交"> </form>
驗(yàn)證碼檢測(cè) captcha.php 頁(yè)面
//接收地址欄上面的參數(shù) if($_GET['act']=='verify'){ //說(shuō)明是提交的表單 //接收表單中用戶(hù)輸入的內(nèi)容 $code = $_POST['captcha']; //和創(chuàng)建的驗(yàn)證碼進(jìn)行比較 session_start(); //將用戶(hù)輸入的驗(yàn)證碼 和 我們創(chuàng)建的統(tǒng)一小寫(xiě)之后再進(jìn)行比較 if(strtolower($code) == strtolower($_SESSION['captcha'])){ //說(shuō)明驗(yàn)證碼正確 echo '驗(yàn)證碼正確'; }else{ echo '驗(yàn)證碼錯(cuò)誤'; } }else if($_GET['act']=='show'){ //說(shuō)明需要顯示一個(gè)圖片 require 'Captcha.class.php'; $captcha = new Captcha(); $captcha -> makeImage(); }
4、圖像壓縮
對(duì)圖像進(jìn)行壓
縮處理非常簡(jiǎn)單,因?yàn)榫鸵粋€(gè)函數(shù)
參數(shù)1:目標(biāo)圖像資源(畫(huà)布)
參數(shù)2:等待壓縮圖像資源
參數(shù)3:目標(biāo)點(diǎn)的x坐標(biāo)
參數(shù)4:目標(biāo)點(diǎn)的y坐標(biāo)
參數(shù)5:原圖的x坐標(biāo)
參數(shù)6:原圖的y坐標(biāo)
參數(shù)7:目的地寬度(畫(huà)布寬)
參數(shù)8:目的地高度(畫(huà)布高)
參數(shù)9:原圖寬度
參數(shù)10:原圖高度
imagecopyresampled($1,$2,$3,$4,$5,$6,$7,$8,$9,$10)
封裝的圖像壓縮類(lèi)
<?php /* * 圖像壓縮處理類(lèi) */ class Thumb { private $_filename; //等待壓縮的圖像 private $_thumb_path = 'thumb/'; //壓縮圖像的保存目錄 public function __set($p,$v) { if(property_exists($this,$p)){ $this -> $p = $v; } } //構(gòu)造方法初始化需要壓縮的圖像 public function __construct($file) { if(!file_exists($file)){ echo '文件有誤,不能壓縮'; return; } $this -> _filename = $file; } //圖像壓縮處理 function makeThumb($area_w,$area_h) { $src_image = imagecreatefrompng($this->_filename); $res = getimagesize($this->_filename); echo '<pre>'; var_dump($res); die; $dst_x = 0; $dst_y = 0; $src_x = 0; $src_y = 0; //原圖的寬度、高度 $src_w = imagesx($src_image); //獲得圖像資源的寬度 $src_h = imagesy($src_image); //獲得圖像資源的高度 if($src_w / $area_w < $src_h/$area_h){ $scale = $src_h/$area_h; } if($src_w / $area_w >= $src_h/$area_h){ $scale = $src_w / $area_w; } $dst_w = (int)($src_w / $scale); $dst_h = (int)($src_h / $scale); $dst_image = imagecreatetruecolor($dst_w,$dst_h); $color = imagecolorallocate($dst_image,255,255,255); //將白色設(shè)置為透明色 imagecolortransparent($dst_image,$color); imagefill($dst_image,0,0,$color); imagecopyresampled($dst_image,$src_image,$dst_x,$dst_y,$src_x,$src_y,$dst_w,$dst_h,$src_w,$src_h); //可以在瀏覽器直接顯示 //header("Content-Type:image/png"); //imagepng($dst_image); //分目錄保存壓縮的圖像 $sub_path = date('Ymd').'/'; //規(guī)范:上傳的圖像保存到upload目錄,壓縮的圖像保存到thumb目錄 if(!is_dir($this -> _thumb_path . $sub_path)){ mkdir($this -> _thumb_path . $sub_path,0777,true); } $filename = $this -> _thumb_path . $sub_path.'thumb_'.$this->_filename; //也可以另存為一個(gè)新的圖像 imagepng($dst_image,$filename); return $filename; } } $thumb = new Thumb('upload.jpg'); $thumb -> _thumb_path = 'static/thumb/'; $file = $thumb -> makeThumb(100,50); // var_dump($file);
更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專(zhuān)題:《PHP圖形與圖片操作技巧匯總》、《PHP數(shù)組(Array)操作技巧大全》、《PHP數(shù)據(jù)結(jié)構(gòu)與算法教程》、《php程序設(shè)計(jì)算法總結(jié)》、《PHP數(shù)學(xué)運(yùn)算技巧總結(jié)》、《php字符串(string)用法總結(jié)》及《php常見(jiàn)數(shù)據(jù)庫(kù)操作技巧匯總》
希望本文所述對(duì)大家PHP程序設(shè)計(jì)有所幫助。
- PHP的圖像處理實(shí)例小結(jié)【文字水印、圖片水印、壓縮圖像等】
- 用來(lái)給圖片加水印的PHP類(lèi)
- php給圖片添加文字水印方法匯總
- PHP圖片處理之使用imagecopy函數(shù)添加圖片水印實(shí)例
- 超級(jí)好用的一個(gè)php上傳圖片類(lèi)(隨機(jī)名,縮略圖,加水印)
- PHP Imagick完美實(shí)現(xiàn)圖片裁切、生成縮略圖、添加水印
- php gd2 上傳圖片/文字水印/圖片水印/等比例縮略圖/實(shí)現(xiàn)代碼
- php下圖片文字混合水印與縮略圖實(shí)現(xiàn)代碼
- php圖片處理:加水印、縮略圖的實(shí)現(xiàn)(自定義函數(shù):watermark、thumbnail)
- php文字水印和php圖片水印實(shí)現(xiàn)代碼(二種加水印方法)
- PHP圖像處理 imagestring添加圖片水印與文字水印操作示例
相關(guān)文章
php 數(shù)組的指針操作實(shí)現(xiàn)代碼
php 數(shù)組的指針操作實(shí)現(xiàn)代碼,數(shù)組在php編寫(xiě)中是非常重要的操作,學(xué)習(xí)php的朋友可以參考下。2011-02-02Laravel中l(wèi)og無(wú)法寫(xiě)入問(wèn)題的解決
這篇文章主要介紹了Laravel中l(wèi)og無(wú)法寫(xiě)入問(wèn)題的解決,文中給出了詳細(xì)解決方法供大家參考學(xué)習(xí),對(duì)大家具有一定的參考借鑒價(jià)值,需要的朋友們下面來(lái)一起看看吧。2017-06-06PHP連接Nginx服務(wù)器并解析Nginx日志的方法
這篇文章主要介紹了PHP連接Nginx服務(wù)器并解析Nginx日志的方法,PHP+Nginx也是目前一種相當(dāng)流行的服務(wù)器搭建方案,需要的朋友可以參考下2015-08-08PHP簡(jiǎn)單實(shí)現(xiàn)文本計(jì)數(shù)器的方法
這篇文章主要介紹了PHP簡(jiǎn)單實(shí)現(xiàn)文本計(jì)數(shù)器的方法,涉及PHP針對(duì)文本文件的簡(jiǎn)單判斷,讀取及寫(xiě)入等操作技巧,需要的朋友可以參考下2016-04-04