php實現(xiàn)的證件照換底色功能示例【人像摳圖/換背景圖】
本文實例講述了php實現(xiàn)的證件照換底色功能。分享給大家供大家參考,具體如下:
<?php//背景圖和原圖需要保持寬高要保持一樣,這里的示例原圖用的是藍(lán)色背景init();function init(){ $old = ’1.png’; $new = ’2.png’; //創(chuàng)建一個png透明圖 $img = imagecreatefrompng($old); setpng($img,$old,$new);}function setpng($imgid,$filename,$savename){ $bg = ’bg.png’;//背景圖 $new = imagecreatefrompng($bg);//創(chuàng)建一個png透明圖 list($width,$height)=getimagesize($filename);//獲取長和寬 $white = imagecolorallocate($imgid,1,155,215);//選擇一個替換顏色。這里是綠色 cleancolor($imgid,$white); imagecolortransparent($imgid,$white);//把選擇的顏色替換成透明 imagecopymerge($new,$imgid,0,0,0,0,$width,$height,100);//合并圖片 imagepng($new,$savename);//保存圖片 imagedestroy($imgid);//銷毀 imagedestroy($new); echo ’<img src='http://www.gepszalag.com/bcjs/’.$savename.’'>’;}function cleancolor($imgid,$color){ $width = imagesx($imgid);//獲取寬 $height = imagesy($imgid);//獲取高 for($i=0;$i<$width;$i++){ for($k=0;$k<$height;$k++){ //對比每一個像素 $rgb = imagecolorat($imgid,$i,$k); $r = ($rgb >> 16)&0xff;//取R $g = ($rgb >> 8)&0xff;//取G $b = $rgb&0xff;//取B $randr = 1.5; $randg = 1; $randb=1; //藍(lán)色RGB大致的位置。替換成綠色 if($r<=65*$randr && $g<=225*$randg && $b<=255*$randb && $b*$randb>=100){//如果能夠精確的計算出要保留位置的,這里可以寫絕對的數(shù)字if($i>=$width/2 && $i<=$width/2 && $k>=$height/2 && $k<=$height/2){ }else{ //改變顏色 imagesetpixel($imgid,$i,$k,$color);} } } }} $old指的是要處理的圖片,指定為png格式 $new指的是處理后輸出的圖片名 $bg指的是背景圖
更多關(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)文章:
1. .Net Core和RabbitMQ限制循環(huán)消費的方法2. jsp網(wǎng)頁實現(xiàn)貪吃蛇小游戲3. asp(vbs)Rs.Open和Conn.Execute的詳解和區(qū)別及&H0001的說明4. ASP.NET MVC遍歷驗證ModelState的錯誤信息5. 用css截取字符的幾種方法詳解(css排版隱藏溢出文本)6. ASP 信息提示函數(shù)并作返回或者轉(zhuǎn)向7. asp中response.write("中文")或者js中文亂碼問題8. PHP設(shè)計模式中工廠模式深入詳解9. CSS hack用法案例詳解10. 將properties文件的配置設(shè)置為整個Web應(yīng)用的全局變量實現(xiàn)方法
