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 '';

}

function cleancolor($imgid,$color){

    $width = imagesx($imgid);//获取宽

    $height = imagesy($imgid);//获取高

    for($i=0;$i<$width;$i++){

        for($k=0;$k<$hei
路是自己走出来的,而不是选出来的。
原文地址:https://www.cnblogs.com/mo3408/p/15741730.html