I am trying to convert a color pdf to black and white pdf. I am going to use the pdf to send it in fax and I am using Twilio and it is explicitly converting the color pdf to monochrome pdf however, I want to do it in my server-side to be able to preview the outcome.
As I have Imagick, and found some topics mainly on Imagick, I wanted to give it a try but I couldn't find the necessary class in Imagick. I found some for grayscale but fax is explicitly black & white (monochrome), so no identical to the fax case.
Closest I could find was:
->transformImageColorSpace(\Imagick::COLORSPACE_SRGB) and
->setImageColorSpace(Imagick::COLORSPACE_GRAY)
But these are grayscale, not monochrome.
Also, on Imagick formums, I found these commands but I don't want to execute with shell_exec (as I've read there are couple of down-sides)
// I am pretty sure this one should work, but couldn't find how to do it in php:
convert -density 288 in.pdf -resize 25% out.png
// thus this one:
convert -density 600 in.pdf -threshold 15% -type bilevel -compress fax out.pdf
// Also found this one:
convert -density 288 image.pdf -resize 25% -threshold 50% -type bilevel image.tiff
How to achieve what I am trying to achieve in php either using above commands or any other php compatible way? How does Twilio do it?
Update:
Expected output (how Twilio does it):
Using below answer:
$img = new Imagick('path/to/document.pdf');
$img->quantizeImage(2, // Number of colors
Imagick::COLORSPACE_GRAY, // Colorspace
50, // Depth tree
TRUE, // Dither
FALSE); // Error correction
$img->writeImage('path/to/output.png')
Update 2:
Using $img->quantizeImage(2, Imagick::COLORSPACE_GRAY, 1, TRUE, FALSE);




