- 把文字生成1位深度的bmp图 并保存服务器 获取到地址
function createBmpImage($input_text,$font_file="public/assets/fonts/simsun.ttc",$width=128,$height=32,$fontSize=12)
{
$bmp = imagecreate($width, $height);
$white = imagecolorallocate($bmp, 255, 255, 255);
$text_color = imagecolorallocate($bmp, 0, 0, 0);
imagefill($bmp, 0, 0, $white);
$font_file =ROOT_PATH. $font_file;
$textX = ($width - imagettfbbox($fontSize, 0, $font_file, $input_text)[4]) / 2;
$textY = ($height - imagettfbbox($fontSize, 0, $font_file, $input_text)[7]) / 2;
imagettftext($bmp, $fontSize, 0, $textX, $textY, $text_color, $font_file, $input_text);
$savekey = "/uploads/".date('Y').date('m').date('d')."/";
$savekey = '/' . ltrim($savekey, '/');
$uploadDir = substr($savekey, 0, strripos($savekey, '/') + 1);
$fileName = uniqueNo().'.bmp';
$destDir = ROOT_PATH . 'public' . str_replace('/', DS, $uploadDir);
if (!is_dir($destDir)) {
@mkdir($destDir, 0755, true);
}
$destFile = $destDir . $fileName;
imagebmp_custom($bmp,$destFile,1);
$url=['url'=>$savekey.$fileName,'root_url'=>$destFile];
return $url??[];
}
function imagebmp_custom(&$im, $filename = '', $bit = 8, $compression = 0)
{
if (!in_array($bit, array(1, 4, 8, 16, 24, 32))) {
$bit = 8;
} else if ($bit == 32)
{
$bit = 24;
}
$bits = pow(2, $bit);
imagetruecolortopalette($im, true, $bits);
$width = imagesx($im);
$height = imagesy($im);
$colors_num = imagecolorstotal($im);
$rgb_quad = '';
if ($bit <= 8)
{
for ($i = 0; $i < $colors_num; $i++) {
$colors = imagecolorsforindex($im, $i);
$rgb_quad .= chr($colors['blue']) . chr($colors['green']) . chr($colors['red']) . "\0";
}
$bmp_data = '';
if ($compression == 0 || $bit < 8) {
$compression = 0;
$extra = '';
$padding = 32 - ($width * $bit) % 32;
if ($padding % 32 != 0) {
$extra = str_repeat("\0", $padding);
}
for ($j = $height - 1; $j >= 0; $j--) {
$i = 0;
while ($i < $width) {
$bin = 0;
$limit = $width - $i < 8 / $bit ? (8 / $bit - $width + $i) * $bit : 0;
for ($k = 8 - $bit; $k >= $limit; $k -= $bit) {
$index = imagecolorat($im, $i, $j);
$bin |= $index << $k;
$i++;
}
$bmp_data .= chr($bin);
}
$bmp_data .= $extra;
}
}
else if ($compression == 1 && $bit == 8) {
for ($j = $height - 1; $j >= 0; $j--) {
$last_index = "\0";
$same_num = 0;
for ($i = 0; $i <= $width; $i++) {
$index = imagecolorat($im, $i, $j);
if ($index !== $last_index || $same_num > 255) {
if ($same_num != 0) {
$bmp_data .= chr($same_num) . chr($last_index);
}
$last_index = $index;
$same_num = 1;
} else {
$same_num++;
}
}
$bmp_data .= "\0\0";
}
$bmp_data .= "\0\1";
}
$size_quad = strlen($rgb_quad);
$size_data = strlen($bmp_data);
} else {
$extra = '';
$padding = 4 - ($width * ($bit / 8)) % 4;
if ($padding % 4 != 0) {
$extra = str_repeat("\0", $padding);
}
$bmp_data = '';
for ($j = $height - 1; $j >= 0; $j--) {
for ($i = 0; $i < $width; $i++) {
$index = imagecolorat($im, $i, $j);
$colors = imagecolorsforindex($im, $index);
if ($bit == 16) {
$bin = 0 << $bit;
$bin |= ($colors['red'] >> 3) << 10;
$bin |= ($colors['green'] >> 3) << 5;
$bin |= $colors['blue'] >> 3;
$bmp_data .= pack("v", $bin);
} else {
$bmp_data .= pack("c*", $colors['blue'], $colors['green'], $colors['red']);
}
}
$bmp_data .= $extra;
}
$size_quad = 0;
$size_data = strlen($bmp_data);
$colors_num = 0;
}
$file_header = "BM" . pack("V3", 54 + $size_quad + $size_data, 0, 54 + $size_quad);
$info_header = pack("V3v2V*", 0x28, $width, $height, 1, $bit, $compression, $size_data, 0, 0, $colors_num, 0);
if ($filename != '') {
$fp = fopen($filename, "wb");
fwrite($fp, $file_header);
fwrite($fp, $info_header);
if ($rgb_quad !== '')
fwrite($fp, $rgb_quad);
fwrite($fp, $bmp_data);
fclose($fp);
return true;
}
}
createBmpImage("张三疯");
- 取模方法
<?php
namespace app\common\service;
class BmpDelivery
{
public function getData($targetFile,$horizontal=false,$leftToRight=true,$topToBottom=true,$highBitFirst=false,$pixelReverse=true){
$modOptions = [
'horizontal' =>$horizontal,
'leftToRight' =>$leftToRight,
'topToBottom' => $topToBottom,
'highBitFirst' => $highBitFirst,
'invertPixel' => $pixelReverse,
];
return $this->imageToBitmap($targetFile, $modOptions);
}
protected function imageToBitmap($imagePath, $options = []) {
$defaults = [
'horizontal' => true,
'leftToRight' => true,
'topToBottom' => true,
'highBitFirst' => true,
'invertPixel' => false,
];
$settings = array_merge($defaults, $options);
$pixels = $this->readMonochromeBmpPixels($imagePath);
$bitmapData = [];
if ($settings['horizontal']) {
foreach ($pixels as $row) {
for ($x = 0; $x < sizeof($row); $x += 8) {
$byte = 0;
for ($bit = 0; $bit < 8; $bit++) {
if ($x + $bit < $row) {
$bitValue = $row[$x + $bit];
if ($settings['invertPixel']) {
$bitValue = $bitValue ? 0 : 1;
}
if ($settings['highBitFirst']) {
$byte |= ($bitValue << (7 - $bit));
} else {
$byte |= ($bitValue << $bit);
}
}
}
$bitmapData[] = $byte;
}
}
}
else
{
for ($y = 0; $y < count($pixels); $y += 8) {
for($x=0;$x<count($pixels[0]);$x++) {
$byte = 0;
for ($bit = 0; $bit < 8; $bit++) {
if ($y + $bit < count($pixels)) {
$bitValue = $pixels[ $y + $bit][$x];
if ($settings['invertPixel']) {
$bitValue = $bitValue ? 0 : 1;
}
if ($settings['highBitFirst']) {
$byte |= ($bitValue << (7 - $bit));
} else {
$byte |= ($bitValue << $bit);
}
}
}
$bitmapData[] = $byte;
}
}
}
$hexString = '';
$string='';
$strArr = [];
foreach ($bitmapData as $index => $byte) {
$string .= sprintf("%02X", $byte);
$hexString .= sprintf("0x%02X", $byte);
if ($index < count($bitmapData) - 1) {
$string .= "";
$hexString .= ",";
}
}
$strArr['string']= $string;
$strArr['hexString']= $hexString;
return $strArr;
}
protected function readMonochromeBmpPixels($filePath) {
$file = fopen($filePath, 'rb');
if (!$file) {
die("Cannot open file.");
}
fseek($file, 10);
$dataPosition = unpack('V', fread($file, 4))[1];
fseek($file, 18);
$width = unpack('V', fread($file, 4))[1];
$height = unpack('V', fread($file, 4))[1];
$rowSize = ceil($width / 8);
$paddedRowSize = (ceil($rowSize / 4) * 4);
$pixelArray = [];
for ($y = 0; $y < $height; $y++) {
fseek($file, $dataPosition + ($height-1-$y)*$paddedRowSize);
$row = fread($file, $paddedRowSize);
for ($x = 0; $x < $width; $x++) {
$byteIndex = (int)($x / 8);
$bitIndex = 7 - ($x % 8);
$byteValue = ord($row[$byteIndex]);
$bitValue = ($byteValue >> $bitIndex) & 1;
$pixelArray[$y][$x] = ($bitValue?0:1);
}
}
fclose($file);
return $pixelArray;
}
}
- 把存在服务器的图片地址拿到以后进行取模
$imagesData= new BmpDelivery();
$data=$imagesData->getData($imageArr['root_url']);
array(2) {
["string"] => string(1024) "000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000105090121C10F0101000F00000FE00000000F8888888F88A8C0888F88888880000040404040404C44424140C040000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000804020130C33C00000001F8000FF000080601F201806FF0428100CFF0418200001010101018101FF0101010101010100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001000000000000000000010000000001000000000000000000000100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"
["hexString"] => string(2559) "0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x10,0x50,0x90,0x12,0x1C,0x10,0xF0,0x10,0x10,0x00,0xF0,0x00,0x00,0xFE,0x00,0x00,0x00,0x00,0xF8,0x88,0x88,0x88,0xF8,0x8A,0x8C,0x08,0x88,0xF8,0x88,0x88,0x88,0x00,0x00,0x04,0x04,0x04,0x04,0x04,0x04,0xC4,0x44,0x24,0x14,0x0C,0x04,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0x40,0x20,0x13,0x0C,0x33,0xC0,0x00,0x00,0x00,0x1F,0x80,0x00,0xFF,0x00,0x00,0x80,0x60,0x1F,0x20,0x18,0x06,0xFF,0x04,0x28,0x10,0x0C,0xFF,0x04,0x18,0x20,0x00,0x01,0x01,0x01,0x01,0x01,0x81,0x01,0xFF,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x00,0x00,0x00,0x00,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00"
}