2016-05-30 104 views
0

我正在尝试一些示例代码,以便用PHP5-GD libaray进行实验。我已经为Google做出了一些努力,但没有成功。 我在文档根目录下有一个字体:/home//www/tiffanytwolight-regular.ttf(我已经将/ var/www/html /这是我安装时的默认根目录下的文档根目录)。我想绘制一个直方图。 重要的代码片段是:如何在Linux Mint 17.2下使用PHP提供字体?

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; 
    } 
} 

我还没有成功地给$字体的有意义的内容。 $ this->字体发生多次但未设置。 实例:

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

调用类函数是代码: 包括( “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(); 

这将导致一个错误的信息(通过函数ini_set '上'( '的display_errors',):

()警告:imagettfbbox():找不到/打开字体在/ home /par/www/SimpleBar.class.php线30

我希望得到一些很好的提示。

回答

0

你并不需要GD全系统的字体,只要它们存储在您的项目资源范围内那么p浏览完整路径而不是文件名。从ImageTTFText() manual page

fontfile

您要使用的TrueType字体的路径。

根据其GD库的PHP版本使用,当fontfile没有领先/时则.ttf将被追加到 文件名开始,图书馆将尝试沿着库 - 搜索该文件名 定义的字体路径。

使用GD库的版本低于2.0.18时,空格字符而不是分号用作不同字体文件的'路径分隔符' 。无意中使用此功能将导致 警告消息:警告:找不到/打开字体。对于 这些受影响的版本,唯一的解决方案是将字体移动到不包含空格的路径 。

在很多情况下,字体与使用它的脚本位于相同的目录下,以下技巧将缓解任何包含 问题。

<?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'; 
?> 
+1

是的,这工作,非常感谢。我之前没有使用过这个库,所以我从网上找到的一些代码开始,对此进行测试。并没有显示任何图形,只是在屏幕左上角的一个小方块。 –