【问题标题】:Warning: imagettftext() [function.imagettftext]: Could not find/open font in /home/a2424901/public_html/index.php on line 35警告:imagettftext() [function.imagettftext]:在第 35 行的 /home/a2424901/public_html/index.php 中找不到/打开字体
【发布时间】:2012-05-09 03:34:31
【问题描述】:
<?php
session_start();
require_once 'facebook.php';
$app_id = "418907881455014";
$app_secret = "36389d2c4caaf6de86982cb87686a494";
$redirect_uri = 'http://gooogle12.comuf.com';
$facebook = new Facebook(array(
        'appId' => $app_id,
        'secret' => $app_secret,
        'cookie' => true
));
$user = $facebook->getUser();
$user_profile = $facebook->api('/me');

$coded = $_REQUEST['code'];

$access_token = $facebook->getAccessToken();
$name = "".$user_profile['name']."";
$fbid = "".$user_profile['id']."";

function RandomLine($filename) {
    $lines = file($filename) ;
    return $lines[array_rand($lines)] ;
}
$reason = RandomLine("reason.txt");  

$canvas = imagecreatefromjpeg ("bg.jpg");                                   // background image file
$black = imagecolorallocate( $canvas, 0, 0, 0 );                         // The second colour - to be used for the text
$font = "Arial.ttf";                                                         // Path to the font you are going to use
$fontsize = 20;                                                             // font size

$birthday = "".$user_profile['birthday']."";
$death = "- ".date('d/m/Y', strtotime( '+'.rand(0, 10000).' days'))."";

imagettftext( $canvas, 22, -1, 110, 120, $black, $font, $name );            // name
imagettftext( $canvas, 22, -1, 110, 170, $black, $font, $birthday );        // birthday
imagettftext( $canvas, 22, -1, 255, 172, $black, $font, $death );           // death
imagettftext( $canvas, 20, -1, 110, 220, $black, $font, $reason );           // reason


$facebook->setFileUploadSupport(true);

//Create an album
$album_details = array(
        'message'=> 'How will you die?',
        'name'=> 'How will you die?'
);
$create_album = $facebook->api('/me/albums', 'post', $album_details);

//Get album ID of the album you've just created
$album_uid = $create_album['id'];

//Upload a photo to album of ID...

$file='img/'.$fbid.'.jpg'; //Example image file

$photo_details = array( 'message'=> 'Find...51', 'image' => '@'.realpath($file));
$upload_photo = $facebook->api('/'.$album_uid.'/photos', 'post', $photo_details);


    enter code here



ImageDestroy( $canvas );

header("Location: http://facebook.com".$fbid."&photoid=".$upphoto."")
?>

我正在使用这个 php 代码制作一个 facebook 应用程序。我将字体 Arial.ttf上传到我网站的根目录。但我仍然显示错误-Warning: imagettftext() [function.imagettftext]: Could not find/open font in /home/a2424901/public_html/index.php on line 35。我尝试改变案例,但我没有为我工作。这段代码哪里出错了?

【问题讨论】:

    标签: php truetype imagettftext


    【解决方案1】:

    From the docs

    根据 PHP 使用的 GD 库版本,当字体文件不以前导 / 开头时,.ttf 将附加到文件名,并且库将尝试沿着库定义的字体路径搜索该文件名.

    这似乎暗示字体文件应该是一个绝对路径,如果不是,该函数将在其末尾附加另一个.ttf

    指定字体文件的完整路径。

    $font = "/home/a2424901/public_html/Arial.ttf";
    

    或者省略.ttf 并使用GDFONTPATHThe documentation 建议如下:

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

    putenv('GDFONTPATH=' . realpath('.'));
    $font = "Arial";
    

    【讨论】:

      【解决方案2】:

      添加到 user2724960 的答案; 将 FontName 更改为 __DIR__ . '/graph/fonts/someFont.ttf' 为我做了。

      整行:

      $myPicture->setFontProperties(array("FontName"=>__DIR__ .  '/graph/fonts/someFont.ttf',"FontSize"=>14));
      

      不要忘记将“someFont”替换为您的字体文件的名称(默认:“Forgotte”)

      【讨论】:

      • 它有效,谢谢。我以为是权限问题,其实不是。
      【解决方案3】:

      我的解决方案(对我有用):

      realpath('here/is/right/path/to/font.ttf');
      

      【讨论】:

        【解决方案4】:

        我也在 XAMPP 上,显然 XAMPP 只支持字体的完整目录路径。此代码适用于 Windows (XAMPP) 和我们的供应商 Linux 服务器:

        $dir= dirname(realpath(__FILE__));
        $sep=DIRECTORY_SEPARATOR;   
        $font =$dir.$sep.'arial.ttf';
        imagettftext($thumb, $size, 0, $y_pos, $size, $textcolor, $font, $txt);
        

        (假设你的字体文件和你的php文件在同一个目录下)

        【讨论】:

          【解决方案5】:

          我有同样的问题。我的字体名称是

          Titr.TTF

          我把它改成了

          Titr.ttf

          而且效果很好。

          【讨论】:

            【解决方案6】:

            它对我有用: 使用绝对路径

            $font = 'C:\wamp\www\Persian-txt2img\Vazir-Code.ttf';
            $font = mb_convert_encoding($font, 'big5', 'utf-8');
            
            // Add the text
            imagettftext($image, 24, 0, 64, 48, $text_color, $font, $text);
            

            【讨论】:

              【解决方案7】:

              如果您使用的是 pChart,请使用:

              $myPicture->setFontProperties(array("FontName"=>"../fonts/Forgotte.ttf","FontSize"=>11));
              

              【讨论】:

                【解决方案8】:

                在 GoDaddy(共享主机,Cpanel)上,我只需要在 域之后指定路径:

                我的文件在这里:

                URL:  http://example.com/assets/fonts/verdana/verdana.ttf
                path:  /home/content/123456/html/example/assets/fonts/verdana/verdana.ttf
                

                有效的 PHP 代码:

                $font = 'assets/fonts/verdana/verdana.ttf';
                

                对路径使用其他值不起作用。

                【讨论】:

                  【解决方案9】:

                  我在使用 localhost (XAMPP) 时遇到了同样的问题。

                  我的解决方案是:

                  // Set Correct Path to Font File
                  $fontPath='C:\xampp\htdocs\Adeplay\fonts\Tesox\tesox.ttf'; 
                  

                  【讨论】:

                    【解决方案10】:

                    请检查资源下的字体文件夹。

                    【讨论】:

                    • 这应该是评论,而不是答案。
                    猜你喜欢
                    • 1970-01-01
                    • 1970-01-01
                    • 2021-08-11
                    • 1970-01-01
                    • 1970-01-01
                    • 2012-09-16
                    • 2014-06-21
                    • 2015-10-05
                    • 1970-01-01
                    相关资源
                    最近更新 更多