I am learning php and I need to modify the printing of some tickets in an application of my work. I have managed to work correctly but I have a problem.
A legend field2 has been added, which is a variable description, from 1 to 200 characters. In another part of the code I detect whether there is a legend or not, and if it exists, I print it inside the ticket.
The issue is that through a for each I have managed to print it but it is aligned to the left and the need is to print aligned to the center.
I copy the related code to print:
$lines = explode('|', wordwrap($leyenda2, 40, '|'));
$x=27; $y=220; $int=40;
imagettftext ($dest, 18 , 0 , $x , $y , 0 , $vardir.'arial.ttf' , "Fecha: $vfecha" ); $y+=$int;
imagettftext ($dest, 18 , 0 , $x , $y , 0 , $vardir.'arial.ttf' , "$vpuntos puntos" ); $y+=$int;
imagettftext ($dest, 18 , 0 , $x , $y , 0 , $vardir.'arial.ttf' , "Socio nro.: $vnrosocio" ); $y+=$int;
imagettftext ($dest, 18 , 0 , $x , $y , 0 , $vardir.'arial.ttf' , "$vapellido $vnombre" ); $y+=$int;
imagettftext ($dest, 18 , 0 , $x , $y , 0 , $vardir.'arial.ttf' , "DNI $vdni" ); $y+=$int;
imagettftext ($dest, 18 , 0 , $x , $y , 0 , $vardir.'arial.ttf' , "Premio: $vpremio" ); $y+=$int;
imagettftext ($dest, 18 , 0 , $x , $y , 0 , $vardir.'arial.ttf' , "$vdescpremio" ); $y+=$int;
if ($vcate=="Accesorio2") {
imagettftext ($dest, 18 , 0 , $x , $y , 0 , $vardir.'arial.ttf' , " " ); $y+=$int;
imagettftext ($dest, 18 , 0 , $x , $y , 0 , $vardir.'arial.ttf' , " " ); $y+=$int;
foreach ($lines as $line)
{
imagettftext($dest, 18, 0, $x, $y, $font_color, $vardir.'arial.ttf', $line);
$y += $int;
}
}
In the variable legend2 I have the text that I need to format.
On the line: if ($vcate=="Accesorio2")
I define that if it is Accessory2 print the key.
Here I show a graph with what I get now, and below what I need:
Last version of the code what works, but calculation is close:
<?php
Header("Content-type: image/png");
$im = imagecreate(400,300);
$fondo=imagecolorallocate ($im, 255, 255, 110);
$gris=imagecolorallocate ($im, 160, 160,160);
$negro=imagecolorallocate ($im, 0, 0, 0);
$texto="Estosos dsdwsvwes rbbuna rbprueba de impresio Esto esrbbrrbr una prueba de impresiorbrr Esto es una prueba de impresion";
$lines1 = explode('|', wordwrap($texto, 28, '|'));
$y=150;
foreach ($lines1 as $line1) {
{
$line1=trim($line1);
$line1 = str_pad($line1, 40,"0", STR_PAD_BOTH);
$bbox = imagettfbbox(18, 0,'arial.ttf', $line1);
$ancho_max=$bbox[2]-$bbox[0];
$bbox_fila = imagettfbbox(18, 0, 'arial.ttf', $line1);
$dx = ($ancho_max - ($bbox_fila[2] - $bbox_fila[0]))/2;
imagettftext($im, 18, 0, $x + $dx, $y, $negro,'arial.ttf', $line1);
}
}
Imagepng($im);
imagedestroy($im);
?>