简体   繁体   English

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

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

im having a page in which users upload their images to convert into black and white image.. 即时通讯有一个页面,用户可以在其中上传其图像以转换为黑白图像。

i tried some of the code from internet but it works only for jpeg image when i upload some gif 我尝试了一些来自互联网的代码,但是当我上传一些gif时,它仅适用于jpeg图像

or png images it shows an full black image or the same image with the same color scale... 或png图像,它显示全黑图像或具有相同色标的相同图像...

any codes appreciated. 任何代码表示赞赏。

i cant tell because you didnt show us the code but it sounds like you need to test for the image type you got from the user before you init the image in GD. 我不能告诉您,因为您没有向我们显示代码,但是听起来您需要在从GD中初始化图像之前测试您从用户那里获得的图像类型。

i bet you have a problem with this function 我敢打赌您对此功能有疑问

$image = imagecreatefromjpeg ($filename); $ image = imagecreatefromjpeg ($ filename);

you need to test for image type and optionally call one of these function 您需要测试图像类型,并可以选择调用以下函数之一

imagecreatefrompng ($filename) imagecreatefrompng ($ filename)

imagecreatefromgif ($filename) imagecreatefromgif ($ filename)

try this 尝试这个

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

... ...


Use the imagecolorallocatealpha() function to allocate color while keeping track of the background color. 使用imagecolorallocatealpha()函数分配颜色,同时跟踪背景颜色。

Example: 例:

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

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

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