Centrer un texte dans un rectangle avec GD

Fermé
socialGeek Messages postés 9 Date d'inscription dimanche 6 mars 2016 Statut Membre Dernière intervention 27 janvier 2020 - 27 août 2016 à 20:56
Pitet Messages postés 2826 Date d'inscription lundi 11 février 2013 Statut Membre Dernière intervention 21 juillet 2022 - 29 août 2016 à 14:50
Bonjour,

Alors voilà pour terminer mon site je dois centrer du texte dans un rectangle, pour cela je voudrais utilisez imagettfbbox mais le problème c'est que la longueur texte est variable ( Noms et prénom de l'utilisateur )

Je ne vois pas trop comment utiliser cette fonction pour qu'elle centre le texte selon sa taille !

Voici mon code:

public function render_img_result(){

        $listuser = json_decode( urldecode($this->uri->segment(3)) );      
        $app_id = $this->uri->segment(4);
        $slogans= $this->App_content->get_all_question($app_id);
        $bg =  $this->App->get_app($app_id)[0]['img'];
        $dst_im = imagecreatefromjpeg($bg);     
        $black = imagecolorallocate($dst_im, 0, 0, 0);
        $white = imagecolorallocate($dst_im, 255, 255, 255);

        // Set Path to Font File
        $font_path = 'assets/fonts/RobotoSlab-Regular.ttf';

        // Mon texte 1 ( Noms et Prénom de l'utilisateur )
        $padding = 50 + (213 - strlen( $listuser[0]->name ) * 8 ) / 2;
        imagettftext($dst_im, 40, 0, $padding, 460, $black, $font_path, $listuser[0]->name);


        // Mon texte 2 ( Noms et prénom d'un amis )
        $padding = 630 + (213 - strlen( $listuser[1]->name ) * 8 ) / 2;
        imagettftext($dst_im, 40, 0, $padding, 460, $black, $font_path,$listuser[1]->name);


        // Avatar utilisateur
        $src_im = imagecreatefromjpeg("http://graph.facebook.com/".$listuser[0]->id."/picture?type=normal&width=350&height=350");
        imagecopy ( $dst_im , $src_im , 110 , 35 , 0 , 0 , $this->img_size , $this->img_size);
        imagedestroy($src_im);

        // Avatar d'un amis
        $src_im = imagecreatefromjpeg("http://graph.facebook.com/".$listuser[1]->id."/picture?type=normal&width=350&height=350");
        imagecopy ( $dst_im , $src_im , 740 , 35 , 0 , 0 , $this->img_size , $this->img_size);
        imagedestroy($src_im);


        header("Content-type: image/jpeg");
        imagejpeg($dst_im);
        imagedestroy($dst_im);
    }


Pouvais vous me conseillez ?

Merci d'avance !



1 réponse

Pitet Messages postés 2826 Date d'inscription lundi 11 février 2013 Statut Membre Dernière intervention 21 juillet 2022 524
29 août 2016 à 14:50
Salut,

Le premier commentaire de la doc de la fonction imagettftext donne une bonne explication (en anglais) pour centrer horizontalement : https://www.php.net/manual/fr/function.imagettftext.php#83248

Bonne journée,
0