php 由ttf字体文件生成png预览图
<?php
/**
* Created by graph-design-api.
* AUTHOR: daiguojin
* Date: 2020/9/16 9:40
*/
//header("Content-type: image/png");
// The text to draw
$text = 'ABeeZee';
// Replace path by your own font path
$fontFile = 'ABeeZee.ttf';
$fontSize = 24;
$pos = imagettfbbox($fontSize, 0, $fontFile, $text);
$width = $pos[2] - $pos[0];
$height = $pos[1] - $pos[7];
// Create the image
$im = imagecreatetruecolor($width, $height);
//imagealphablending($im, false);//这里很重要,意思是不合并颜色,直接用$img图像颜色替换,包括透明色;
//imagesavealpha($im, true); //这里很重要,意思是不要丢了$thumb图像的透明色;
// Create some colors
$white = imagecolorallocate($im, 255, 255, 255);
$black = imagecolorallocate($im, 0, 0, 0);
imagecolortransparent($img, $white);
imagefill($im, 0, 0, $white);
// Add some shadow to the text
//imagettftext($im, 60, 0, 11, 21, $grey, $font, $text);
// Add the text
imagettftext($im, $fontSize, 0, 0, $height, $black, $fontFile, $text);
// Using imagepng() results in clearer text compared with imagejpeg()
imagepng($im, 'ABeeZee.png');
imagedestroy($im);
echo "Done";
PHP获取ttf格式文件字体名的方法示例
<?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], //平台(操作系统)
'language' = $table[3], //字体名称的语言
'encoding' = $table[2], //字体名称的编码
'name' = $fontname //字体名称
);
//break;
}
fseek($fp, $npos, SEEK_SET);
}
}
}
fclose($fp);
}
return $names;
}
?>
© 版权声明
THE END
暂无评论内容