PHP圖像處理技術(shù)實例總結(jié)【繪圖、水印、驗證碼、圖像壓縮】
本文實例總結(jié)了PHP圖像處理技術(shù)。分享給大家供大家參考,具體如下:
1、繪圖
場景: 驗證碼、圖像水印、圖像壓縮處理
php繪圖坐標體系是從0,0點越向右值越大,越向下值越大
需要開啟php的gd2擴展 php.ini 中
參數(shù)1:圖像資源(畫布)
參數(shù)2:開始的x軸坐標
參數(shù)3:開始的y軸坐標
參數(shù)4:結(jié)束的x軸坐標
參數(shù)5:結(jié)束的y軸坐標
參數(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:目標圖像
參數(shù)2:原始圖像
參數(shù)3:目標圖像坐標x
參數(shù)4:目標圖像坐標y
參數(shù)5:原始圖像開始坐標x
參數(shù)6:原始圖像開始坐標y
參數(shù)7:原始圖像寬度
參數(shù)8:原始圖像高度
(5)將圖片繪制到畫布上:imagecopy ( $p1, $p2, $p3, $p4, $p5, $6, $7, $8)
參數(shù)1:目標圖像
參數(shù)2:字體 1,2,3,4 或 5,則使用內(nèi)置字體
參數(shù)3:目標圖像坐標x
參數(shù)4:目標圖像坐標y
參數(shù)5:字符,文字
參數(shù)6:顏色
(6)繪制字符串:imagestring( $p1, $p2, $p3, $p4, $p5, $6)
// 向畫布寫入字符,文字
參數(shù)1:圖像資源
參數(shù)2:字體大小
參數(shù)3:傾斜角度
參數(shù)4:x軸坐標
參數(shù)5:y軸坐標
參數(shù)6:字體顏色
參數(shù)7:字體文件
參數(shù)8:文字
(7)繪制中文:imagettftext($p1, $p2, $p3, $p4, $p5, $6, $7, $8)
參數(shù)1:圖像資源
參數(shù)2:弧形開始x坐標
參數(shù)3:弧形開始y坐標
參數(shù)4:弧形寬度
參數(shù)5:弧形高度
參數(shù)6:弧形開始角度
參數(shù)7:弧形結(jié)束角度
參數(shù)8:繪圖顏色
(8)繪制弧形:imagearc($p1, $p2, $p3, $p4, $p5, $6, $7, $8)
// 三點鐘的位置是起點(0度), 順時針方向繪畫
實例 - 弧形
// 創(chuàng)建一個 200X200 的圖像 $img = imagecreatetruecolor(200, 200); // 分配顏色 $white = imagecolorallocate($img, 255, 255, 255); $black = imagecolorallocate($img, 0, 0, 0); // 畫一個黑色的圓 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坐標
參數(shù)3:弧形開始y坐標
參數(shù)4:弧形寬度
參數(shù)5:弧形高度
參數(shù)6:弧形開始角度
參數(shù)7:弧形結(jié)束角度
參數(shù)8:繪圖顏色
參數(shù)9:填充樣式
【
IMG_ARC_PIE : 用直線連接產(chǎn)生圓形邊界
IMG_ARC_CHORD : 用直線連接了起始和結(jié)束點
IMG_ARC_NOFILL : 明弧或弦只有輪廓,不填充
IMG_ARC_EDGED :用直線將起始和結(jié)束點與中心點相連,和 IMG_ARC_NOFILL 一起使用是畫餅狀圖輪廓的好方法(而不用填充)
】
(9)繪制弧形并填充:imagefilledarc($p1, $p2, $p3, $p4, $p5, $6, $7, $8, $9)
// 三點鐘的位置是起點(0度), 順時針方向繪畫
實例 - 弧形填充
// 創(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()
實例 - 圖片加字
// 建立一幅 100X30 的圖像 $im = imagecreate(100, 30); // 白色背景和藍色文本 $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);
3、驗證碼
封裝的驗證碼類
<?php /* * 生成驗證碼 */ class Captcha { private $_width = 100; private $_height = 25; private $_number = 4; //顯示的驗證碼的字符個數(shù) private $_font = 15; //驗證碼字體大小 private $_fontfile = 'STXINWEI.TTF'; //創(chuàng)建驗證碼圖像 public function makeImage() { # 1. 創(chuàng)建圖像資源(畫布) $image = imagecreatetruecolor($this->_width,$this->_height); //隨機填充顏色 //mt_rand(0,255) 生成一個更具有唯一性的隨機數(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(); //隨機生成驗證碼文字 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條干擾線條 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個干擾像素點 for($i=0;$i<100;$i++){ imagesetpixel($image,mt_rand(0,$this->_width),mt_rand(0,$this->_height),$color); } # 5.將驗證碼保存起來嗎,便于后面再其他地方使用 //只能使用session來存儲,session明天就會講到 session_start(); $_SESSION['captcha'] = $code; //在瀏覽器輸出、顯示一下 header("Content-Type:image/png"); imagepng($image); imagedestroy($image); } /** * 隨機產(chǎn)生隨機數(shù) */ public function makeCode() { # 獲得字母的范圍(大寫字母、小寫字母) $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ù)字 //將上面的三個數(shù)組合并成一個數(shù)組 $code = array_merge($lower,$upper,$number); # 打亂數(shù)組元素的順序 shuffle($code); //隨機從上面的數(shù)組中篩選出n個字符,需要通過下標來取數(shù)組的元素 $str = ''; for($i=0;$i<$this->_number;$i++){ $str .= $code[$i]; } return $str; } /** * 驗證用戶輸入的驗證碼和我們生產(chǎn)的驗證碼是否一致 * @param [str] $input [輸入驗證碼值] * @return */ public function checkCode($input) { session_start(); if(strtolower($code) == strtolower($_SESSION['captcha'])){ //說明驗證碼正確 //echo '驗證碼正確'; return true; }else{ //echo '驗證碼錯誤'; return false; } } } ?>
實例 - 驗證碼驗證(結(jié)合上面的驗證類)
html頁面
<form action="captcha.php?act=verify" method="post"> 驗證碼:<input type="text" name="captcha"> <img src="captcha.php?act=show"> <br> <input type="submit" value="提交"> </form>
驗證碼檢測 captcha.php 頁面
//接收地址欄上面的參數(shù) if($_GET['act']=='verify'){ //說明是提交的表單 //接收表單中用戶輸入的內(nèi)容 $code = $_POST['captcha']; //和創(chuàng)建的驗證碼進行比較 session_start(); //將用戶輸入的驗證碼 和 我們創(chuàng)建的統(tǒng)一小寫之后再進行比較 if(strtolower($code) == strtolower($_SESSION['captcha'])){ //說明驗證碼正確 echo '驗證碼正確'; }else{ echo '驗證碼錯誤'; } }else if($_GET['act']=='show'){ //說明需要顯示一個圖片 require 'Captcha.class.php'; $captcha = new Captcha(); $captcha -> makeImage(); }
4、圖像壓縮
對圖像進行壓
縮處理非常簡單,因為就一個函數(shù)
參數(shù)1:目標圖像資源(畫布)
參數(shù)2:等待壓縮圖像資源
參數(shù)3:目標點的x坐標
參數(shù)4:目標點的y坐標
參數(shù)5:原圖的x坐標
參數(shù)6:原圖的y坐標
參數(shù)7:目的地寬度(畫布寬)
參數(shù)8:目的地高度(畫布高)
參數(shù)9:原圖寬度
參數(shù)10:原圖高度
imagecopyresampled($1,$2,$3,$4,$5,$6,$7,$8,$9,$10)
封裝的圖像壓縮類
<?php /* * 圖像壓縮處理類 */ 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; //也可以另存為一個新的圖像 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)容感興趣的讀者可查看本站專題:《PHP圖形與圖片操作技巧匯總》、《PHP數(shù)組(Array)操作技巧大全》、《PHP數(shù)據(jù)結(jié)構(gòu)與算法教程》、《php程序設(shè)計算法總結(jié)》、《PHP數(shù)學(xué)運算技巧總結(jié)》、《php字符串(string)用法總結(jié)》及《php常見數(shù)據(jù)庫操作技巧匯總》
希望本文所述對大家PHP程序設(shè)計有所幫助。
- PHP的圖像處理實例小結(jié)【文字水印、圖片水印、壓縮圖像等】
- 用來給圖片加水印的PHP類
- php給圖片添加文字水印方法匯總
- PHP圖片處理之使用imagecopy函數(shù)添加圖片水印實例
- 超級好用的一個php上傳圖片類(隨機名,縮略圖,加水印)
- PHP Imagick完美實現(xiàn)圖片裁切、生成縮略圖、添加水印
- php gd2 上傳圖片/文字水印/圖片水印/等比例縮略圖/實現(xiàn)代碼
- php下圖片文字混合水印與縮略圖實現(xiàn)代碼
- php圖片處理:加水印、縮略圖的實現(xiàn)(自定義函數(shù):watermark、thumbnail)
- php文字水印和php圖片水印實現(xiàn)代碼(二種加水印方法)
- PHP圖像處理 imagestring添加圖片水印與文字水印操作示例
相關(guān)文章
PHP連接Nginx服務(wù)器并解析Nginx日志的方法
這篇文章主要介紹了PHP連接Nginx服務(wù)器并解析Nginx日志的方法,PHP+Nginx也是目前一種相當流行的服務(wù)器搭建方案,需要的朋友可以參考下2015-08-08