2016-09-02 110 views
0

我有這個腳本,用用戶選擇的另一種顏色替換黑色或白色,但在某些顏色(紫色背景綠色書寫)上工作正常,它會在字母周圍留下白色像素化邊框? 我不知道該怎麼辦? 看到它在這裏 - http://marijasorganicpantry.com.au/imagephp.php在圖像上的PHP顏色替換

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

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

回答

0

因爲在原始圖像中的字母有一個邊界,這是不是一個完美的黑色,這樣的顏色是不是

R = 0 
G = 0 
B = 0 

我有一點點改變您的原始代碼,以避免不必要的步驟。這是你需要什麼?

<?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); 
?> 

我已保存的文件的結果,讓你看看質量imagejpeg和imagepng

+0

感謝那個之間的區別,我已經採取了最灰度的嘗試,使圖像純黑和白色但是我仍然有一些灰色斑點的圖像,所以他們顯示爲白色,我怎麼可以添加額外的灰色顏色的代碼,以便他們也轉換?我試過如果($ r == 255和$ g == 255和$ b == 255 || $ r == 212和$ g == 212和$ b == 212 || $ r == 224和$ g == 224和$ b == 224),但它不工作代碼有什麼問題? – user2530616

+0

如果你想假設原始圖像只是白色和黑色,只要嘗試做這樣的事情如果($ r!== 0 || $ g!== 0 || $ b!== 0)。在你的代碼中,你忘記了很多可能的組合;) – Davide

+0

當我放大時,我可以看到很多灰色斑點,所以我想也許如果我可以得到它們的值並將它們全部添加進去,至少可以獲得更好的質量並獲得擺脫一些景點:)我怎樣才能添加更多的陰影代碼?我卡住了 – user2530616