2011-04-22 45 views
0

若跌破该功能适用​​于大部分或根本没有。我试图通过像素循环并切换颜色,同时保持alpha。我认为我一直在工作,直到我将页面背景变成白色,并在图标周围看到一个小边框。PHP GD - 颜色替换使用Alpha给图像我不知道边界

我认为问题是,颜色被施加到半透明像素。因此,白色在图像的消除锯齿的部分上变成灰色。然后我决定在设置新颜色之前使像素完全透明。这没有做什么。

于是问题。为什么白色图像有一个小边框?

颜色转换函数。

function updateThumb($image, $newColor) { 
    $img = imagecreatefrompng($image); 

    $w = imagesx($img); 
    $h = imagesy($img); 

    // Work through pixels 
    for($y=0;$y<$h;$y++) { 
     for($x=0;$x<$w;$x++) { 
      // Apply new color + Alpha 
      $rgb = imagecolorsforindex($img, imagecolorat($img, $x, $y)); 

     $transparent = imagecolorallocatealpha($img, 0, 0, 0, 127); 
     imagesetpixel($img, $x, $y, $transparent); 

      $pixelColor = imagecolorallocatealpha($img, $newColor[0], $newColor[1], $newColor[2], $rgb['alpha']); 
      imagesetpixel ($img, $x, $y, $pixelColor); 
     } 
    } 

    // Restore Alpha 
    imageAlphaBlending($img, true); 
    imageSaveAlpha($img, true); 

    return $img; 
} 

大拇指需要一个翻转效果,所以图像是在下面的功能。我不认为这与问题有关,我试图改变上述功能来创建图像,而不是返回它,并发生同样的事情。

function makeThumb($path, $top, $bottom=FALSE) { 
    $width = imagesx($top); 
    $height = imagesy($top); 

    $thumbHeight = $bottom != FALSE ? $height * 2 : $height; 

    // Create Transparent PNG 
    $thumb = imagecreatetruecolor($width, $thumbHeight); 
    $transparent = imagecolorallocatealpha($thumb, 0, 0, 0, 127); 
    imagefill($thumb, 0, 0, $transparent); 

    // Copy Top Image 
    imagecopy($thumb, $top, 0, 0, 0, 0, $width, $height); 

    // Copy Bottom Image 
    if ($bottom != FALSE) { 
     imagecopy($thumb, $bottom, 0, $height, 0, 0, $width, $height); 
    } 

    // Save Image with Alpha 
    imageAlphaBlending($thumb, true); 
    imageSaveAlpha($thumb, true); 
    imagepng($thumb, $path); // save image as gif 

} 

函数被称为像这样...

$thumbTop = updateThumb('Path/To/Thumb', array(255,255,255)); 
makeThumb('output/path', $thumbTop); 

这是图标:http://img171.imageshack.us/i/iconkc.png/

任何想法?

回答

1

OMG THANK YOU FOR CODE OF此位!!!! :D还有其他的我也要感谢,但我无法在这个网站上找到他们的代码。我拿了你的代码和另一个人的代码并合并它们。我注意到,您保存图像,我还没有想出如何保存其他然后点击右键,通过浏览器保存的彩色图像,但图像会改变颜色。

function updateThumb($image, $newColor) { 
    $img = imagecreatefrompng($image); 

    $w = imagesx($img); 
    $h = imagesy($img); 

    // Work through pixels 
    for($y=0;$y<$h;$y++) { 
     for($x=0;$x<$w;$x++) { 
      // Apply new color + Alpha 
      $rgb = imagecolorsforindex($img, imagecolorat($img, $x, $y)); 

      $transparent = imagecolorallocatealpha($img, 0, 0, 0, 127); 
      imagesetpixel($img, $x, $y, $transparent); 


      // Here, you would make your color transformation. 
      $red_set=$newColor[0]/100*$rgb['red']; 
      $green_set=$newColor[1]/100*$rgb['green']; 
      $blue_set=$newColor[2]/100*$rgb['blue']; 
      if($red_set>255)$red_set=255; 
      if($green_set>255)$green_set=255; 
      if($blue_set>255)$blue_set=255; 

      $pixelColor = imagecolorallocatealpha($img, $red_set, $green_set, $blue_set, $rgb['alpha']); 
      imagesetpixel ($img, $x, $y, $pixelColor); 
     } 
    } 

    // Restore Alpha 
    imageAlphaBlending($img, true); 
    imageSaveAlpha($img, true); 

    return $img; 
} 

