我不确定以下功能是否适用于大部分功能或根本不运行。我想要的是循环像素并在保持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/
有什么想法吗?
答案 0 :(得分:1)
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 :(得分: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)
}