Rumah >pembangunan bahagian belakang >masalah PHP >PHP如何获取ttf格式文件字体名

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

autoload
autoloadke hadapan
2021-03-26 14:31:102712semak imbas

    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教程

Atas ialah kandungan terperinci PHP如何获取ttf格式文件字体名. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!

Kenyataan:
Artikel ini dikembalikan pada:西部数码. Jika ada pelanggaran, sila hubungi admin@php.cn Padam