douyou2732 2016-05-30 10:42
浏览 40

如何在Linux Mint 17.2下在PHP中使用字体?

I am trying out some sample code in order to experiment with the PHP5-GD libaray. I have made several efforts to Google for an answer, but no success. I have a font under document root: /home//www/tiffanytwolight-regular.ttf (I have changed document root from /var/www/html/ which is the default root in my installation). I want to draw a bar histogram. Important code-snippets are:

class SimpleBar {
    private $xgutter = 20; // left/right margin
    private $ygutter = 20; // top/bottom margin
    private $bottomspace = 30; // gap at the bottom
    private $internalgap = 20; // space between bars
    private $cells = array(); // labels/amounts for bar chart
    private $totalwidth; // width of the image
    private $totalheight; // height of the image
    private $font; // the font to use

    function __construct( $width, $height, $font ) {
        $this->totalwidth = $width;
        $this->totalheight = $height;
        $this->font = $font;
    }
}

I have not succeeded in giving $font a meaningful content. $this->font occurs several times but is not set. Examples:

$box = ImageTTFbBox( $textsize, 0, $this->font, $key );
ImageTTFText( $image, $textsize, 0, ($center-($tw/2)),
    ($this->totalheight-$this->ygutter), $black,
    $this->font, $key );

The code that calls the class functions is: include("SimpleBar.class.php");

$graph = new SimpleBar( 500, 300, "tiffanytwolight-regular.ttf" );
$graph->addBar( "USA", 200 );
$graph->addBar( "India", 400 );
$graph->addBar( "UK", 240 );
$graph->addBar( "Australia", 170 );
$graph->addBar( "UAE", 270 );
$graph->draw();

This results in an error message (via ini_set('display_errors', 'on'):

(!) Warning: imagettfbbox(): Could not find/open font in /home/par/www/SimpleBar.class.php on line 30

I would appreciate some good hints.

  • 写回答

1条回答 默认 最新

  • duanhun3273 2016-05-30 10:58
    关注

    You don't need system-wide fonts for GD. Just store them within your project resources and then provide a full path rather than a file name. From the ImageTTFText() manual page:

    fontfile

    The path to the TrueType font you wish to use.

    Depending on which version of the GD library PHP is using, when fontfile does not begin with a leading / then .ttf will be appended to the filename and the library will attempt to search for that filename along a library-defined font path.

    When using versions of the GD library lower than 2.0.18, a space character, rather than a semicolon, was used as the 'path separator' for different font files. Unintentional use of this feature will result in the warning message: Warning: Could not find/open font. For these affected versions, the only solution is moving the font to a path which does not contain spaces.

    In many cases where a font resides in the same directory as the script using it the following trick will alleviate any include problems.

    <?php
    // Set the enviroment variable for GD
    putenv('GDFONTPATH=' . realpath('.'));
    
    // Name the font to be used (note the lack of the .ttf extension)
    $font = 'SomeFont';
    ?>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 如何用stata画出文献中常见的安慰剂检验图
  • ¥15 c语言链表结构体数据插入
  • ¥40 使用MATLAB解答线性代数问题
  • ¥15 COCOS的问题COCOS的问题
  • ¥15 FPGA-SRIO初始化失败
  • ¥15 MapReduce实现倒排索引失败
  • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
  • ¥15 找一位技术过硬的游戏pj程序员
  • ¥15 matlab生成电测深三层曲线模型代码
  • ¥50 随机森林与房贷信用风险模型