I can generate the desired 1bpp image with convert using the monochrome option:
convert -font URWGothicBook -pointsize 10 -size 128x48 -monochrome label:"Test text" text.bmp
But I cannot find an equivalent to monochrome in IMagick? I have tried several other options, but I still get 24bpp output.
Here is what I have tried so far - any suggestions would be appreciated!
I know I could use exec with convert, but I would like to use IMagick if possible.
Code: Select all
$wtext = "This is test text";
/* Create Imagick objects */
$image = new Imagick();
$draw = new ImagickDraw();
$color = new ImagickPixel('black');
/* Font properties */
$draw->setFontSize(12);
$draw->setFillColor($color);
$draw->setStrokeAntialias(true);
$draw->setTextAntialias(true);
/* Get font metrics */
$metrics = $image->queryFontMetrics($draw, $wtext);
/* Create text */
$draw->annotation(0, $metrics['ascender'], $wtext);
/* Create image */
$image->newImage(128, 48, 'white');
$image->setImageDepth(1);
$image->setImageColorSpace(imagick::COLORSPACE_GRAY);
$image->setImageType(imagick::IMGTYPE_BILEVEL);
$image->setImageFormat('bmp');
$image->drawImage($draw);
/* Output the image */
header("Content-Type: image/bmp");
echo $image;