怎么在PHP中使用GD庫處理圖像?相信很多沒有經(jīng)驗的人對此束手無策,為此本文總結(jié)了問題出現(xiàn)的原因和解決方法,通過這篇文章希望你能解決這個問題。
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)識 )= imageColorAllocate(img,r,g,b)
填充畫布
imageFill(img,x,y,顏色標(biāo)識)
輸出畫布
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)行效果圖如下:
驗證碼實現(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); ?>
看完上述內(nèi)容,你們掌握怎么在PHP中使用GD庫處理圖像的方法了嗎?如果還想學(xué)到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝各位的閱讀!
當(dāng)前題目:怎么在PHP中使用GD庫處理圖像-創(chuàng)新互聯(lián)
當(dāng)前地址:http://chinadenli.net/article30/dghdso.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站設(shè)計、虛擬主機(jī)、網(wǎng)站改版、做網(wǎng)站、響應(yīng)式網(wǎng)站、外貿(mào)建站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容