Centrer un texte dans un rectangle avec GD

Petit nouveau ! | 7 Messages

27 août 2016, 20:52

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 !

Avatar du membre
Administrateur PHPfrance
Administrateur PHPfrance | 9783 Messages

27 août 2016, 22:14

Bonjour,

Voici une piste issue d'un commentaire dans la doc : http://php.net/manual/fr/function.image ... .php#68518
Quand tout le reste a échoué, lisez le mode d'emploi...

Petit nouveau ! | 7 Messages

27 août 2016, 22:47

Je suis perdu avec cette fonction ! Je ne comprend pas comment procédez

Pouvez vous m'aidez svp ?

Mon texte a centrer dans un rectangle:
$text = $listuser[0]->name;
Mon code :
<?php
		$listuser = json_decode( urldecode($this->uri->segment(3)) );		
		$app_id = $this->uri->segment(4);
		$slogans= $this->App_content->get_all_question($app_id);

                // Fond
		$bg = $this->App->get_app($app_id)[0]['img'];
		$dst_im = imagecreatefromjpeg($bg);
                
                 // Couleurs
		$black = imagecolorallocate($dst_im, 0, 0, 0);
		$white = imagecolorallocate($dst_im, 255, 255, 255);

		$font_path = 'assets/fonts/RobotoSlab-Regular.ttf';

		// Test centrer texte dans un rectangle

               $box = @imageTTFBbox(12,0, $font_path, $text);
               $width = abs($box[4] - $box[0]);
               $height = abs($box[5] - $box[1]);
		
		$x -= $width/2;
                $y += $heigth/2;

		$text = $listuser[0]->name;
		
                imagettftext($dst_im, 40, 0, $padding, 460, $black, $font_path, $text);

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

Mammouth du PHP | 688 Messages

27 août 2016, 23:42

dans l'exemple donné, il y utilisation des variables $x et $y :
imageTTFText($img,$size,0,$x,$y,$color,$font,$text);
pas dans ton code.

Petit nouveau ! | 7 Messages

28 août 2016, 22:28

J'ai apporter les changements mais rien ne s'affiche :/