PHP的圖像處理實例小結(jié)【文字水印、圖片水印、壓縮圖像等】
本文實例講述了PHP的圖像處理。分享給大家供大家參考,具體如下:
1、添加文字水印
//1、打開圖片資源 $src="./material/sea.jpg"; $info=getimagesize($src);//獲取圖片信息 $type=image_type_to_extension($info[2],false);//轉(zhuǎn)化圖片類型 //var_dump($info); $fun="imagecreatefrom{$type}";//拼接成為imagecreatefromjpeg()方法 $image=$fun($src);//新建GD圖片資源 //操作圖片 $font="./material/segoepr.ttf"; $content="@SuperTory"; $color=imagecolorallocate($image,255,255,255); imagettftext($image,10,0,0,$info[1]-5,$color,$font,$content);//圖片上寫文字 //輸出圖片 header("content-type:".$info['mime']);//$imfo['mine']='image/jpeg' $output="image{$type}";//拼接成為imagejpeg()方法 $output($image);//輸出到頁面 $output($image,'./material/watermarked.'.$type);//輸出到本地路徑 //銷毀圖片內(nèi)存資源 imagedestroy($image);
2、壓縮圖像
//打開圖像 $src="./material/logo.png"; $info=getimagesize($src); $type=image_type_to_extension($info[2],false); $create="imagecreatefrom".$type; $image=$create($src); //壓縮 $tinyImg=imagecreatetruecolor(100,100); //新建壓縮后的圖像資源 //將原圖映射到壓縮后的圖像資源上 imagecopyresampled($tinyImg,$image,0,0,0,0,100,100,$info[0],$info[1]); header("content-type:".$info['mime']); $output="image{$type}"; //$output($image); $output($tinyImg); //銷毀 imagedestroy($image); imagedestroy($tinyImg);
3、添加水印圖片
//獲取原圖片 $src="./material/sea.jpg"; $info=getimagesize($src); $type=image_type_to_extension($info[2],false); $create="imagecreatefrom".$type; $image=$create($src); //獲取水印圖片資源 $markSrc="./material/logo.png"; $markInfo=getimagesize($markSrc); $markType=image_type_to_extension($markInfo[2],false); $create="imagecreatefrom".$markType; $markImage=$create($markSrc); $tinyImg=imagecreatetruecolor(100,100); imagecopyresampled($tinyImg,$markImage,0,0,0,0, 100,100,$markInfo[0],$markInfo[1]); imagecopymerge($image,$tinyImg,$info[0]-100,$info[1]-100, 0,0,100,100,100); //合并圖片:(原圖,水印圖,原圖x位置,原圖y位置,水印x起點,水印y起點,水印x終點,水印y終點,不透明度) header("content-type:".$info['mime']); $output="image{$type}"; $output($image); imagedestroy($image); imagedestroy($markImage);
更多關(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è)計有所幫助。
相關(guān)文章
PHP 實現(xiàn) JSON 數(shù)據(jù)的編碼和解碼操作詳解
這篇文章主要介紹了PHP 實現(xiàn) JSON 數(shù)據(jù)的編碼和解碼操作,結(jié)合實例形式詳細(xì)分析了PHP操作json格式數(shù)據(jù)編碼、解碼函數(shù)使用場景及相關(guān)操作注意事項,需要的朋友可以參考下2020-04-04php使用mysqli和pdo擴展,測試對比mysql數(shù)據(jù)庫的執(zhí)行效率完整示例
這篇文章主要介紹了php使用mysqli和pdo擴展,測試對比mysql數(shù)據(jù)庫的執(zhí)行效率,結(jié)合完整實例形式對比分析了php分別使用mysqli與pdo進行數(shù)據(jù)庫插入操作的執(zhí)行時間,需要的朋友可以參考下2019-05-05PHP函數(shù)實現(xiàn)從一個文本字符串中提取關(guān)鍵字的方法
這篇文章主要介紹了PHP函數(shù)實現(xiàn)從一個文本字符串中提取關(guān)鍵字的方法,涉及php針對字符串的遍歷與查找等操作技巧,需要的朋友可以參考下2015-07-07PHP中func_get_args(),func_get_arg(),func_num_args()的區(qū)別
我們再看一下PHP的這三個函數(shù)[unc_get_arg(),func_get_args(),func_num_args()]的區(qū)別,我們先看一下,下面的實例代碼2013-09-09PHP 的異常處理、錯誤的拋出及回調(diào)函數(shù)等面向?qū)ο蟮腻e誤處理方法
PHP 5 提供了一種新的面向?qū)ο蟮腻e誤處理方法,包括PHP 的異常處理、錯誤的拋出及回調(diào)函數(shù)等面向?qū)ο蟮腻e誤處理方法,需要了解更多的朋友可以參考下2012-12-12