Bonjour,

Je crée un petit script PHP qui va m'écrire deux blocs de texte sur une image, et j'aimerais que ces blocs de texte soient centrés dans les "box" créées pour chacun d'eux.

Voici le code que j'ai trouvé et qui me permet de mettre à la ligne du texte trop long en créant des "box" via imagettfbbox

Code php : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
$textSize = 48;
$font = "sys/Mohave-BoldItalic.otf";
$maxWidth = 459;
$texte_visiteur_brut = addslashes("NOM SUR PLUSIEURS LIGNES EN DEUXIEME COLONNE");
$texte_local_brut = addslashes("NOM EN PREMIERE COLONNE");
$image_bot=imagecreatetruecolor(1200,1200);
$noir = imagecolorallocatealpha($image_bot, 0, 0, 0,0);
$noir_fondu = imagecolorallocatealpha($image_bot, 0, 0, 0,45);
$vert = imagecolorallocatealpha($image_bot, 255, 255, 255,0);
 
// Coupe du nom du local en plusieurs lignes sur la base du maxWidth
$words_local = explode(' ', $texte_local_brut);
$lines_local = array();
$line_local = "";
foreach ($words_local as $word_local) {
    $box_local = imagettfbbox($textSize, 0, 'sys/Mohave-BoldItalic.otf', $line_local . $word_local);
 
    $width_local = $box_local[4] - $box_local[0];
    if($width_local > $maxWidth) {
        $line_local = trim($line_local);
        $line_local .= "\n";
    }
    $line_local .= $word_local . ' ';
 
}
 
// Coupe du nom du visiteur en plusieurs lignes sur la base du maxWidth
$words_visiteur = explode(' ', $texte_visiteur_brut );
$lines_visiteur = array();
$line_visiteur = "";
foreach ($words_visiteur as $word_visiteur) {
    $box_visiteur = imagettfbbox($textSize, 0, 'sys/Mohave-BoldItalic.otf', $line_visiteur . $word_visiteur);
 
    $width_visiteur = $box_visiteur[4] - $box_visiteur[0];
    if($width_visiteur > $maxWidth) {
        $line_visiteur = trim($line_visiteur);
        $line_visiteur .= "\n";
    }
    $line_visiteur .= $word_visiteur . ' ';
 
}
 
// Ecrit dans l'image
imagettftext($image_bot, 48, 0, 102, 512, $vert, 'sys/Mohave-BoldItalic.otf', $line_local); // ecrit le local
imagettftext($image_bot, 48, 0, 702, 512, $vert, 'sys/Mohave-BoldItalic.otf', $line_visiteur); // ecrit le visiteur 
imagepng($image_bot);

Comment obtenir chaque texte centré ? Merci