Home >Backend Development >PHP Problem >How to get the font name of ttf format file in PHP

How to get the font name of ttf format file in PHP

autoload
autoloadforward
2021-03-26 14:31:102717browse

TTF (TrueTypeFont) is a font file format jointly launched by Apple and Microsoft. With the popularity of windows, it has become the most commonly used A font file format that is inevitably used in daily use.

<?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;
}
?>

Run result:

string(6) "SimHei"
string(5) "SimHe" //貌似有UTF-16LE编码漏字的BUG
string(6) "黑体"

Note: If you only need to get the font name, the above code can be improved as follows:

<?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;
}
?>

Recommended: "php video tutorial" "php tutorial"

The above is the detailed content of How to get the font name of ttf format file in PHP. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:西部数码. If there is any infringement, please contact admin@php.cn delete