我正在寻找从具有以下内容的字符串编写 1 位位图的可能性:
$str = "001011000111110000";
零是白色,一是黑色。BMP 文件将为 18 x 1 像素。
我不想要 24 位 BMP,而是真正的 1 位 BMP。
有谁知道 PHP 中的标头和转换方法?
有点奇怪的要求:)
所以,首先,你想在这里使用的是php-gd。通常,在任何具有体面存储库的操作系统上安装 php 时,都会包含此功能,但以防万一它不适合您,您可以在此处获取安装说明;
http://www.php.net/manual/en/image.setup.php
首先,我们需要弄清楚图像的宽度需要多大;高度显然总是一个。
所以;
$str = $_GET['str'];
$img_width = strlen($str);
strlen 会告诉我们 $str 字符串中有多少个字符,并且由于我们为每个字符提供一个像素,因此字符数量将给我们所需的宽度。
为了便于访问,请将字符串拆分为一个数组 - 每个元素对应每个单独的像素。
$color_array = str_split($str);
现在,让我们设置一个"指针",我们要绘制到哪个像素。它是php,所以你不需要发起这个,但整洁是件好事。
$current_px = (int) 0;
现在您可以初始化GD并开始制作图像;
$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.
}
这将绘制您的图像,然后您需要做的就是显示它;
header('Content-type: image/png');
imagepng($im);
imagedestroy($im);
请注意,根本不需要$white声明 - 我只是把它留了下来,让你了解如何使用 gd 声明不同的颜色。
在使用之前,您可能需要对其进行一些调试 - 我已经很久没有使用 GD 了。无论如何,希望这有帮助!
这不是一个奇怪的要求。
我完全同意这个问题的目的,实际上我必须管理一些 1 位单色图像。
答案是:
- GD在PHP网站上没有很好的记录。
- 当您想从头开始创建映像时,您必须使用
imagecreate()
或imagecreatetruecolor()
- 似乎刚才提到的两种方法(函数)都无法从头开始创建1位图像。
- 我通过创建一个外部图像来解决,1 位单色 png,并加载它
imagecreatefrompng()
.
另外:我刚刚从这里下载了官方库开源代码官方Bitbucket存储库
我在gd.h
中发现了什么?
上面提到的函数的定义:
/* 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);
所以"官方"的解决方案是:创建一个带有 imagecreate()
的 2 调色板图像(包装 gdImageCreate()
GD 功能)。
"替代"解决方案是创建一个外部图像,1 位单色 png,并且如上所述imagecreatefrompng()
。
要创建没有gd或imagemagick的单色位图图像,您可以使用pack将机器字节顺序转换为小端字节顺序以及一些用于处理字符串的函数,对于参考和更多详细信息,您可以在3v4l上检查维基百科页面位图文件格式或此脚本。
对于此示例,我将使用更复杂的输入,这只是为了更好地解释在创建位图时应如何对齐每行;
<?php
$pixelDataArray = array(
"11101010111",
"10101010101",
"11101110111",
"10001010100",
"10001010100",
);
首先要将输入转换为像素数组或位图数据,像素阵列上的每一行都应对齐 dword/32bit/4bytes。
$pixelWidth = strlen($pixelDataArray[0]);
$pixelHeight = count($pixelDataArray);
$dwordAlignment = 32 - ($pixelWidth % 32);
if ($dwordAlignment == 32) {
$dwordAlignment = 0;
}
$dwordAlignedLength = $pixelWidth + $dwordAlignment;
现在我们可以正确对齐字符串,然后将其转换为 1 字节整数数组,然后转换为二进制字符串。
$pixelArray = '';
foreach (array_reverse($pixelDataArray) as $row) {
$dwordAlignedPixelRow = str_pad($row, $dwordAlignedLength, '0', STR_PAD_RIGHT);
$integerPixelRow = array_map('bindec', str_split($dwordAlignedPixelRow, 8));
$pixelArray .= implode('', array_map('chr', $integerPixelRow));
}
$pixelArraySize = 'strlen($pixelArray);
然后让我们构建颜色表
$colorTable = pack(
'CCCxCCCx',
//blue, green, red
255, 255, 255, // 0 color
0, 0, 0 // 1 color
);
$colorTableSize = 'strlen($colorTable);
现在将使用位图信息标头,以便更好地支持BITMAPINFOHEADER(40字节标头)。
$dibHeaderSize = 40;
$colorPlanes = 1;
$bitPerPixel = 1;
$compressionMethod = 0; //BI_RGB/NONE
$horizontal_pixel_per_meter = 2835;
$vertical_pixel_per_meter = 2835;
$colorInPalette = 2;
$importantColors = 0;
$dibHeader = 'pack('VVVvvVVVVVV', $dibHeaderSize, $pixelWidth, $pixelHeight, $colorPlanes, $bitPerPixel, $compressionMethod, $pixelArraySize, $horizontal_pixel_per_meter, $vertical_pixel_per_meter, $colorInPalette, $importantColors);
最后一部分是文件头
$bmpFileHeaderSize = 14;
$pixelArrayOffset = $bmpFileHeaderSize + $dibHeaderSize + $colorTableSize;
$fileSize = $pixelArrayOffset + $pixelArraySize;
$bmpFileHeader = pack('CCVxxxxV', 'ord('B'), 'ord('M'), $fileSize, $pixelArrayOffset);
现在只需将所有字符串连接成一个字符串,就可以使用了。
$bmpFile = $bmpFileHeader . $dibHeader . $colorTable . $pixelArray;
$bmpBase64File = base64_encode($bmpFile);
?>
<img src="data:image/bitmap;base64, <?= $bmpBase64File ?>" style="image-rendering: crisp-edges;width: 100px;"/>
<img src="data:image/bitmap;base64, Qk1SAAAAAAAAAD4AAAAoAAAACwAAAAUAAAABAAEAAAAAABQAAAATCwAAEwsAAAIAAAAAAAAA////AAAAAACKgAAAioAAAO7gAACqoAAA6uAAAA==" style="image-rendering: crisp-edges;width: 100px;height: ;"/>