douwen3836 2016-09-02 02:27
浏览 56
已采纳

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>
  • 写回答

1条回答 默认 最新

  • dp0518 2016-09-02 02:36
    关注

    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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 正弦信号发生器串并联电路电阻无法保持同步怎么办
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 个人网站被恶意大量访问,怎么办
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)