Create a single bit bitmap (monochrome) in php

I am looking for an opportunity to write 1 bit of a bitmap from a string with this content:

$str = "001011000111110000"; 

Zero is white and one is black. The BMP file will be 18 x 1 px.

I do not need a 24-bit BMP, but a real 1-bit BMP.

Does anyone know the header and conversion method in PHP?

+7
source share
2 answers

This is a bit strange request :)

So, you would like to use php-gd to get started. This is usually included when installing php on any OS with a decent repo, but just remember that this is not for you, you can get installation instructions here;

http://www.php.net/manual/en/image.setup.php

First, we need to figure out how large the image will be in width; height, obviously, will always be one.

So,

 $str = $_GET['str']; $img_width = strlen($str); 

strlen will tell us how many characters there are in the string $ str, and since we give one pixel per character, the number of characters will give us the required width.

To facilitate access, divide the string into an array - with each element for each individual pixel.

 $color_array = str_split($str); 

Now set up the "pointer" for which we draw a pixel. This is php, so you DON'T NEED to do this, but it's nice to be careful.

 $current_px = (int) 0; 

And now you can initialize GD and start making an image;

 $im = imagecreatetruecolor($img_width, 1); // Initialise colours; $black = imagecolorallocate($im, 0, 0, 0); $white = imagecolorallocate($im, 255, 255, 255); // Now, start running through the array foreach ($color_array as $y) { if ($y == 1) { imagesetpixel ( $im, $current_px , 1 , $black ); } $current_px++; // Don't need to "draw" a white pixel for 0. Just draw nothing and add to the counter. } 

This will draw your image, then all you need to do is display it;

 header('Content-type: image/png'); imagepng($im); imagedestroy($im); 

Note that the $ white declaration is not needed at all - I just left it to give you an idea of ​​how you declare different colors with gd.

You will probably have to debug this a bit before using it - this has been a long time since I used GD. Anyway, hope this helps!

+3
source

This is NOT a weird request.

I completely agree with the purpose of the question, in fact I have to manage some 1-bit monochrome images.

Answer:

  • GD is not well documented on the PHP website.
  • If you want to create an image from scratch, you must use imagecreate() or imagecreatetruecolor()
  • It seems that both of the just mentioned methods (functions) cannot create 1bit images from scratch.
  • I decided to create an external image, 1 bit monochrome png, loading it with imagecreatefrompng() .

Also: I just downloaded the open source official library Official Bitbucket repository

What did I find in gd.h ?:

Definition of top functions:

 /* Functions to manipulate images. */ /* Creates a palette-based image (up to 256 colors). */ BGD_DECLARE(gdImagePtr) gdImageCreate (int sx, int sy); /* An alternate name for the above (2.0). */ \#define gdImageCreatePalette gdImageCreate /* Creates a truecolor image (millions of colors). */ BGD_DECLARE(gdImagePtr) gdImageCreateTrueColor (int sx, int sy); 

So, the “official” solution is to create an image with a color palette with imagecreate() (which wraps the gdImageCreate() GD function).

An “alternative” solution is to create an external image, 1 bit monochrome png and its imagecreatefrompng() , as I said above.

0
source

All Articles