替换图像颜色并保持 100% Alpha 背景
我有一个透明背景的 png。前景是单色的。我想在保留 100% Alpha 背景的同时更改此单色。
下面的代码不起作用,当我有 gif 时它起作用了。然而,当我将 gif 更改为 png 然后添加 alpha 内容时,颜色更改失败了。
如果我添加 imagetruecolortopalette($im, false, 255);
颜色会改变,但 alpha 会丢失。
有人可以告诉我哪里出错了请
$imgname = "block.png";
$im = imagecreatefrompng($imgname);
imagealphablending($im, false);
$index = imagecolorclosest ($im, 153,153,153); // get Grey COlor
imagecolorset($im,$index,0,0,0); // SET NEW COLOR
imageAlphaBlending($im, true);
imageSaveAlpha($im, true);
$imgname = "result.png";
imagepng($im, $imgname ); // save image as gif
imagedestroy($im);
编辑::这是一个足够简单的脚本。
当我使用 gif 时,颜色发生了变化并保存了新图像。当 a 更改为 .png 时。颜色不再改变,因此原始图像通过脚本时没有变化。
I have a png with a transparent background. The foreground is a single color. I want to change this single color while preserving the 100% alpha background.
The code below doesn't work, it worked when I had a gif. However, when I changed the gif to a png then added the alpha stuff the color change broke down.
If I add imagetruecolortopalette($im, false, 255);
the color is changed but alpha is lost.
Can someone show me where I am going wrong please
$imgname = "block.png";
$im = imagecreatefrompng($imgname);
imagealphablending($im, false);
$index = imagecolorclosest ($im, 153,153,153); // get Grey COlor
imagecolorset($im,$index,0,0,0); // SET NEW COLOR
imageAlphaBlending($im, true);
imageSaveAlpha($im, true);
$imgname = "result.png";
imagepng($im, $imgname ); // save image as gif
imagedestroy($im);
EDIT :: This is a simple enough script.
When I used a gif, the color changed and the new image was saved. When a changed to a .png. The color no longer changed and thus the original image passes through the script unchanged.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
它是 PNG-24 吗?据我所知,这只适用于 PNG-8。也许你可以尝试使用 Imagick
Is it a PNG-24? As far as I believe that would only work with PNG-8. Maybe you could try to use Imagick