Manipulating $ _GET with Line Break

1

I'm using the GD Library, I have the code below and I want to put a line break $texto = "primeira linha\nsegunda linha"; , however it needs to pass $get .

What I mean by going through $get is that if you replace the line

$texto = $_GET['texto']; 

by

$texto = "linha1\n linha2"; 

In the file img.php the \n will break the line and using the form in method get the text stays on the same line.

How to fix this?

index.php

<?php $go = isset($_GET['texto'])?$_GET['texto']:"primeira linha\nsegunda linha";?>
   <form method="get">
   <input name="texto" type="text" value="<?php echo $go ?>" size="100"/>
   <input type="submit" value="Criar imagem" />
   </form>
   <img src="img.php?texto=<?php echo $go ?>"  /> 

img.php

<?php
$tamfonte = 25;
$fonte = 'verdana.ttf';
$texto = $_GET['texto'];
$img = imagettfbbox($tamfonte, 0, $fonte, $texto);
$largura = "600";
$altura = "400";
$imagem = imagecreate($largura, $altura);
imagecolorallocate($imagem, 255, 255, 255);
$corfonte = imagecolorallocate($imagem, 0, 0, 0);
imagefttext($imagem, $tamfonte, 0, 0, abs($img[5]), $corfonte, $fonte, $texto);
header( 'Content-type: image/jpeg' );
imagejpeg($imagem, NULL, 100);  ?>   
    
asked by anonymous 16.10.2014 / 06:12

2 answers

2

In order to pass% of line breaks or other non-alphanumeric characters, you must encode the URL value.

In PHP you can use the $_GET function:

$string = "primeira linha\nsegunda linha";

$url = "http://www.example.com/?texto=".urlencode($string);

echo $url; // http://www.example.com/?texto=primeira+linha%0Asegunda+linha

PHP does decode automatically when it puts the values within urlencode() so your line break should be present when you access $_GET .

    
16.10.2014 / 13:48
1

To display 2 lines you have to break the string and repeat the text creation. I mean anything like this:

<?php

$tamfonte = 25;
$fonte = 'verdana.ttf';

$texto = explode("qualquer_separador", $_GET['texto']);


$largura = "600";
$altura = "400";

$imagem = imagecreate($largura, $altura);
imagecolorallocate($imagem, 255, 255, 255);
$corfonte = imagecolorallocate($imagem, 0, 0, 0);

$img = imagettfbbox($tamfonte, 0, $fonte, $texto[0]);

imagefttext($imagem, $tamfonte, 0, 0, abs($img[5]), $corfonte, $fonte, $texto[0]);


$img2 = imagettfbbox($tamfonte, 0, $fonte, $texto[1]);

imagefttext($imagem, $tamfonte, 0, 0, abs($img2[5]), $corfonte, $fonte, $texto[1]);

header( 'Content-type: image/jpeg' );
imagejpeg($imagem, NULL, 100);  

?> 

Note: This is just the concept, I did not do the sentence placement calculations because I do not know where to put them. So how are you going to stay on top of each other.

    
16.10.2014 / 12:01