Create 1 bit bitmap (monochrome) in php

丶灬走出姿态 提交于 2019-12-23 08:48:06

问题


I'm looking for the possibility of write a 1 bit 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 don't want a 24bit BMP, but a real 1bit BMP.

Does anyone know the header and the conversion method in PHP?


回答1:


That's a little bit of a strange request :)

So, what you'd want to use here is php-gd, for a start. Generally this is included when installing php on any OS with decent repo's, but just incase it isn't for you, you can get the installation instructions here;

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

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

So;

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

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

For ease of access, split the string into an array - with each element for each separate pixel.

$color_array = str_split($str);

Now, let's set up a "pointer", for which pixel we're drawing to. It's php so you don't NEED to initalise this, but it's nice to be tidy.

$current_px = (int) 0;

And now you can initialise GD and start making the 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 do is display it;

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

Note that the $white declaration isn't needed at all - I just left it in to give you an idea of how you declare different colours with gd.

You'll probably need to debug this a bit before using it - it's been a long time since I've used GD. Anyway, hope this helps!




回答2:


That's NOT a strange request.

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

The answer is:

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

In addition: I've just downloaded the official library open source code from hereOfficial Bitbucket Repository

What I've found in gd.h?:

The definition of the upper mentioned 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: create a 2 colour palette image with imagecreate() (that wraps the gdImageCreate() GD function).

The "alternative" solution is to create an external image, 1bit monochrome png, and it with imagecreatefrompng() as I said above.



来源:https://stackoverflow.com/questions/11167706/create-1-bit-bitmap-monochrome-in-php

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!