php實現(xiàn)的證件照換底色功能示例【人像摳圖/換背景圖】
更新時間:2024年04月08日 11:35:55 作者:TANKING
這篇文章主要介紹了php實現(xiàn)的證件照換底色功能,結(jié)合實例形式分析了php實人像摳圖與換背景圖相關(guān)操作技巧,需要的朋友可以參考下
本文實例講述了php實現(xiàn)的證件照換底色功能。分享給大家供大家參考,具體如下:
<?php
//背景圖和原圖需要保持寬高要保持一樣,這里的示例原圖用的是藍色背景
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="'.$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;
//藍色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ù)學運算技巧總結(jié)》、《php字符串(string)用法總結(jié)》及《php常見數(shù)據(jù)庫操作技巧匯總》
希望本文所述對大家PHP程序設(shè)計有所幫助。
您可能感興趣的文章:
- 使用PHP生成二維碼的兩種方法(帶logo圖像)
- php圖像處理函數(shù)大全(推薦收藏)
- PHP輸出圖像imagegif、imagejpeg與imagepng函數(shù)用法分析
- PHP圖片裁剪函數(shù)(保持圖像不變形)
- PHP圖像處理之imagecreate、imagedestroy函數(shù)介紹
- PHP實現(xiàn)提取一個圖像文件并在瀏覽器上顯示的代碼
- 基于OpenCV的PHP圖像人臉識別技術(shù)
- PHP圖像識別技術(shù)原理與實現(xiàn)
- PHP中繪制圖像的一些函數(shù)總結(jié)
- PHP GD庫生成圖像的幾個函數(shù)總結(jié)
- 解析php常用image圖像函數(shù)集
- PHP基于GD庫的圖像處理方法小結(jié)

