PHP获取ttf格式文件字体名的方法示例

这篇文章主要介绍了PHP获取ttf格式文件字体名的方法,结合实例形式分析了php读取ttf字体文件属性的相关操作技巧,需要的朋友可以参考下。

本文实例讲述了PHP获取ttf格式文件字体名的方法,分享给大家供大家参考,具体如下:

  1. <?php
  2. $names = GetFontName('c:/windows/fonts/FZHPJW.TTF');
  3. foreach ($names as $name) {
  4. if ($name['language'] == 1033)
  5. $code = 'utf-16le';
  6. elseif ($name['language'] == 2052) $code = 'utf-16be';
  7. var_dump(mb_convert_encoding($name['name'],'utf-8',$code));
  8. }
  9. function GetFontName($FilePath) {
  10. $fp = fopen($FilePath, 'r');
  11. if ($fp) {
  12. //TT_OFFSET_TABLE
  13. $meta = unpack('n6', fread($fp, 12));
  14. //检查是否是一个true type字体文件以及版本号是否为1.0
  15. if ($meta[1] != 1 || $meta[2] != 0)
  16. return FALSE;
  17. $Found = FALSE;
  18. for ($i = 0; $i < $meta[3]; $i++) {
  19. //TT_TABLE_DIRECTORY
  20. $tablemeta = unpack('N4', $data = fread($fp, 16));
  21. if (substr($data, 0, 4) == 'name') {
  22. $Found = TRUE;
  23. break;
  24. }
  25. }
  26. if ($Found) {
  27. fseek($fp, $tablemeta[3]);
  28. //TT_NAME_TABLE_HEADER
  29. $tablecount = unpack('n3', fread($fp, 6));
  30. $Found = FALSE;
  31. for ($i = 0; $i < $tablecount[2]; $i++) {
  32. //TT_NAME_RECORD
  33. $table = unpack('n6', fread($fp, 12));
  34. if ($table[4] == 1) {
  35. $npos = ftell($fp);
  36. fseek($fp, $n = $tablemeta[3] + $tablecount[3] + $table[6], SEEK_SET);
  37. $fontname = trim($x = fread($fp, $table[5]));
  38. if (strlen($fontname) > 0) {
  39. $names[] = array (
  40. 'platform' => $table[1], //平台(操作系统)
  41. 'language' => $table[3], //字体名称的语言
  42. 'encoding' => $table[2], //字体名称的编码
  43. 'name' => $fontname //字体名称
  44. );
  45. //break;
  46. }
  47. fseek($fp, $npos, SEEK_SET);
  48. }
  49. }
  50. }
  51. fclose($fp);
  52. }
  53. return $names;
  54. }
  55. ?>

运行结果:

string(6) "SimHei"

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

string(6) "黑体"

注:如果这里仅需要获取字体名称,可将上述代码进行改进如下:

  1. <?php
  2. $names = GetFontName('c:/windows/fonts/FZHPJW.TTF');
  3. $newnames = array();
  4. foreach ($names as $name) {
  5. if ($name['language'] == 1033)
  6. $code = 'utf-16le';
  7. elseif ($name['language'] == 2052) $code = 'utf-16be';
  8. array_push($newnames,@mb_convert_encoding($name['name'], 'utf-8', $code));
  9. }
  10. $font_name=array_pop($newnames);
  11. echo $font_name;
  12. function GetFontName($FilePath) {
  13. $fp = fopen($FilePath, 'r');
  14. if ($fp) {
  15. //TT_OFFSET_TABLE
  16. $meta = unpack('n6', fread($fp, 12));
  17. //检查是否是一个true type字体文件以及版本号是否为1.0
  18. if ($meta[1] != 1 || $meta[2] != 0)
  19. return FALSE;
  20. $Found = FALSE;
  21. for ($i = 0; $i < $meta[3]; $i++) {
  22. //TT_TABLE_DIRECTORY
  23. $tablemeta = unpack('N4', $data = fread($fp, 16));
  24. if (substr($data, 0, 4) == 'name') {
  25. $Found = TRUE;
  26. break;
  27. }
  28. }
  29. if ($Found) {
  30. fseek($fp, $tablemeta[3]);
  31. //TT_NAME_TABLE_HEADER
  32. $tablecount = unpack('n3', fread($fp, 6));
  33. $Found = FALSE;
  34. for ($i = 0; $i < $tablecount[2]; $i++) {
  35. //TT_NAME_RECORD
  36. $table = unpack('n6', fread($fp, 12));
  37. if ($table[4] == 1) {
  38. $npos = ftell($fp);
  39. fseek($fp, $n = $tablemeta[3] + $tablecount[3] + $table[6], SEEK_SET);
  40. $fontname = trim($x = fread($fp, $table[5]));
  41. if (strlen($fontname) > 0) {
  42. $names[] = array (
  43. 'platform' => $table[1], //平台(操作系统)
  44. 'language' => $table[3], //字体名称的语言
  45. 'encoding' => $table[2], //字体名称的编码
  46. 'name' => $fontname //字体名称
  47. );
  48. //break;
  49. }
  50. fseek($fp, $npos, SEEK_SET);
  51. }
  52. }
  53. }
  54. fclose($fp);
  55. }
  56. return $names;
  57. }
  58. ?>

则此时可直接输出:

黑体