我正在尝试做一些文本并将其转换为图像。
我从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);
实际上在Symfony2中没有直接的方法来获取webdir的路径,因为框架完全独立于webdir。但是你可以使用getRootDir()
:
$path = $this->get('kernel')->getRootDir() . '/../web';
$this->get('request')->getBasePath();
$this->get('request')->getBasePath();
会直接把你放到web目录