How to center up to 200 characters of text when printing in php

2

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);
?>
    
asked by look68 16.02.2017 в 17:10
source

1 answer

2

You could use the str_pad function to fill each row until it has 40 characters:

foreach ($lines as $line)
        {
            $line = str_pad($line, 40," ", STR_PAD_BOTH);
            imagettftext($dest, 18, 0, $x, $y, $font_color, $vardir.'arial.ttf', $line);
            $y += $int;
        }

That will insert spaces on both sides to complete 40 characters.

Edit: given the problem that in many fonts the width of the letters is not uniform, and that the user can not use a monospace font, the alternative is to use imagettfbbox .

We generate, as reference, a string of 40 "X" that will be our maximum box:

$string_referencia=str_pad("X", 40,"X", STR_PAD_BOTH);

We measure its width

$bbox = imagettfbbox(18, 0, $vardir.'arial.ttf', $string_referencia);
$ancho_max=$bbox[2]-$bbox[0];

Then for each row, we add to the X position the delta needed to center:

$bbox_fila = imagettfbbox(18, 0, $vardir.'arial.ttf', $line);
$dx = ($ancho_max - ($bbox_fila[2] - $bbox_fila[0]))/2
imagettftext($dest, 18, 0, $x + $dx, $y, $font_color, $vardir.'arial.ttf', $line);

Edit: To clarify, the code would be:

$string_referencia=str_pad("X", 40,"X", STR_PAD_BOTH);
$bbox = imagettfbbox(18, 0, $vardir.'arial.ttf', $string_referencia);
$ancho_max=$bbox[2]-$bbox[0];

foreach ($lines as $line) {
    $bbox_fila = imagettfbbox(18, 0, $vardir.'arial.ttf', $line);
    $dx = ($ancho_max - ($bbox_fila[2] - $bbox_fila[0]))/2
    imagettftext($dest, 18, 0, $x + $dx, $y, $font_color, $vardir.'arial.ttf', $line);
    $y += $int;
    }
    
answered by 16.02.2017 / 18:42
source