function makeThumb($path, $top, $bottom=FALSE) { 
    $width = imagesx($top); 
    $height = imagesy($top); 

    $thumbHeight = $bottom != FALSE ? $height * 2 : $height; 

    // Create Transparent PNG 
    $thumb = imagecreatetruecolor($width, $thumbHeight); 
    $transparent = imagecolorallocatealpha($thumb, 0, 0, 0, 127); 
    imagefill($thumb, 0, 0, $transparent); 

    // Copy Top Image 
    imagecopy($thumb, $top, 0, 0, 0, 0, $width, $height); 

    // Copy Bottom Image 
    if ($bottom != FALSE) { 
     imagecopy($thumb, $bottom, 0, $height, 0, 0, $width, $height); 
    } 

    // Save Image with Alpha 
    imageAlphaBlending($thumb, true); 
    imageSaveAlpha($thumb, true); 
    header('Content-Type: image/png'); 
    imagepng($thumb, $path); // save image as png 

} 

$thumbTop = updateThumb('input/path', array(240,105,15)); 
makeThumb('output/path', $thumbTop); 
1

谢谢!你激励我在我正在构建的图像工作者中逐像素地使用像素。 起初,我有同样的问题,但我想我找到了如何解决它。

问题

,在我看来,好像imagesetpixel不更换新的RGBA值的像素,而是把他们交给旧的。

解决方案

function updateThumb($image, $newColor) { 
    $img = imagecreatefrompng($image); 

    $w = imagesx($img); 
    $h = imagesy($img); 

    // create a new blank image. 
    $target = imagecreatetruecolor($w, $h); 
    $transparent = imagecolorallocatealpha($target, 0, 0, 0, 127); 
    imagefill($target, 0, 0, $transparent); 

    // Work through pixels 
    for($y=0;$y<$h;$y++) { 
     for($x=0;$x<$w;$x++) { 
      // Apply new color + Alpha 
      $rgb = imagecolorsforindex($img, imagecolorat($img, $x, $y)); 

      // and set the new color to the blank image. 
      $pixelColor = imagecolorallocatealpha($target, $newColor[0], $newColor[1], $newColor[2], $rgb['alpha']); 
      imagesetpixel($target, $x, $y, $pixelColor); 
     } 
    } 

    // Restore Alpha 
    imageAlphaBlending($target, true); 
    imageSaveAlpha($target, true); 

    return $target; 
} 

出了什么问题:

// new image 
$target = imagecreatetruecolor(1, 1); 

// Set a Pixel 
$pixelColor = imagecolorallocatealpha($target, 0, 0, 0, 60); 
imagesetpixel($target, 0, 0, $pixelColor); 

// "Replace" The Pixel 
$pixelColor = imagecolorallocatealpha($target, 255, 255, 255, 60); 
imagesetpixel($target, 0, 0, $pixelColor); 

// See the rgba values of the pixel 
var_dump(imagecolorsforindex($target, imagecolorat($target, 0, 0))); 

预计:

array(4) { 
    ["red"]=> int(255) 
    ["green"]=> int(255) 
    ["blue"]=> int(255) 
    ["alpha"]=> int(60) 
} 

实际:

array(4) { 
    ["red"]=> int(174) 
    ["green"]=> int(174) 
    ["blue"]=> int(174) 
    ["alpha"]=> int(28) 
} 
+0

直到我找到这个,我一直在努力解决同样的问题。谢谢! – 2017-09-29 10:43:02