2011-02-04 50 views
1

更换一种颜色我曾尝试:我怎样才能与其他的PNG 24阿尔法透明图像与GD

$index = imagecolorresolve ($im, 0,0,0); // get black 
imagecolorset($im, $index, 255, 0, 255); // SET NEW COLOR 

这似乎与PNG 8工作,但不是24,如果我有8个做它由于抗锯齿而变得怪异。

以下是我正在使用的完整测试代码。 (这只是测试代码,所以要温和)。

function LoadPNG($imgname, $color = false) 
{   
    $im = @imagecreatefrompng($imgname); 
    imagealphablending($im, false); 

    if($color) { 
     $index = imagecolorresolve ($im, 0,0,0); // get black 
     imagecolorset($im, $index, 255, 0, 255); // SET NEW COLOR 
    } 

    imageAlphaBlending($im, true); 
    imageSaveAlpha($im, true); 

    return $im; 
} 

header('Content-Type: image/png'); 

$img = LoadPNG("head.png", "red"); 

imagepng($img); 
imagedestroy($img); 
+0

这并未对tolenrance DOC回答你的问题,但我使用[pngcolorizealpha](http://theserverpages.com/php/manual/en/function.imagecopy.php#48355)与一些[imagefilledpolyg on()](http://us2.php.net/imagefilledpolygon) – Mikhail 2011-02-04 20:16:06

+0

这些将最终是代表汽车零件的复杂件。所以我不能把它们画成多边形。 (可以完成,但我做不到。) – merlincam 2011-02-04 20:24:29

回答

3

你可以尝试以下方法:

  • 周期的所有点
  • 得到这一点
  • 的颜色,如果你colorA匹配,即像素设置为所需colorB

代码:

for ($x=imagesx($im); $x--;) { 
    for ($y=imagesy($im); $y--;) { 
     $c = imagecolorat($im, $x, $y); 
     if ($c[0] == 0 && $c[1] == 0 && $c[2] == 0) { 
      // here we use the new color, but the original alpha channel 
      $colorB = imagecolorallocatealpha($im, 255, 0, 255, $c[3]); 
      imagesetpixel($im, $x, $y, $colorB); 
     } 
    } 
} 

希望这有助于!

7

基于英迪的解决方案,我做了一个脚本,它的工作原理:

$imgname = "yourimage.png"; 
$im = imagecreatefrompng($imgname); 
imagealphablending($im, false); 
for ($x = imagesx($im); $x--;) { 
    for ($y = imagesy($im); $y--;) { 
     $rgb = imagecolorat($im, $x, $y); 
     $c = imagecolorsforindex($im, $rgb); 
     if ($c['red'] < 40 && $c['green'] < 40 && $c['blue'] < 40) { // dark colors 
      // here we use the new color, but the original alpha channel 
      $colorB = imagecolorallocatealpha($im, 255, 0, 255, $c['alpha']); 
      imagesetpixel($im, $x, $y, $colorB); 
     } 
    } 
} 
imageAlphaBlending($im, true); 
imageSaveAlpha($im, true); 
header('Content-Type: image/png'); 
imagepng($im); 
imagedestroy($im); 

我想办法优化它,因为它是相当缓慢

0

该功能的工作就像一个魅力:

public function ImageToBlackAndWhite($im) { 
    for ($x = imagesx($im); $x--;) { 
     for ($y = imagesy($im); $y--;) { 
      $rgb = imagecolorat($im, $x, $y); 
      $r = ($rgb >> 16) & 0xFF; 
      $g = ($rgb >> 8) & 0xFF; 
      $b = $rgb & 0xFF; 
      $gray = ($r + $g + $b)/3; 
      if ($gray < 0xFF) { 

       imagesetpixel($im, $x, $y, 0xFFFFFF); 
      }else 
       imagesetpixel($im, $x, $y, 0x000000); 
     } 
    } 
    imagefilter($im, IMG_FILTER_NEGATE); 

} 
1

此功能将取代1或所有颜色为1新颜色,保持透明度级别(否则边界可能会看起来很糟糕,如果PARTIAL透明度用于绘制边界)。

COMPLETE ANSWER TO SIMILAR POST

<?php 

function colorizeKeepAplhaChannnel($inputFilePathIn, $targetRedIn, $targetGreenIn, $targetBlueIn, $outputFilePathIn) { 
    $im_src = imagecreatefrompng($inputFilePathIn); 
    $im_dst = imagecreatefrompng($inputFilePathIn); 
    $width = imagesx($im_src); 
    $height = imagesy($im_src); 

    // Note this: FILL IMAGE WITH TRANSPARENT BG 
    imagefill($im_dst, 0, 0, IMG_COLOR_TRANSPARENT); 
    imagesavealpha($im_dst,true); 
    imagealphablending($im_dst, true); 

    $flagOK = 1; 
    for($x=0; $x<$width; $x++) { 
     for($y=0; $y<$height; $y++) { 
      $rgb = imagecolorat($im_src, $x, $y); 
      $colorOldRGB = imagecolorsforindex($im_src, $rgb); 
      $alpha = $colorOldRGB["alpha"]; 
      $colorNew = imagecolorallocatealpha($im_src, $targetRedIn, $targetGreenIn, $targetBlueIn, $alpha); 

      $flagFoundColor = true; 
      // uncomment next 3 lines to substitute only 1 color (in this case, BLACK/greys) 
/* 
      $colorOld = imagecolorallocatealpha($im_src, $colorOldRGB["red"], $colorOldRGB["green"], $colorOldRGB["blue"], 0); // original color WITHOUT alpha channel 
      $color2Change = imagecolorallocatealpha($im_src, 0, 0, 0, 0); // opaque BLACK - change to desired color 
      $flagFoundColor = ($color2Change == $colorOld); 
*/ 

      if (false === $colorNew) { 
       //echo("FALSE COLOR:$colorNew alpha:$alpha<br/>"); 
       $flagOK = 0; 
      } else if ($flagFoundColor) { 
       imagesetpixel($im_dst, $x, $y, $colorNew); 
       //echo "x:$x y:$y col=$colorNew alpha:$alpha<br/>"; 
      } 
     } 
    } 
    $flagOK2 = imagepng($im_dst, $outputFilePathIn); 

    if ($flagOK && $flagOK2) { 
     echo ("<strong>Congratulations, your conversion was successful </strong><br/>new file $outputFilePathIn<br/>"); 
    } else if ($flagOK2 && !$flagOK) { 
     echo ("<strong>ERROR, your conversion was UNsuccessful</strong><br/>Please verify if your PNG is truecolor<br/>input file $inputFilePathIn<br/>"); 
    } else if (!$flagOK2 && $flagOK) { 
     $dirNameOutput = dirname($outputFilePathIn)."/"; 
     echo ("<strong>ERROR, your conversion was successful, but could not save file</strong><br/>Please verify that you have PERMISSION to save to directory $dirName <br/>input file $inputFilePathIn<br/>"); 
    } else { 
     $dirNameOutput = dirname($outputFilePathIn)."/"; 
     echo ("<strong>ERROR, your conversion was UNsuccessful AND could not save file</strong><br/>Please verify if your PNG is truecolor<br/>Please verify that you have PERMISSION to save to directory $dirName <br/>input file $inputFilePathIn<br/>"); 
    } 

    echo ("TargetName:$outputFilePathIn wid:$width height:$height CONVERTED:|$flagOK| SAVED:|$flagOK2|<br/>"); 
    imagedestroy($im_dst); 
    imagedestroy($im_src); 
} 

$targetRed = 255; 
$targetGreen = 255; 
$targetBlue = 0; 

//$inputFileName = 'frameSquareBlack_88x110.png'; 
$inputFileName = 'testMe.png'; 
$dirName = "../img/profilePics/"; 
$nameTemp = basename($inputFileName, ".png"); 
$outputFileName = $nameTemp."_$targetRed"."_$targetGreen"."_$targetBlue.png"; 
$inputFilePath = $dirName.$inputFileName; 
$outputFilePath = $dirName.$outputFileName; 

//echo "inputFileName:$inputFilePath<br>outputName:$outputFilePath<br>"; 
colorizeKeepAplhaChannnel($inputFilePath, $targetRed, $targetGreen, $targetBlue, $outputFilePath); 
?> 
<br/><br/> 
Original <br/> 
<img src="<?php echo $inputFilePath; ?>"> 
<br /><br />Colorized<br/> 
<img src="<?php echo $outputFilePath; ?>"> 
<br /> 
1

一个好办法做它使用paintOpaqueImage(),这也允许使用色容差

$targetColor = "#0074AD"; 
$fill = "#0074AA"; 
$tolerance = 30000; 

$im = new Imagick("yourimage.png"); 
if ($im->paintOpaqueImage ($targetColor , $fill , $tolerance)){ 

    $im->writeImage("yourimage.png"); 

} 

你可以看到http://www.imagemagick.org/script/command-line-options.php#fuzz