本文由PHP教程栏目给大师介绍php证件照换底色、php人像抠图、php换布景图办法,但愿对于你有所协助!
php证件照换底色、php人像抠图、php换布景图
代码以下:
<?php
//布景图以及原图需求坚持宽高要坚持同样,这里的示例原图用的是蓝色布景
init();
function init(){
$old = '1.png';
$new = '2.png';
//创立一个png通明图
$img = imagecreatefrompng($old);
setpng($img,$old,$new);
}
function setpng($imgid,$filename,$savename){
$bg = 'bg.png';//布景图
$new = imagecreatefrompng($bg);//创立一个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){
//假如可以准确的较量争论出要保存地位的,这里能够写相对的数字
if($i>=$width/2 && $i<=$width/2 && $k>=$height/2 && $k<=$height/2){
}else{
//改动色彩
imagesetpixel($imgid,$i,$k,$color);
}
}
}
}
}
$old指的是要处置的图片,指定为png格局
$new指的是处置后输入的图片名
$bg指的是布景图
Author:TANKING
Date:2020-05-07
Web:http://www.likeyunba.com/
WeChat:face6009
推选进修:《PHP视频教程》
以上便是php也能够完成证件照换底色,快来进修!的具体内容,更多请存眷酷吧易资源网别的相关文章!
有用么