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
Release: 2023-03-09 07:04:02
forward
2743 people have browsed it

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;
}
?>
Copy after login

Run result:

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

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;
}
?>
Copy after login

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!

Related labels:
source:西部数码
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Issues
php data acquisition?
From 1970-01-01 08:00:00
0
0
0
PHP extension intl
From 1970-01-01 08:00:00
0
0
0
How to learn php well
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template