Home >Backend Development >PHP Tutorial >PHP can also change the background color of ID photos, come and learn!

PHP can also change the background color of ID photos, come and learn!

藏色散人
藏色散人forward
2021-09-21 16:33:054224browse

This article is from the PHP tutorial column to introduce to you how to change the background color of ID photos in PHP, cut out portraits in PHP, and change the background image in PHP. I hope it will be helpful to you!

php ID photo change background color, php portrait cutout, php change background image

The code is as follows:

<?php //背景图和原图需要保持宽高要保持一样,这里的示例原图用的是蓝色背景
init();
function init(){
    $old = &#39;1.png&#39;;
    $new = &#39;2.png&#39;;
    //创建一个png透明图
    $img = imagecreatefrompng($old);
    setpng($img,$old,$new);
}
function setpng($imgid,$filename,$savename){
    $bg = &#39;bg.png&#39;;//背景图
    $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 &#39;<img  src="&#39;.$savename.&#39;" alt="PHP can also change the background color of ID photos, come and learn!" >';
}
function cleancolor($imgid,$color){
    $width = imagesx($imgid);//获取宽
    $height = imagesy($imgid);//获取高
    for($i=0;$i> 16)&0xff;//取R
            $g = ($rgb >> 8)&0xff;//取G
            $b = $rgb&0xff;//取B
            $randr = 1.5;
            $randg = 1;
            $randb=1;
            //蓝色RGB大致的位置。替换成绿色
            if($r=100){
                //如果能够精确的计算出要保留位置的,这里可以写绝对的数字
                if($i>=$width/2 && $i=$height/2 && $k<pre class="brush:php;toolbar:false">$old指的是要处理的图片,指定为png格式
$new指的是处理后输出的图片名
$bg指的是背景图

PHP can also change the background color of ID photos, come and learn!

Author:TANKING
Date:2020-05-07
Web:http://www.likeyunba.com/
WeChat:face6009

Recommended study:《 PHP video tutorial

The above is the detailed content of PHP can also change the background color of ID photos, come and learn!. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:segmentfault.com. If there is any infringement, please contact admin@php.cn delete