gpt4 book ai didi

PHP:如何从 .t​​tf 文件中读取 "Title"字体?

转载 作者:IT王子 更新时间:2023-10-29 00:02:57 27 4
gpt4 key购买 nike

我真的需要能够从 .ttf 真字体文件中提取元数据。

我正在为我们所有设计师使用的所有字体构建一个中央数据库(他们总是通过电子邮件交换字体以接管设计元素等)。我想得到所有的字体,有些名字很傻,比如 00001.ttf,所以文件名没有帮助,但我知道字体有元数据,我需要一些方法在 PHP 中提取它。

然后我可以创建一个循环来查看我指定的目录,获取这些数据(以及我可以同时获取的任何其他数据,并将其添加到数据库中。

我真的需要帮助阅读这个元数据部分。

最佳答案

我遇到了this link .它会做你想做的(我已经测试过它并发布了结果)。只需将要从中解析数据的 TTF 文件的路径传递给类。然后使用 $fontinfo[1]。' '.$fontinfo[2] 为名称。

如果你不想注册,这里是类(class)

结果数据

Array
(
[1] => Almonte Snow
[2] => Regular
[3] => RayLarabie: Almonte Snow: 2000
[4] => Almonte Snow
[5] => Version 2.000 2004
[6] => AlmonteSnow
[8] => Ray Larabie
[9] => Ray Larabie
[10] => Larabie Fonts is able to offer unique free fonts through the generous support of visitors to the site. Making fonts is my full-time job and every donation, in any amount, enables me to continue running the site and creating new fonts. If you would like to support Larabie Fonts visit www.larabiefonts.com for details.
[11] => http://www.larabiefonts.com
[12] => http://www.typodermic.com
)

用法

<?php 
include 'ttfInfo.class.php';
$fontinfo = getFontInfo('c:\windows\fonts\_LDS_almosnow.ttf');
echo '<pre>';
print_r($fontinfo);
echo '</pre>';
?>

ttfInfo.class.php

<?php 
/**
* ttfInfo class
* Retrieve data stored in a TTF files 'name' table
*
* @original author Unknown
* found at http://www.phpclasses.org/browse/package/2144.html
*
* @ported for used on http://www.nufont.com
* @author Jason Arencibia
* @version 0.2
* @copyright (c) 2006 GrayTap Media
* @website http://www.graytap.com
* @license GPL 2.0
* @access public
*
* @todo: Make it Retrieve additional information from other tables
*
*/
class ttfInfo {
/**
* variable $_dirRestriction
* Restrict the resource pointer to this directory and above.
* Change to 1 for to allow the class to look outside of it current directory
* @protected
* @var int
*/
protected $_dirRestriction = 1;
/**
* variable $_dirRestriction
* Restrict the resource pointer to this directory and above.
* Change to 1 for nested directories
* @protected
* @var int
*/
protected $_recursive = 0;

/**
* variable $fontsdir
* This is to declare this variable as protected
* don't edit this!!!
* @protected
*/
protected $fontsdir;
/**
* variable $filename
* This is to declare this varable as protected
* don't edit this!!!
* @protected
*/
protected $filename;

/**
* function setFontFile()
* set the filename
* @public
* @param string $data the new value
* @return object reference to this
*/
public function setFontFile($data)
{
if ($this->_dirRestriction && preg_match('[\.\/|\.\.\/]', $data))
{
$this->exitClass('Error: Directory restriction is enforced!');
}

$this->filename = $data;
return $this;
} // public function setFontFile

/**
* function setFontsDir()
* set the Font Directory
* @public
* @param string $data the new value
* @return object referrence to this
*/
public function setFontsDir($data)
{
if ($this->_dirRestriction && preg_match('[\.\/|\.\.\/]', $data))
{
$this->exitClass('Error: Directory restriction is enforced!');
}

$this->fontsdir = $data;
return $this;
} // public function setFontsDir

/**
* function readFontsDir()
* @public
* @return information contained in the TTF 'name' table of all fonts in a directory.
*/
public function readFontsDir()
{
if (empty($this->fontsdir)) { $this->exitClass('Error: Fonts Directory has not been set with setFontsDir().'); }
if (empty($this->backupDir)){ $this->backupDir = $this->fontsdir; }

$this->array = array();
$d = dir($this->fontsdir);

while (false !== ($e = $d->read()))
{
if($e != '.' && $e != '..')
{
$e = $this->fontsdir . $e;
if($this->_recursive && is_dir($e))
{
$this->setFontsDir($e);
$this->array = array_merge($this->array, readFontsDir());
}
else if ($this->is_ttf($e) === true)
{
$this->setFontFile($e);
$this->array[$e] = $this->getFontInfo();
}
}
}

if (!empty($this->backupDir)){ $this->fontsdir = $this->backupDir; }

$d->close();
return $this;
} // public function readFontsDir

/**
* function setProtectedVar()
* @public
* @param string $var the new variable
* @param string $data the new value
* @return object reference to this

* DISABLED, NO REAL USE YET

public function setProtectedVar($var, $data)
{
if ($var == 'filename')
{
$this->setFontFile($data);
} else {
//if (isset($var) && !empty($data))
$this->$var = $data;
}
return $this;
}
*/
/**
* function getFontInfo()
* @public
* @return information contained in the TTF 'name' table.
*/
public function getFontInfo()
{
$fd = fopen ($this->filename, "r");
$this->text = fread ($fd, filesize($this->filename));
fclose ($fd);

$number_of_tables = hexdec($this->dec2ord($this->text[4]).$this->dec2ord($this->text[5]));

for ($i=0;$i<$number_of_tables;$i++)
{
$tag = $this->text[12+$i*16].$this->text[12+$i*16+1].$this->text[12+$i*16+2].$this->text[12+$i*16+3];

if ($tag == 'name')
{
$this->ntOffset = hexdec(
$this->dec2ord($this->text[12+$i*16+8]).$this->dec2ord($this->text[12+$i*16+8+1]).
$this->dec2ord($this->text[12+$i*16+8+2]).$this->dec2ord($this->text[12+$i*16+8+3]));

$offset_storage_dec = hexdec($this->dec2ord($this->text[$this->ntOffset+4]).$this->dec2ord($this->text[$this->ntOffset+5]));
$number_name_records_dec = hexdec($this->dec2ord($this->text[$this->ntOffset+2]).$this->dec2ord($this->text[$this->ntOffset+3]));
}
}

$storage_dec = $offset_storage_dec + $this->ntOffset;
$storage_hex = strtoupper(dechex($storage_dec));

for ($j=0;$j<$number_name_records_dec;$j++)
{
$platform_id_dec = hexdec($this->dec2ord($this->text[$this->ntOffset+6+$j*12+0]).$this->dec2ord($this->text[$this->ntOffset+6+$j*12+1]));
$name_id_dec = hexdec($this->dec2ord($this->text[$this->ntOffset+6+$j*12+6]).$this->dec2ord($this->text[$this->ntOffset+6+$j*12+7]));
$string_length_dec = hexdec($this->dec2ord($this->text[$this->ntOffset+6+$j*12+8]).$this->dec2ord($this->text[$this->ntOffset+6+$j*12+9]));
$string_offset_dec = hexdec($this->dec2ord($this->text[$this->ntOffset+6+$j*12+10]).$this->dec2ord($this->text[$this->ntOffset+6+$j*12+11]));

if (!empty($name_id_dec) and empty($font_tags[$name_id_dec]))
{
for($l=0;$l<$string_length_dec;$l++)
{
if (ord($this->text[$storage_dec+$string_offset_dec+$l]) == '0') { continue; }
else { $font_tags[$name_id_dec] .= ($this->text[$storage_dec+$string_offset_dec+$l]); }
}
}
}
return $font_tags;
} // public function getFontInfo

/**
* function getCopyright()
* @public
* @return 'Copyright notice' contained in the TTF 'name' table at index 0
*/
public function getCopyright()
{
$this->info = $this->getFontInfo();
return $this->info[0];
} // public function getCopyright

/**
* function getFontFamily()
* @public
* @return 'Font Family name' contained in the TTF 'name' table at index 1
*/
public function getFontFamily()
{
$this->info = $this->getFontInfo();
return $this->info[1];
} // public function getFontFamily

/**
* function getFontSubFamily()
* @public
* @return 'Font Subfamily name' contained in the TTF 'name' table at index 2
*/
public function getFontSubFamily()
{
$this->info = $this->getFontInfo();
return $this->info[2];
} // public function getFontSubFamily

/**
* function getFontId()
* @public
* @return 'Unique font identifier' contained in the TTF 'name' table at index 3
*/
public function getFontId()
{
$this->info = $this->getFontInfo();
return $this->info[3];
} // public function getFontId

/**
* function getFullFontName()
* @public
* @return 'Full font name' contained in the TTF 'name' table at index 4
*/
public function getFullFontName()
{
$this->info = $this->getFontInfo();
return $this->info[4];
} // public function getFullFontName

/**
* function dec2ord()
* Used to lessen redundant calls to multiple functions.
* @protected
* @return object
*/
protected function dec2ord($dec)
{
return $this->dec2hex(ord($dec));
} // protected function dec2ord

/**
* function dec2hex()
* private function to perform Hexadecimal to decimal with proper padding.
* @protected
* @return object
*/
protected function dec2hex($dec)
{
return str_repeat('0', 2-strlen(($hex=strtoupper(dechex($dec))))) . $hex;
} // protected function dec2hex

/**
* function dec2hex()
* private function to perform Hexadecimal to decimal with proper padding.
* @protected
* @return object
*/
protected function exitClass($message)
{
echo $message;
exit;
} // protected function dec2hex

/**
* function dec2hex()
* private helper function to test in the file in question is a ttf.
* @protected
* @return object
*/
protected function is_ttf($file)
{
$ext = explode('.', $file);
$ext = $ext[count($ext)-1];
return preg_match("/ttf$/i",$ext) ? true : false;
} // protected function is_ttf
} // class ttfInfo

function getFontInfo($resource)
{
$ttfInfo = new ttfInfo;
$ttfInfo->setFontFile($resource);
return $ttfInfo->getFontInfo();
}
?>

2021 年更新

这是该类(class)的更新版本,其中包含一些修复 https://github.com/HusamAamer/TTFInfo.git

关于PHP:如何从 .t​​tf 文件中读取 "Title"字体?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5668901/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com