若跌破该功能适用于大部分或根本没有。我试图通过像素循环并切换颜色,同时保持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/
任何想法?
直到我找到这个,我一直在努力解决同样的问题。谢谢! – 2017-09-29 10:43:02