我正在寻找从这个内容的字符串写一个1位位图的可能性:
$str = "001011000111110000";
Run Code Online (Sandbox Code Playgroud)
零是白色,一个是黑色.BMP文件为18 x 1 px.
我不想要24位BMP,而是真正的1位BMP.
有谁知道PHP中的标题和转换方法?
这是一个有点奇怪的请求:)
因此,您首先要在这里使用 php-gd。通常,在具有体面 repo 的任何操作系统上安装 php 时都会包含此内容,但以防万一它不适合您,您可以在此处获取安装说明;
http://www.php.net/manual/en/image.setup.php
首先,我们需要确定图像的宽度需要多大;高度显然永远是一。
所以;
$str = $_GET['str'];
$img_width = strlen($str);
Run Code Online (Sandbox Code Playgroud)
strlen 将告诉我们 $str 字符串中有多少个字符,并且由于我们为每个字符提供一个像素,因此字符数量将提供所需的宽度。
为便于访问,将字符串拆分为一个数组 - 每个元素用于每个单独的像素。
$color_array = str_split($str);
Run Code Online (Sandbox Code Playgroud)
现在,让我们设置一个“指针”,用于我们绘制的像素。它是 php,所以你不需要初始化它,但保持整洁是很好的。
$current_px = (int) 0;
Run Code Online (Sandbox Code Playgroud)
现在您可以初始化 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.
}
Run Code Online (Sandbox Code Playgroud)
这将绘制您的图像,然后您需要做的就是显示它;
header('Content-type: image/png');
imagepng($im);
imagedestroy($im);
Run Code Online (Sandbox Code Playgroud)
请注意,根本不需要 $white 声明——我只是把它留在这里,让您了解如何使用 gd 声明不同的颜色。
在使用它之前,您可能需要对其进行一些调试 - 自从我使用 GD 以来已经有很长时间了。无论如何,希望这会有所帮助!
| 归档时间: |
|
| 查看次数: |
2960 次 |
| 最近记录: |