您好,登錄后才能下訂單哦!
這期內(nèi)容當中小編將會給大家?guī)碛嘘P(guān)使用PHP怎么獲取ttf格式文件的字體名,文章內(nèi)容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。
具體如下:
<?php $names = GetFontName('c:/windows/fonts/FZHPJW.TTF'); foreach ($names as $name) { if ($name['language'] == 1033) $code = 'utf-16le'; elseif ($name['language'] == 2052) $code = 'utf-16be'; var_dump(mb_convert_encoding($name['name'],'utf-8',$code)); } function GetFontName($FilePath) { $fp = fopen($FilePath, 'r'); if ($fp) { //TT_OFFSET_TABLE $meta = unpack('n6', 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('N4', $data = fread($fp, 16)); if (substr($data, 0, 4) == 'name') { $Found = TRUE; break; } } if ($Found) { fseek($fp, $tablemeta[3]); //TT_NAME_TABLE_HEADER $tablecount = unpack('n3', fread($fp, 6)); $Found = FALSE; for ($i = 0; $i < $tablecount[2]; $i++) { //TT_NAME_RECORD $table = unpack('n6', 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 ( 'platform' => $table[1], //平臺(操作系統(tǒng)) 'language' => $table[3], //字體名稱的語言 'encoding' => $table[2], //字體名稱的編碼 'name' => $fontname //字體名稱 ); //break; } fseek($fp, $npos, SEEK_SET); } } } fclose($fp); } return $names; } ?>
運行結(jié)果:
string(6) "SimHei"
string(5) "SimHe" //貌似有UTF-16LE編碼漏字的BUG
string(6) "黑體"
注:如果這里僅需要獲取字體名稱,可將上述代碼進行改進如下:
<?php $names = GetFontName('c:/windows/fonts/FZHPJW.TTF'); $newnames = array(); foreach ($names as $name) { if ($name['language'] == 1033) $code = 'utf-16le'; elseif ($name['language'] == 2052) $code = 'utf-16be'; array_push($newnames,@mb_convert_encoding($name['name'], 'utf-8', $code)); } $font_name=array_pop($newnames); echo $font_name; function GetFontName($FilePath) { $fp = fopen($FilePath, 'r'); if ($fp) { //TT_OFFSET_TABLE $meta = unpack('n6', 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('N4', $data = fread($fp, 16)); if (substr($data, 0, 4) == 'name') { $Found = TRUE; break; } } if ($Found) { fseek($fp, $tablemeta[3]); //TT_NAME_TABLE_HEADER $tablecount = unpack('n3', fread($fp, 6)); $Found = FALSE; for ($i = 0; $i < $tablecount[2]; $i++) { //TT_NAME_RECORD $table = unpack('n6', 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 ( 'platform' => $table[1], //平臺(操作系統(tǒng)) 'language' => $table[3], //字體名稱的語言 'encoding' => $table[2], //字體名稱的編碼 'name' => $fontname //字體名稱 ); //break; } fseek($fp, $npos, SEEK_SET); } } } fclose($fp); } return $names; } ?>
則此時可直接輸出:
黑體
上述就是小編為大家分享的使用PHP怎么獲取ttf格式文件的字體名了,如果剛好有類似的疑惑,不妨參照上述分析進行理解。如果想知道更多相關(guān)知識,歡迎關(guān)注億速云行業(yè)資訊頻道。
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。