Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/288.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在php中创建1位位图(单色)_Php_Bitmap_Bit - Fatal编程技术网

在php中创建1位位图(单色)

在php中创建1位位图(单色),php,bitmap,bit,Php,Bitmap,Bit,我正在寻找从包含以下内容的字符串编写1位位图的可能性: $str = "001011000111110000"; 零是白色的,一是黑色的。 BMP文件将为18x1px 我不想要24位BMP,而是真正的1位BMP 有人知道PHP中的头和转换方法吗 这是一个有点奇怪的请求:) 首先,您希望在这里使用的是PHPGD。一般来说,这是包括当安装在任何操作系统与体面的回购的php,但只是以防万一它不是为你,你可以在这里得到安装说明 首先,我们需要计算出图像的宽度需要有多大;高度显然永远是一 所以, st

我正在寻找从包含以下内容的字符串编写1位位图的可能性:

$str = "001011000111110000";
零是白色的,一是黑色的。 BMP文件将为18x1px

我不想要24位BMP,而是真正的1位BMP


有人知道PHP中的头和转换方法吗

这是一个有点奇怪的请求:)

首先,您希望在这里使用的是PHPGD。一般来说,这是包括当安装在任何操作系统与体面的回购的php,但只是以防万一它不是为你,你可以在这里得到安装说明

首先,我们需要计算出图像的宽度需要有多大;高度显然永远是一

所以,

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()
    加载它来解决这个问题
另外:我刚刚从这里下载了官方图书馆的开源代码

我在
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()
(包装
gdImageCreate()
GD函数)创建一个双色调色板图像


“替代”解决方案是创建一个外部图像,1比特单色png,并使用
imagecreatefrompng()

要创建没有gd或imagemagick的单色位图图像,您可以使用将机器字节顺序转换为小端字节顺序和一些处理字符串的函数,有关参考和更多详细信息,您可以查看Wikipedia页面或上的此脚本

对于本例,我将使用更复杂的输入,这只是为了更好地解释创建位图时应如何对齐每一行

<?php
$pixelDataArray = array(
    "11101010111",
    "10101010101",
    "11101110111",
    "10001010100",
    "10001010100",
);
现在我们可以正确对齐字符串,然后将其转换为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);
现在,为了更好地支持位图信息头,将使用BitMapInfo头(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;"/>
$bmpFile=$bmpFileHeader$迪布希德$可着色的$像素阵列;
$bmpBase64File=base64_编码($bmpFile);
?>
“style=”图像渲染:清晰的边缘;宽度:100px;"/>

+1有趣的问题!感谢您的回复,但我正在寻找一个bmp生成器1比特单色,可能是纯php。最后我找到了正确的方法和解决方案,现在我可以生成一个正确的1比特bmp单色图像!使用此解决方案,我可以生成一个具有最低可能尺寸的条形码光栅图像对于打印裂土器。啊,我明白了。我发现了一些类可以帮助您使用GD创建BMP图像;您可以在这里找到这些:我希望其余的信息至少对您有用:)您可以给出代码示例“我通过创建外部图像,1比特单色png,使用imagecreatefrompng()加载它来解决问题。”
$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;"/>