PHP获取ttf格式文件字体名的方法示例
发布:smiling 来源: PHP粉丝网 添加日期:2021-11-11 16:57:11 浏览: 评论:0
这篇文章主要介绍了PHP获取ttf格式文件字体名的方法,结合实例形式分析了php读取ttf字体文件属性的相关操作技巧,需要的朋友可以参考下。
本文实例讲述了PHP获取ttf格式文件字体名的方法,分享给大家供大家参考,具体如下:
- <?php
- $names = GetFontName('c:/windows/fonts/FZHPJW.TTF');
- foreach ($names as $name) {
- if ($name['language'] == 1033)
- $code = 'utf-16le';
- elseif ($name['language'] == 2052) $code = 'utf-16be';
- var_dump(mb_convert_encoding($name['name'],'utf-8',$code));
- }
- function GetFontName($FilePath) {
- $fp = fopen($FilePath, 'r');
- if ($fp) {
- //TT_OFFSET_TABLE
- $meta = unpack('n6', fread($fp, 12));
- //检查是否是一个true type字体文件以及版本号是否为1.0
- if ($meta[1] != 1 || $meta[2] != 0)
- return FALSE;
- $Found = FALSE;
- for ($i = 0; $i < $meta[3]; $i++) {
- //TT_TABLE_DIRECTORY
- $tablemeta = unpack('N4', $data = fread($fp, 16));
- if (substr($data, 0, 4) == 'name') {
- $Found = TRUE;
- break;
- }
- }
- if ($Found) {
- fseek($fp, $tablemeta[3]);
- //TT_NAME_TABLE_HEADER
- $tablecount = unpack('n3', fread($fp, 6));
- $Found = FALSE;
- for ($i = 0; $i < $tablecount[2]; $i++) {
- //TT_NAME_RECORD
- $table = unpack('n6', fread($fp, 12));
- if ($table[4] == 1) {
- $npos = ftell($fp);
- fseek($fp, $n = $tablemeta[3] + $tablecount[3] + $table[6], SEEK_SET);
- $fontname = trim($x = fread($fp, $table[5]));
- if (strlen($fontname) > 0) {
- $names[] = array (
- 'platform' => $table[1], //平台(操作系统)
- 'language' => $table[3], //字体名称的语言
- 'encoding' => $table[2], //字体名称的编码
- 'name' => $fontname //字体名称
- );
- //break;
- }
- fseek($fp, $npos, SEEK_SET);
- }
- }
- }
- fclose($fp);
- }
- return $names;
- }
- ?>
运行结果:
string(6) "SimHei"
string(5) "SimHe" //貌似有UTF-16LE编码漏字的BUG
string(6) "黑体"
注:如果这里仅需要获取字体名称,可将上述代码进行改进如下:
- <?php
- $names = GetFontName('c:/windows/fonts/FZHPJW.TTF');
- $newnames = array();
- foreach ($names as $name) {
- if ($name['language'] == 1033)
- $code = 'utf-16le';
- elseif ($name['language'] == 2052) $code = 'utf-16be';
- array_push($newnames,@mb_convert_encoding($name['name'], 'utf-8', $code));
- }
- $font_name=array_pop($newnames);
- echo $font_name;
- function GetFontName($FilePath) {
- $fp = fopen($FilePath, 'r');
- if ($fp) {
- //TT_OFFSET_TABLE
- $meta = unpack('n6', fread($fp, 12));
- //检查是否是一个true type字体文件以及版本号是否为1.0
- if ($meta[1] != 1 || $meta[2] != 0)
- return FALSE;
- $Found = FALSE;
- for ($i = 0; $i < $meta[3]; $i++) {
- //TT_TABLE_DIRECTORY
- $tablemeta = unpack('N4', $data = fread($fp, 16));
- if (substr($data, 0, 4) == 'name') {
- $Found = TRUE;
- break;
- }
- }
- if ($Found) {
- fseek($fp, $tablemeta[3]);
- //TT_NAME_TABLE_HEADER
- $tablecount = unpack('n3', fread($fp, 6));
- $Found = FALSE;
- for ($i = 0; $i < $tablecount[2]; $i++) {
- //TT_NAME_RECORD
- $table = unpack('n6', fread($fp, 12));
- if ($table[4] == 1) {
- $npos = ftell($fp);
- fseek($fp, $n = $tablemeta[3] + $tablecount[3] + $table[6], SEEK_SET);
- $fontname = trim($x = fread($fp, $table[5]));
- if (strlen($fontname) > 0) {
- $names[] = array (
- 'platform' => $table[1], //平台(操作系统)
- 'language' => $table[3], //字体名称的语言
- 'encoding' => $table[2], //字体名称的编码
- 'name' => $fontname //字体名称
- );
- //break;
- }
- fseek($fp, $npos, SEEK_SET);
- }
- }
- }
- fclose($fp);
- }
- return $names;
- }
- ?>
则此时可直接输出:
黑体
Tags: PHP获取ttf
推荐文章
热门文章
最新评论文章
- 写给考虑创业的年轻程序员(10)
- PHP新手上路(一)(7)
- 惹恼程序员的十件事(5)
- PHP邮件发送例子,已测试成功(5)
- 致初学者:PHP比ASP优秀的七个理由(4)
- PHP会被淘汰吗?(4)
- PHP新手上路(四)(4)
- 如何去学习PHP?(2)
- 简单入门级php分页代码(2)
- php中邮箱email 电话等格式的验证(2)