在PHP中创建1位位图(单色) [英] Create 1 bit bitmap (monochrome) in php

查看:550
本文介绍了在PHP中创建1位位图(单色)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在寻找来自与此内容的字符串写入一个1位的位图的可能性:

I'm looking for the possibility of write a 1 bit bitmap from a string with this content:

$str = "001011000111110000";

零是白色的,一个是黑色的。
BMP文件将是18×1像素。

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

我不想要一个24位BMP,而是实实在在的1位BMP。

I don't want a 24bit BMP, but a real 1bit BMP.

有谁知道页眉和PHP中的转换方法?

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

推荐答案

这是一个奇怪的要求一点点:)

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

那么,你会想在这里使用是php-GD,一开始。通常,这与体面回购的任何操作系统安装PHP时包括在内,但只是柜面它不适合你,你可以在这里安装说明;

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.

所以;

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

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);

现在,让我们建立了一个指针,为此像素我们绘图。这是PHP的,所以你不必initalise这一点,但它是很好的整齐。

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;

现在你可以初始化GD,并开始做形象;

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);

请注意,在所有不需要$白色的声明 - 我刚刚离开它给你的你如何与GD声明不同的颜色的想法

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.

您可能会需要使用它之前有点调试这个 - 这是因为我已经用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!

这篇关于在PHP中创建1位位图(单色)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