How to create BMP with 4 bits in PHP 7?

48 Views Asked by At

I'm trying to create a BMP image in PHP 7 using Imagick which works, but the save quality is always 32 bit, which I don't want. I need to save BMP in 4 bit quality. I do this using $image->setImageDepth(4); which doesn't work. Please advise how to do this?

I tried Imagick and it always gives BMP in 32bit.

1

There are 1 best solutions below

2
sinyorridak On

To save a BMP image in 4-bit quality in PHP, you can set the image colorspace to GRAY and the image depth to 4.

$image = new Imagick();
$image->newImage(100, 100, 'gray');
$image->setImageColorspace(Imagick::COLORSPACE_GRAY);
$image->setImageDepth(4);
$image->writeImage('yol/oluşturulan/resim.bmp');

You can replace the 'yol/oluşturulan/resim.bmp' part with the section where you want to save the BMP image.

Sorry google traslate :)

Update.

$image = new Imagick();
$image->newImage(100, 100, 'white');

// Siyah rengi için pikselleri siyah yapın
$image->setImageColorspace(Imagick::COLORSPACE_RGB);
$image->opaquePaintImage('white', 'black', 0, false);

// Kırmızı renk için pikselleri kırmızıya boyayın
$image->setImageColorspace(Imagick::COLORSPACE_RGB);
$image->opaquePaintImage('black', 'red', 0, false);

$image->setImageDepth(4);
$image->writeImage('path/to/save/image.bmp');

In this example, we create an image with a white background and a size of 100x100 pixels. Then, we set the color space to RGB and paint the white pixels black first, and then paint the black pixels red.

Sorry google translate :)