php 点阵 汉字,PHP 文字生成点阵图

七夕到了是不是该做点什么了~

体验地址

[2019-7-31 更新]

做了个包~~

composer require jc91715/lattice -vvv

use Jc91715\Lattice\Lattice;

$str="helloworld";

$lattice = new Lattice($str,5);

echo $lattice->getResult();

a828b78949512f8d0c41be936f10505c.png

00ae99d3018918e788d3e58d3f1e1764.png

$font_width = 16; // 单字宽度

$font_height = 16; // 单字高度

$byteCount = $font_width * $font_height / 8;//一个点阵占的字节数

$str=iconv("utf-8","gb2312//IGNORE", "我是大是的大萨法fasf大多数");

$n=5;//每一行四个字

//所有字的字模

$dot = getDot($str,$byteCount);

/**

* $positions 平面坐标系

* $sections 每一个字模在平面坐标系中的点。(可移动每一个字模的位置)

* $spreadSections 所有字模在平面坐标系中的点,是$sections的展开

*

*/

list($positions,$sections,$spreadSections) = getPositionsSections($dot,$byteCount,$n);

/**

* 输出点阵html字符串

*/

echo getOutHtml($positions,$sections,$spreadSections,$n);

/**

* 从字库中获得每一个字的字模

* @param $str

* @param $byteCount

* @return string

*/

function getDot($str,$byteCount){

$dot='';

$fontFileName = './HZK16';//字库名字

$fp = fopen($fontFileName, "rb");

for ($i=0;$i

if(ord($str[$i])<160){//非汉字

$location=(ord($str{$i}) + 156-1) * $byteCount;

}else {//汉字

$qh = ord($str[$i]) - 32 - 128;//区码

$wh = ord($str[++$i]) - 32 - 128;//位码

$location = (94 * ($qh - 1) + ($wh - 1)) * $byteCount; /* 计算汉字字模在文件中的位置 */

}

fseek($fp, $location, SEEK_SET);//定位到汉字或字母指针开始的地方

$dot.= fread($fp, $byteCount);//读取32字节的长度,一个字节8位,一行依次放两个字节组成16*16的点阵

}

fclose($fp);

return $dot;

}

/**

* 建平面按坐标系。并把每一区块用平面坐标系表示

* @param $dot

* @param $byteCount

* @param $n

* @return array

*/

function getPositionsSections($dot,$byteCount,$n){

$count= strlen($dot)/$byteCount;//多少个字

$positions=[];

$sections =[];

$sectionCount=$count;

for ($i=0;$i

$sections[]=[];

}

$yHeight=(intval($count/$n)*16+16);

$xWeight=16*$n;

for ($i=0;$i

for ($j=0;$j

$positions []=[$j,$i];

$x=ceil(($j+1)/16);

$y=ceil(($i+1)/16);

$y--;

$x--;

$sections[(($y)*$n+$x)][] = [$j,$i];

}

}

for ($b=0;$b

$str = substr($dot,($b)*32,$byteCount);//第几个字

$dot_string='';

for ($c = 0; $c < $byteCount; $c++){

$dot_string .= sprintf("%08b", ord($str[$c]));

if ($c % 2 == 1) {

for($a=0;$a

if($dot_string[$a]){//和平面坐标系关联起来

$sections[$b][intval(16*floor($c/2)+$a)][]=1;

}

}

$dot_string = '';

}

}

}

$spreadSections=[];//每一个字块的的点展开到数组中

foreach ($sections as $section){

$spreadSections = array_merge($spreadSections,$section);

}

return [$positions,$sections,$spreadSections,$count,$sectionCount];

}

function getOutHtml($positions,$sections,$spreadSections,$n){

$str="

foreach (array_chunk($positions,16*$n) as $row){

$str.=getOutRow($row,$sections,$spreadSections);

}

$str .= "

";

return $str;

}

function getOutRow($row,$sections,$spreadSections){

$str="

";

foreach ($row as $td) {

if (!in_array($td,$spreadSections)){//不在平面坐标系中说明这个位置是一个点

$str .= "

O";

}else {

$str .= "

O";

}

}

$str.="

";

return $str;

}

本作品采用《CC 协议》,转载必须注明作者和本文链接

NOT IS BECAUSE I WANT TO WRITE,

BUT I WANT TO INCREASE,

SO I GO TO WRITE~~

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值