php图片转位图,使用PHP和ImageMagick以编程方式将RGB图像转换为1位灰度(b / w)位图图像…

  • Post author:
  • Post category:php


我正在尝试使用

PHP(版本:5.2.13)和ImageMagick(版本:6.7.8-7-Q16)以编程方式将位图RGB图像转换为1位灰度(b / w)位图图像.输入图像是位图,通过ImageMagick函数生成:

bool Imagick::setFormat ( string $format )

其中$format =’bmp2′

以下代码过去曾用过(旧版本的ImageMagick ……不记得哪一个),但它在当前环境中不再起作用了:

private function monochrome() {

if (isset($this->image)) {

try {

// reduce image colors to 2 (black and white)

$this->image->quantizeImage(2, Imagick::COLORSPACE_GRAY, 5, false, true);

// reduce image depth to 1 bit per pixel

$this->image->setImageDepth(1);

$this->image->setImageChannelDepth(Imagick::CHANNEL_ALL, 1);

// set image type to monochrome (2 colors: black and white only)

$this->image->setImageType(Imagick::IMGTYPE_BILEVEL);

}

catch (ImagickException $ie) {

throw $ie;

}

}

else {

throw new Exception(“No image object”);

}

}

问题是产生的图像仍然在RGB颜色空间中.

我也尝试过:

$this->image->setImageColorSpace(Imagick::COLORSPACE_GRAY);

但结果不会改变.

我的目标是为签名捕获应用程序生成尽可能小的黑白位图图像.我知道有比位图更好的图像格式,但生成的图像必须与旧的Access 97兼容.这就是为什么’bmp2’是图像格式的选择.

有任何想法吗?谢谢!