2016-10-13 20 views
1

テキストを入力してイメージに変換しようとしています。フォントのパスとは何ですか?

私はPHPマニュアルからいくつかのコードを得ました。 symfonyの3.1

でそれに取り組んで

イム私が持っている唯一の問題は、フォントのパスを設定しない方法です。

robot-regular.ttfは私の/webディレクトリにあります。

マイコード:

//setting the content type 
    header('Content-Type: image/png'); 

    // Create the image 
    $im = imagecreatetruecolor(400,30); 

    // Create Some colors 
    $white = imagecolorallocate($im, 255, 255, 255); 
    $grey = imagecolorallocate($im, 128, 128, 128); 
    $black = imagecolorallocate($im, 0, 0, 0); 
    imagefilledrectangle($im, 0, 0, 399, 29, $white); 

    // The text to draw 
    $text = 'Testing'; 
    // Replace path by your font path 
    $font = 'Roboto-Regular.ttf'; 

    // Add some shadow to the text 
    imagettftext($im, 20, 0, 11, 21, $grey, $font, $text); 

    // Add the text 
    imagettftext($im, 20, 0, 10, 20, $black, $font, $text); 

    // Using imagepng() resulst in clearer text compared with imagejpeg() 
    imagepng($im); 
    imagedestroy($im); 

答えて

2

実際にフレームワークがwebdirのは完全に独立しているようSymfony2の中でwebdirのへのパスを取得する直接的な方法はありません。 しかし、あなたはgetRootDir()使用することができます。

$path = $this->get('kernel')->getRootDir() . '/../web'; 
1
$this->get('request')->getBasePath(); 

$this->get('request')->getBasePath();は、直接Webディレクトリであなたを配置します

関連する問題