IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Bibliothèques et frameworks PHP Discussion :

Centrer un texte avec imagettfbbox


Sujet :

Bibliothèques et frameworks PHP

  1. #1
    Membre à l'essai
    Homme Profil pro
    Pub
    Inscrit en
    Octobre 2018
    Messages
    20
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 47
    Localisation : France, Nord (Nord Pas de Calais)

    Informations professionnelles :
    Activité : Pub
    Secteur : Communication - Médias

    Informations forums :
    Inscription : Octobre 2018
    Messages : 20
    Points : 11
    Points
    11
    Par défaut Centrer un texte avec imagettfbbox
    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

  2. #2
    Membre à l'essai
    Homme Profil pro
    Pub
    Inscrit en
    Octobre 2018
    Messages
    20
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 47
    Localisation : France, Nord (Nord Pas de Calais)

    Informations professionnelles :
    Activité : Pub
    Secteur : Communication - Médias

    Informations forums :
    Inscription : Octobre 2018
    Messages : 20
    Points : 11
    Points
    11
    Par défaut
    Personne n'a d'idée ?

  3. #3
    Expert éminent
    Avatar de Séb.
    Profil pro
    Inscrit en
    Mars 2005
    Messages
    5 091
    Détails du profil
    Informations personnelles :
    Âge : 46
    Localisation : France

    Informations professionnelles :
    Secteur : High Tech - Opérateur de télécommunications

    Informations forums :
    Inscription : Mars 2005
    Messages : 5 091
    Points : 8 194
    Points
    8 194
    Billets dans le blog
    17
    Par défaut
    Perso j'utiliserais imagettfbbox() pour calculer la place prise par le texte
    Et ensuite je positionnerais le texte pour un centrage en fonction de sa taille et de la taille de l'image finale
    Un problème exposé clairement est déjà à moitié résolu
    Keep It Smart and Simple

Discussions similaires

  1. Réponses: 0
    Dernier message: 06/12/2022, 15h50
  2. centrer un text avec FormBorderStyle.None
    Par Miska59 dans le forum VB.NET
    Réponses: 2
    Dernier message: 10/06/2019, 11h17
  3. Centrer du texte avec un retour à la ligne
    Par Jiyuu dans le forum Qt Quick
    Réponses: 2
    Dernier message: 11/10/2013, 21h44
  4. centrer un bloc avec image et son texte à droite
    Par ml1234 dans le forum Mise en page CSS
    Réponses: 12
    Dernier message: 17/08/2009, 22h58
  5. Centrer un texte dans un string Grid
    Par christine dans le forum C++Builder
    Réponses: 5
    Dernier message: 22/10/2002, 22h33

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo