PHP如何获取ttf格式文件字体名

autoload
发布: 2023-03-09 07:04:02
转载
2677 人浏览过

    TTF(TrueTypeFont)是Apple公司和Microsoft公司共同推出的字体文件格式,随着windows的流行,已经变成最常用的一种字体文件格式,在日常使用过程中的使用也在所难免。

<?php
$names = GetFontName(&#39;c:/windows/fonts/FZHPJW.TTF&#39;);
foreach ($names as $name) {
  if ($name[&#39;language&#39;] == 1033)
    $code = &#39;utf-16le&#39;;
  elseif ($name[&#39;language&#39;] == 2052) $code = &#39;utf-16be&#39;;
  var_dump(mb_convert_encoding($name[&#39;name&#39;],&#39;utf-8&#39;,$code));
}
function GetFontName($FilePath) {
  $fp = fopen($FilePath, &#39;r&#39;);
  if ($fp) {
    //TT_OFFSET_TABLE
    $meta = unpack(&#39;n6&#39;, 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(&#39;N4&#39;, $data = fread($fp, 16));
      if (substr($data, 0, 4) == &#39;name&#39;) {
        $Found = TRUE;
        break;
      }
    }
    if ($Found) {
      fseek($fp, $tablemeta[3]);
      //TT_NAME_TABLE_HEADER
      $tablecount = unpack(&#39;n3&#39;, fread($fp, 6));
      $Found = FALSE;
      for ($i = 0; $i < $tablecount[2]; $i++) {
        //TT_NAME_RECORD
        $table = unpack(&#39;n6&#39;, 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 (
                &#39;platform&#39; => $table[1], //平台(操作系统)
    &#39;language&#39; => $table[3], //字体名称的语言
    &#39;encoding&#39; => $table[2], //字体名称的编码
    &#39;name&#39; => $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(&#39;c:/windows/fonts/FZHPJW.TTF&#39;);
$newnames = array();
foreach ($names as $name) {
  if ($name[&#39;language&#39;] == 1033)
    $code = &#39;utf-16le&#39;;
  elseif ($name[&#39;language&#39;] == 2052) $code = &#39;utf-16be&#39;;
  array_push($newnames,@mb_convert_encoding($name[&#39;name&#39;], &#39;utf-8&#39;, $code));
}
$font_name=array_pop($newnames);
echo $font_name;
function GetFontName($FilePath) {
  $fp = fopen($FilePath, &#39;r&#39;);
  if ($fp) {
    //TT_OFFSET_TABLE
    $meta = unpack(&#39;n6&#39;, 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(&#39;N4&#39;, $data = fread($fp, 16));
      if (substr($data, 0, 4) == &#39;name&#39;) {
        $Found = TRUE;
        break;
      }
    }
    if ($Found) {
      fseek($fp, $tablemeta[3]);
      //TT_NAME_TABLE_HEADER
      $tablecount = unpack(&#39;n3&#39;, fread($fp, 6));
      $Found = FALSE;
      for ($i = 0; $i < $tablecount[2]; $i++) {
        //TT_NAME_RECORD
        $table = unpack(&#39;n6&#39;, 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 (
                &#39;platform&#39; => $table[1], //平台(操作系统)
    &#39;language&#39; => $table[3], //字体名称的语言
    &#39;encoding&#39; => $table[2], //字体名称的编码
    &#39;name&#39; => $fontname //字体名称
            );
            //break;
          }
          fseek($fp, $npos, SEEK_SET);
        }
      }
    }
    fclose($fp);
  }
  return $names;
}
?>
登录后复制

推荐:《php视频教程》《php教程

以上是PHP如何获取ttf格式文件字体名的详细内容。更多信息请关注PHP中文网其他相关文章!

相关标签:
来源:西部数码
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板