《PHP實例:PHP基于GD庫的圖像處理方法小結(jié)》要點:
本文介紹了PHP實例:PHP基于GD庫的圖像處理方法小結(jié),希望對您有用。如果有疑問,可以聯(lián)系我們。
本文實例講述了PHP基于GD庫的圖像處理方法.分享給大家供大家參考,具體如下:PHP應(yīng)用
gd圖像處理技術(shù)PHP應(yīng)用
extension=php_gd2.dllPHP應(yīng)用
創(chuàng)建畫布PHP應(yīng)用
畫布,一種資源型數(shù)據(jù),可操作的圖像資源PHP應(yīng)用
創(chuàng)建畫布(新建)PHP應(yīng)用
imageCreate(width,height) //創(chuàng)建基于調(diào)色板的畫布PHP應(yīng)用
imageCreateTrueColor(width,height) //創(chuàng)建真彩色的畫布PHP應(yīng)用
基于圖片創(chuàng)建畫布(打開)PHP應(yīng)用
imageCreateFromJPEG( url)
imageCreateFromPNG(url)
imageCreateFromGIF(url)PHP應(yīng)用
操作畫布PHP應(yīng)用
分配顏色:如果需要在畫布上使用某種顏色,應(yīng)該先將顏色分配到畫布上.PHP應(yīng)用
(顏色標(biāo)識 )= imageColorAllocate(img,r,g,b)PHP應(yīng)用
填充畫布PHP應(yīng)用
imageFill(img,x,y,顏色標(biāo)識)PHP應(yīng)用
輸出畫布PHP應(yīng)用
1. 輸出到圖片文件PHP應(yīng)用
2. 直接輸出,需要告知瀏覽器輸出為圖片信息(header("Content-type:image/png;"))PHP應(yīng)用
imagePNG(img[,url]) imageJPEG() imageGIF()
銷毀畫布資源PHP應(yīng)用
imageDestroy(img)PHP應(yīng)用
<?php header('content-type:image/png'); $img = imagecreate(300,300); $color = imagecolorallocate($img,223,22,44); imagefill($img,3,3,$color); imagepng($img); imagedestroy(); ?>
運行效果圖如下:PHP應(yīng)用
PHP應(yīng)用
驗證碼實現(xiàn)PHP應(yīng)用
<?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>
運行效果圖如下:PHP應(yīng)用
?PHP應(yīng)用
<?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); ?>
注意:圖片輸出前后不能有額外輸出PHP應(yīng)用
更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《PHP圖形與圖片操作技巧匯總》、《PHP基本語法入門教程》、《PHP運算與運算符用法總結(jié)》、《php面向?qū)ο蟪绦蛟O(shè)計入門教程》、《PHP網(wǎng)絡(luò)編程技巧總結(jié)》、《PHP數(shù)組(Array)操作技巧大全》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總》PHP應(yīng)用
希望本文所述對大家PHP程序設(shè)計有所幫助.PHP應(yīng)用
轉(zhuǎn)載請注明本頁網(wǎng)址:
http://www.snjht.com/jiaocheng/3195.html