Php颜色替换图像

I have this script that replaces black or white color with another color as chosen by the user, works ok however on certain colors (purple background green writing) it leaves a white pixelated border around the letters? I cant figure out what to do? see it here - http://marijasorganicpantry.com.au/imagephp.php

<?php
ob_start();
    $txtcolor=$_REQUEST['text1'];
    $r1txt=hexdec(substr($txtcolor,0,2));
    $g1txt=hexdec(substr($txtcolor,2,2));
    $b1txt=hexdec(substr($txtcolor,4,2));
    
    $backcolor=$_REQUEST['back1'];
    $r1back=hexdec(substr($backcolor,0,2));
    $g1back=hexdec(substr($backcolor,2,2));
    $b1back=hexdec(substr($backcolor,4,2));
    $imgname="demo_the-crown-prints_work-hard_5x7.jpg";
    
    $im = imagecreatefromjpeg($imgname);
    $w = imagesx($im);
    $h = imagesy($im); 
    $gd = imagecreatetruecolor($w,$h);
    imagecopyresampled($gd, $im, 0, 0, 0, 0, $w, $h, $w, $h);
    imagefilter($gd, IMG_FILTER_COLORIZE,$r1txt,$g1txt,$b1txt); 

    for($x=0;$x<$w;$x++) 
    {
        for($y=0;$y<$h;$y++)
            {
                $rgb = imagecolorat($gd, $x, $y);
                $r = ($rgb >> 16) & 0xFF;
                $g = ($rgb >> 8) & 0xFF;
                $b = $rgb & 0xFF;
                
                if ($r==255 and $g==255 and $b==255)
                {   
                $pixelColor=imagecolorallocatealpha($gd,$r1back,$g1back,$b1back,10);
                imagesetpixel($gd,$x,$y,$pixelColor);   
                }
            }
    }
    
imagejpeg($gd,NULL,100);
$outputBuffer = ob_get_clean();
$base64 = base64_encode($outputBuffer);
echo '<a id="downloadimage" style="text-decoration:none;" download>
    <img id="image2" width=150 height=250 src="data:image/jpeg;base64,'.$base64.'" />
    <li style="padding-top:7px;textalign:center;display:block;border-radius:10px;background-color:royaleblue;height:30px;width:100px;">download</li></a>';    
?>

</div>

Because in the original image the letters have a border that it is not a perfect black, so the color is not

R = 0
G = 0
B = 0

I have change a little bit your original code to avoid unnecessary steps. Is this what do you need?

<?php
ob_start();
$txtcolor="20FF00";
$r1txt=hexdec(substr($txtcolor,0,2));
$g1txt=hexdec(substr($txtcolor,2,2));
$b1txt=hexdec(substr($txtcolor,4,2));

$backcolor="FF0ED9";
$r1back=hexdec(substr($backcolor,0,2));
$g1back=hexdec(substr($backcolor,2,2));
$b1back=hexdec(substr($backcolor,4,2));
$imgname="demo_the-crown-prints_work-hard_5x7.jpg";

$gd = imagecreatefromjpeg($imgname);
$w = imagesx($gd);
$h = imagesy($gd);
for($x=0;$x<$w;$x++) 
{
    for($y=0;$y<$h;$y++)
        {
            $rgb = imagecolorat($gd, $x, $y);
            $r = ($rgb >> 16) & 0xFF;
            $g = ($rgb >> 8) & 0xFF;
            $b = $rgb & 0xFF;

            if ($r>200 && $g>200 && $b>200)
            {   
                $pixelColor=imagecolorallocate($gd,$r1back,$g1back,$b1back);
                imagesetpixel($gd,$x,$y,$pixelColor);                   
            } else {
                $pixelColor=imagecolorallocate($gd,$r1txt,$g1txt,$b1txt);
                imagesetpixel($gd,$x,$y,$pixelColor);
            }
        }
}

imagejpeg($gd,'simpletext.jpg', 100);
imagepng($gd,'simpletext.png', 0);
?>

I have saved the result on files to let you see the difference of quality between the imagejpeg and imagepng