繁体   English   中英

使用php gd库将彩色图像转换成黑白图像

[英]convert color image into black and white image using php gd library

即时通讯有一个页面,用户可以在其中上传其图像以转换为黑白图像。

我尝试了一些来自互联网的代码,但是当我上传一些gif时,它仅适用于jpeg图像

或png图像,它显示全黑图像或具有相同色标的相同图像...

任何代码表示赞赏。

我不能告诉您,因为您没有向我们显示代码,但是听起来您需要在从GD中初始化图像之前测试您从用户那里获得的图像类型。

我敢打赌您对此功能有疑问

$ image = imagecreatefromjpeg ($ filename);

您需要测试图像类型,并可以选择调用以下函数之一

imagecreatefrompng ($ filename)

imagecreatefromgif ($ filename)

尝试这个

if ($img = imagecreatefrompng($file))
else if ($img = imagecreatefromjpeg($file))

...


使用imagecolorallocatealpha()函数分配颜色,同时跟踪背景颜色。

例:

 $color = imagecolorallocatealpha($image, 0, 0, 0,127); // transparent color

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM