【发布时间】:2017-02-22 16:25:39
【问题描述】:
我正在尝试做一些文本并将其转换为图像。
我从 php 手册中得到了一些代码。
我正在 symfony 3.1 中处理它
我唯一的问题是你如何设置字体的路径。
roboto-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);
【问题讨论】: