我可以使用 PHP 中的 GD 库交换图像中的颜色吗?
I got the image like this (it's a graph):
(source: kitconet.com)
I want to change the colours, so the white is black, the graph line is light blue, etc.. is it possible to achieve with GD and PHP?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
这会将白色替换为灰色
This will replace the white color with Gray
我在使这个解决方案发挥作用时遇到了麻烦。 该图像不能是真彩色图像。 首先使用 imagetruecolortopalette() 进行转换;
I had trouble making this solution work. The image cannot be a true color image. Convert it first with imagetruecolortopalette();
我知道这已经晚了,而且是事后发生的,但我已经编写了一个脚本,可以在更大的范围内完成此任务。 希望看到这篇文章的人可以使用它。 它需要许多单色图层的源图像(您的选择)。 您为其提供源颜色和每层的色调,脚本将返回专门根据您提供的十六进制代码着色的合成图像(具有完全透明度)。
查看下面的代码。 更详细的解释可以在我的 博客。
I know this is late and after the fact, but I've put together a script that will do this on a slightly larger scale. Hopefully someone who comes across this post can use it. It takes an number of source images that are one-color layers (your choice). You provide it with a source color and the tint of each layer and the script returns a composite image (with full transparency) colored specifically to your provided hex code.
Check out the code below. A more detailed explanation can be found on my blog.
我自己没有尝试过,但你可以看看函数 imagecolorset()< /a> 在 GD 库中它具有类似颜色填充的效果,这可以帮助处理白色背景。
I haven't tried it myself but you can look at the function imagecolorset() in the GD library It does a color fill like effect, that could help with the white background.
您可以尝试 imagefilter 函数 http://lv.php.net/imagefilter - 但这不会让您可以直接将一种颜色替换为另一种颜色,只需更改 r/g/b 组件即可。
可以使用 imagesetpixel http://nl2.php.net/imagesetpixel 实现非常低级别的解决方案设置新的像素值。
You could try the imagefilter function http://lv.php.net/imagefilter - but that will not give your direct access to replace one color with another, just changing the r/g/b components.
A very low level solution could be implemented using imagesetpixel http://nl2.php.net/imagesetpixel to set the new pixel values.
IMG_FILTER_NEGATE:反转图像的所有颜色。
http://www.php.net/manual/en/function.imagefilter.php
这可能是一个解决方案吗?
IMG_FILTER_NEGATE: Reverses all colors of the image.
http://www.php.net/manual/en/function.imagefilter.php
Could that be a solution?
缓慢但可靠的方法,迭代每个像素。
The slow but sure approach, iterating over every pixel.
我不知道有任何现成的功能。 但我想你可以遍历图像的每个像素并改变它的颜色......
I'm not aware of any ready-made functions. But I suppose you could go trough every pixel of the image and change it's colour...