如何正确使用imagettftext?
How to correctly use of imagettftext?
我正在制作一个表格,用户正在填写该表格以创建邮件签名:
generatarjeta.php
if(isset($_GET["suc"]) && $_GET["suc"] !="")
{ $code="";
foreach($_GET as $key => $value){
$code.= $key . "=" . $value . "&";
}
$code = rtrim($code,"&");
?>
<img src="imagem.php?<?php echo $code;?>">
<script>window.history.pushState("", "", '/generatarjeta.php');</script>
<?php exit();
}
<form method="get">
<table>
<tr><td><label>Sucursal</label></td><td><select name="suc">
<option value="">Seleccione...</option>
<?php
$con_sucursal = mssql_query("select * from localitos where local NOT IN (3,5, 7,12, 13, 14,15,16,17,19, 20, 21, 22,23,24,25,26,27)");
while ($res_sucursal = mssql_fetch_array($con_sucursal))
{
echo "<option value='$res_sucursal[0]'>$res_sucursal[1]</option>";
}?>
</select></td></tr>
<tr><td><label>Nombre del Trabajador: </label></td><td><input type="text" name="nombre" required="" onkeypress="return textonly(event);"/></td></tr>
<tr><td><label>Puesto: </label></td><td><input type="text" name="puesto" required="" onkeypress="return textonly(event);"/></td></tr>
<tr><td><label>Telefono: </label></td><td><input type="text" name="tel" required="" onkeypress='return solonums(event)'/> <label>Extension: </label><input type="text" name="ext" onkeypress='return solonums(event)'/></td></tr>
<tr><td><label>correo: </label></td><td><input type="text" name="mail" placeholder="nombre.apellido"/></td></tr>
<tr><td><input type="submit" name="submit" value="Generar"/></td><td><input type="reset"/></td></tr>
</table>
</form>
imagem.php
//Carregar imagem
$rImg = ImageCreateFromJPEG("the_firma.jpg");
//Definir cor
$cor = imagecolorallocate($rImg, 226, 41, 34);
$cor2 = imagecolorallocate($rImg, 60, 60, 59);
$tel = "$_GET[tel] Ext. $_GET[ext]";
$correo = "$_GET[mail]@mail.x";
//Escrever nome
imagettftext($rImg,14,0,850,60,$cor,'Open Sans',$_GET["nombre"]);
//imagestring($rImg,5,850,60,urldecode($_GET["nombre"]),$cor);
//imagestring($rImg,5,850,80,urldecode($_GET["puesto"]),$cor2);
//imagestring($rImg,5,800,120,urldecode($tel),$cor2);
//imagestring($rImg,5,800,140,urldecode($correo),$cor2);
//Header e output
header('Content-type: image/jpeg');
imagejpeg($rImg,NULL,100);
我的问题是,当我使用 imagestring 时,一切正常,但我需要使用 Open Sans ttf,所以我尝试使用 imagettftext,但它 "echoes" 在加载 img src 和单独测试文件时也没有显示任何错误。
有什么建议吗?
更新:
$tel = "$_GET[tel] Ext. $_GET[ext]";
$correo = "$_GET[mail]@mail.mx";
$puesto = strtoupper($_GET["puesto"]);
imagettftext($rImg, 14, 0, 780,60, $cor, 'OpenSans-Bold.ttf', $_GET['nombre']); (WORKS)
imagettftext($rImg, 8, 0, 780,80, $cor2, 'OpenSans-Regular.ttf', $puesto); (WORKS)
imagettftext($rImg,9,780,135,$cor2, 'OpenSans-Regular.ttf',urldecode($_GET["tel"])); (CRASHES)
imagettftext($rImg,9,780,172,$cor2,'OpenSans-Regular.ttf',$correo);(CRASHES)
imagettftext($rImg,8,780,209,$cor2,'OpenSans-Regular.ttf',$_GET["direccion"]);(CRASHES)
imagettftext($rImg,8,780,223,$cor2,'OpenSans-Regular.ttf',$_GET["dir2"]);(CRASHES)
与 documented 一样,$fontfile
参数需要字体文件的 路径 ,而不是它的显示名称:
The path to the TrueType font you wish to use.
最简单的解决方案是将 .ttf 文件 (opensans.ttf
?) 放在与脚本相同的文件夹中,然后像这样更改函数调用:
imagettftext($rImg, 14, 0, 850, 60, $cor, 'opensans.ttf', $_GET['nombre']);
我正在制作一个表格,用户正在填写该表格以创建邮件签名:
generatarjeta.php
if(isset($_GET["suc"]) && $_GET["suc"] !="")
{ $code="";
foreach($_GET as $key => $value){
$code.= $key . "=" . $value . "&";
}
$code = rtrim($code,"&");
?>
<img src="imagem.php?<?php echo $code;?>">
<script>window.history.pushState("", "", '/generatarjeta.php');</script>
<?php exit();
}
<form method="get">
<table>
<tr><td><label>Sucursal</label></td><td><select name="suc">
<option value="">Seleccione...</option>
<?php
$con_sucursal = mssql_query("select * from localitos where local NOT IN (3,5, 7,12, 13, 14,15,16,17,19, 20, 21, 22,23,24,25,26,27)");
while ($res_sucursal = mssql_fetch_array($con_sucursal))
{
echo "<option value='$res_sucursal[0]'>$res_sucursal[1]</option>";
}?>
</select></td></tr>
<tr><td><label>Nombre del Trabajador: </label></td><td><input type="text" name="nombre" required="" onkeypress="return textonly(event);"/></td></tr>
<tr><td><label>Puesto: </label></td><td><input type="text" name="puesto" required="" onkeypress="return textonly(event);"/></td></tr>
<tr><td><label>Telefono: </label></td><td><input type="text" name="tel" required="" onkeypress='return solonums(event)'/> <label>Extension: </label><input type="text" name="ext" onkeypress='return solonums(event)'/></td></tr>
<tr><td><label>correo: </label></td><td><input type="text" name="mail" placeholder="nombre.apellido"/></td></tr>
<tr><td><input type="submit" name="submit" value="Generar"/></td><td><input type="reset"/></td></tr>
</table>
</form>
imagem.php
//Carregar imagem
$rImg = ImageCreateFromJPEG("the_firma.jpg");
//Definir cor
$cor = imagecolorallocate($rImg, 226, 41, 34);
$cor2 = imagecolorallocate($rImg, 60, 60, 59);
$tel = "$_GET[tel] Ext. $_GET[ext]";
$correo = "$_GET[mail]@mail.x";
//Escrever nome
imagettftext($rImg,14,0,850,60,$cor,'Open Sans',$_GET["nombre"]);
//imagestring($rImg,5,850,60,urldecode($_GET["nombre"]),$cor);
//imagestring($rImg,5,850,80,urldecode($_GET["puesto"]),$cor2);
//imagestring($rImg,5,800,120,urldecode($tel),$cor2);
//imagestring($rImg,5,800,140,urldecode($correo),$cor2);
//Header e output
header('Content-type: image/jpeg');
imagejpeg($rImg,NULL,100);
我的问题是,当我使用 imagestring 时,一切正常,但我需要使用 Open Sans ttf,所以我尝试使用 imagettftext,但它 "echoes" 在加载 img src 和单独测试文件时也没有显示任何错误。
有什么建议吗?
更新:
$tel = "$_GET[tel] Ext. $_GET[ext]";
$correo = "$_GET[mail]@mail.mx";
$puesto = strtoupper($_GET["puesto"]);
imagettftext($rImg, 14, 0, 780,60, $cor, 'OpenSans-Bold.ttf', $_GET['nombre']); (WORKS)
imagettftext($rImg, 8, 0, 780,80, $cor2, 'OpenSans-Regular.ttf', $puesto); (WORKS)
imagettftext($rImg,9,780,135,$cor2, 'OpenSans-Regular.ttf',urldecode($_GET["tel"])); (CRASHES)
imagettftext($rImg,9,780,172,$cor2,'OpenSans-Regular.ttf',$correo);(CRASHES)
imagettftext($rImg,8,780,209,$cor2,'OpenSans-Regular.ttf',$_GET["direccion"]);(CRASHES)
imagettftext($rImg,8,780,223,$cor2,'OpenSans-Regular.ttf',$_GET["dir2"]);(CRASHES)
与 documented 一样,$fontfile
参数需要字体文件的 路径 ,而不是它的显示名称:
The path to the TrueType font you wish to use.
最简单的解决方案是将 .ttf 文件 (opensans.ttf
?) 放在与脚本相同的文件夹中,然后像这样更改函数调用:
imagettftext($rImg, 14, 0, 850, 60, $cor, 'opensans.ttf', $_GET['nombre']);