PHP基于GD庫(kù)的圖像處理方法小結(jié)
本文實(shí)例講述了PHP基于GD庫(kù)的圖像處理方法。分享給大家供大家參考,具體如下:
gd圖像處理技術(shù)
extension=php_gd2.dll
創(chuàng)建畫布
畫布,一種資源型數(shù)據(jù),可操作的圖像資源
創(chuàng)建畫布(新建)
imageCreate(width,height) //創(chuàng)建基于調(diào)色板的畫布
imageCreateTrueColor(width,height) //創(chuàng)建真彩色的畫布
基于圖片創(chuàng)建畫布(打開)
imageCreateFromJPEG( url)
imageCreateFromPNG(url)
imageCreateFromGIF(url)
操作畫布
分配顏色:如果需要在畫布上使用某種顏色,應(yīng)該先將顏色分配到畫布上。
(顏色標(biāo)識(shí) )= imageColorAllocate(img,r,g,b)
填充畫布
imageFill(img,x,y,顏色標(biāo)識(shí))
輸出畫布
1. 輸出到圖片文件
2. 直接輸出,需要告知瀏覽器輸出為圖片信息(header("Content-type:image/png;"))
imagePNG(img[,url]) imageJPEG() imageGIF()
銷毀畫布資源
imageDestroy(img)
<?php header('content-type:image/png'); $img = imagecreate(300,300); $color = imagecolorallocate($img,223,22,44); imagefill($img,3,3,$color); imagepng($img); imagedestroy(); ?>
運(yùn)行效果圖如下:
驗(yàn)證碼實(shí)現(xiàn)
<?php header('content-type:image/png'); $code = '123456789abcdefghijklmnpqrstuvwxvz'; $length = strlen($code); $print = ''; for($i=0; $i<4; $i++){ $print.=$code[mt_rand(0,$length-1)]; } // echo $print; $img = imagecreatefrompng('./str.png'); $color = mt_rand(0,1)==1?imagecolorallocate($img,0,0,0):imagecolorallocate($img,255,255,255); //圖片大小 $img_width = imagesx($img); $img_height = imagesy($img); //字體大小 $font = 5; $font_width = imagefontwidth($font); $font_height = imagefontheight($font); $fin_w = ($img_width-$font_width*4)/2; $fin_h = ($img_height-$font_height)/2; imagestring($img,$font,$fin_w,$fin_h,$print,$color); imagepng($img); imagedestroy($img); ?> <image src="gd_string.php" onclick="this.src='gd_string.php?ra='+Math.random()"></image>
運(yùn)行效果圖如下:
<?php session_start(); $im=imagecreatetruecolor(80,30); $str=""; for ($i=0;$i<4;$i++){ $str.=dechex(rand(0,15)); } $_SESSION['code']=$str; $white=imagecolorallocate($im,255,255,255); imagestring($im,rand(2,5),rand(0,70),rand(0,10),$str,$white); //imagettftext($im,rand(0,5),rand(0,180),rand(0,100),rand(0,10),$white,"simhei.ttf",$str); for($i=0;$i<20;$i++){ $color=imagecolorallocate($im,rand(0,255),rand(0,255),rand(0,255)); imageline($im,rand(0,90),rand(0,20),rand(0,100),rand(0,100),$color); } header("content-type:image/png"); imagepng($im); imagedestroy($im); ?>
注意:圖片輸出前后不能有額外輸出
更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《PHP圖形與圖片操作技巧匯總》、《PHP基本語(yǔ)法入門教程》、《PHP運(yùn)算與運(yùn)算符用法總結(jié)》、《php面向?qū)ο蟪绦蛟O(shè)計(jì)入門教程》、《PHP網(wǎng)絡(luò)編程技巧總結(jié)》、《PHP數(shù)組(Array)操作技巧大全》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫(kù)操作入門教程》及《php常見數(shù)據(jù)庫(kù)操作技巧匯總》
希望本文所述對(duì)大家PHP程序設(shè)計(jì)有所幫助。
- 使用PHP生成二維碼的兩種方法(帶logo圖像)
- php圖像處理函數(shù)大全(推薦收藏)
- PHP輸出圖像imagegif、imagejpeg與imagepng函數(shù)用法分析
- PHP圖片裁剪函數(shù)(保持圖像不變形)
- PHP圖像處理之imagecreate、imagedestroy函數(shù)介紹
- PHP實(shí)現(xiàn)提取一個(gè)圖像文件并在瀏覽器上顯示的代碼
- 基于OpenCV的PHP圖像人臉識(shí)別技術(shù)
- PHP圖像識(shí)別技術(shù)原理與實(shí)現(xiàn)
- PHP中繪制圖像的一些函數(shù)總結(jié)
- PHP GD庫(kù)生成圖像的幾個(gè)函數(shù)總結(jié)
- 解析php常用image圖像函數(shù)集
- php實(shí)現(xiàn)的證件照換底色功能示例【人像摳圖/換背景圖】
相關(guān)文章
php 時(shí)間計(jì)算問(wèn)題小結(jié)
最近在學(xué)PHP,一位朋友問(wèn)到時(shí)間的計(jì)算,此時(shí)我想到了delphi和mssql的計(jì)算函數(shù),它們用起來(lái)都很方便,但查查php手冊(cè)并未發(fā)現(xiàn)類似的時(shí)間計(jì)算函數(shù),通過(guò)網(wǎng)文的啟發(fā)和自已的測(cè)試,還是找到簡(jiǎn)單的方法來(lái)實(shí)現(xiàn)2009-01-01PHP實(shí)現(xiàn)限制IP訪問(wèn)及提交次數(shù)的方法詳解
這篇文章主要介紹了PHP實(shí)現(xiàn)限制IP訪問(wèn)及提交次數(shù)的方法,涉及php針對(duì)客戶端來(lái)訪IP的獲取、判斷以及結(jié)合session記錄IP訪問(wèn)次數(shù)等相關(guān)操作技巧,需要的朋友可以參考下2017-07-07PHP GD 圖像處理組件的常用函數(shù)總結(jié)
如今,在互聯(lián)網(wǎng)上,很多網(wǎng)站都要處理大量圖片,比如:頭像、上傳的圖片做縮略圖加水印等等。他們需要服務(wù)端的大量圖片處理,一門好的服務(wù)端語(yǔ)言環(huán)境一定要有對(duì)圖像處理的支持。2010-04-04ThinkPHP刪除欄目(實(shí)現(xiàn)批量刪除欄目)
下面小編就為大家?guī)?lái)一篇ThinkPHP刪除欄目(實(shí)現(xiàn)批量刪除欄目)。小編覺得挺不錯(cuò)的?,F(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-06-06PHP圖像處理之imagecreate、imagedestroy函數(shù)介紹
這篇文章主要介紹了PHP圖像處理之imagecreate、imagedestroy函數(shù)介紹,imagecreate用于創(chuàng)建一個(gè)圖像,imagedestroy用于銷毀一個(gè)圖像,需要的朋友可以參考下2014-11-11