Handling $_GET with line break

Asked

Viewed 1,688 times

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";, but it needs to go through $get.

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

$texto = $_GET['texto']; 

for

$texto = "linha1\n linha2"; 

in the archive img.php the \n will break the line and using the form in method get the text is 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);  ?>   
  • @brasofilo - What I meant to go through $get was that if you replace the line $text = $_GET['text']; for that $text = "line1 n Linha2"; in the img.php file n will break the line and using oform in the get method the text is on the same line. this command "imagecreate( int_x size,int y_size);" has to do with GD the term in the perguta is because I used it is this!

2 answers

2

So you can pass by $_GET line breaks or other non-alphanumeric characters, you must encode the value to stay URL safe.

In PHP you can make use of the function urlencode():

$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 the Decode automatically when you place the values inside the $_GET so your line break should be present when you access $_GET['texto'].

1

To display 2 lines you have to break the string and repeat the text creation. That is anything of this type:

<?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 calculations of the positioning of sentences because I do not know where you want to put them. Just like this, they’ll be on top of each other.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.