Centrer un texte dans un rectangle avec GD

Répondre


Cette question est un moyen d’empêcher des soumissions automatisées de formulaires par des robots.
Smileys
:D :) :( :o :shock: :? 8-) :lol: :x :P :oops: :cry: :evil: :twisted: :roll: :wink: :!: :?: :idea: :arrow: :| :mrgreen: =D> #-o =P~ :^o :non: :priere: 8-|
Voir plus de smileys
  Revue du sujet
 

  Étendre la vue Revue du sujet : Centrer un texte dans un rectangle avec GD

Re: Centrer un texte dans un rectangle avec GD

par bubus » 28 août 2016, 22:28

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

Re: Centrer un texte dans un rectangle avec GD

par tof73 » 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.

Re: Centrer un texte dans un rectangle avec GD

par bubus » 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);
?>

Re: Centrer un texte dans un rectangle avec GD

par @rthur » 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

Centrer un texte dans un rectangle avec GD

par bubus » 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 !