PHP:灰度 jpg、gif、png...也带有 alpha 通道?
$im = ImageCreateFromString(file_get_contents($source_file)); ImageFilter($im, IMG_FILTER_GRAYSCALE);
知道我能做什么,正确地对 gif 和 png 进行灰度化并具有透明度吗?这个片段实际上效果很好,它将 jpg 和 png 转换为灰度。然而 gif 有点“错误”——它们并不总是有效,这取决于图像。有时其中还残留一些浅色。此外,此代码片段不适用于 alpha 通道。如果我转换具有透明度的 gif 或 png,透明部分总是会变黑。
当然,我正在查询图像类型,并在对其进行“灰度化”之后,我将再次设置正确的类型。
你有什么想法吗?
$im = ImageCreateFromString(file_get_contents($source_file));
ImageFilter($im, IMG_FILTER_GRAYSCALE);
any idea what i could do, to properly grayscale gifs and pngs with transperancy? This snippet actually works good, it transforms jpgs and pngs to grayscale. However gifs are a little bit "buggy" - they don't always work, it depends on the image. Sometimes there are a few pale colors left in them. Moreover this snippet doesn't work with alpha-channels. If i convert a gif or a png with transparancy the transparent parts always get blackened.
Of course im querying the image-type and after "grayscaling" it, i'll set the proper type again.
Have you any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
此代码应该保留 alpha,但它比 imagefilter 慢:
如果您仍然遇到问题,请尝试在图像创建之后(在 $width=.. 之前)编写此代码:
This code should preserve the alpha, but it's slower than imagefilter:
If you still have problems try to write this after the image creation (before the $width=..):
对于 png,简单调用 imagesavealpha() 即可解决 alpha 通道上黑色像素的问题,完整代码:
For pngs a simple call to imagesavealpha() solves the problem of black pixels on the alpha channel, complete code: