Generate transparent image through a form

Asked

Viewed 155 times

0

Hello.

I want to create a form with one input field and the other as Submit.

When you write something in the input field and click the Submit button an image will be generated through the PHP GD library. But the problem is, I don’t know how I can do this, I’ve researched several forums and read several articles on, but I can’t at all.

HTML CODE

<form action="" method="get">
<input name="nome" type="text" placeholder="Texto a ser Gerado" size="100"/>
<input type="submit" value="Criar Imagem"/>

PHP CODE

<?php
//Define o header como sendo de imagem
header("Content-type: image/png");

// Definições
$preto = imagecolorallocate($i, 0,0,0); #Cor do texto; "cor preta"
$texto = "Exemplo"; #Texto a ser escrito
$fonte = "trebuc.ttf"; #Fonte que será utilizada

//Escreve na imagem
imagettftext($i, 32, 0, 160,360, $preto, $fonte, $texto);

//Gera a imagem na tela
imagejpeg($i);

//Destroi a imagem para liberar memória
imagedestroy($i);

?>

Please give me a solution to my problem, how can I fix it?

Thank you so much from the start.

1 answer

0

I think you can do something like:

index.html

<form action="arquivo_gera_imagem.php" method="post">
     <input name="nome" type="text" placeholder="Texto a ser Gerado" size="100"/>
     <input type="submit" value="Criar Imagem"/>
</form>

file_gera_image.php

<?php
//Define o header como sendo de imagem
header("Content-type: image/png");

$i = imagecreatefrompng('IMAGEM.png');    

// Definições
$preto = imagecolorallocate($i, 0,0,0); #Cor do texto; "cor preta"
$fonte = "trebuc.ttf"; #Fonte que será utilizada
$texto = $_POST["nome"];

//Escreve na imagem
imagettftext($i, 32, 0, 160,360, $preto, $fonte, $texto);

//Gera a imagem na tela
imagejpeg($i);

//Destroi a imagem para liberar memória
imagedestroy($i);
?>

Note that in the index.html file I fixed the form, closing the tag, setting the method to POST and including in the action the name of the target file.

In the file file_image.php I changed the value of the variable $text to what comes in the post $_POST["name"]

  • I did everything as you said, but I did not get a result with the code. Typing something in the input field and clicking the Ubmit button generates something like this. i.imgur.com/pnqNppo.png - But it does not generate the image with the text you insert in the input field, I hope you help. Thanks in advance.

  • I’ll check again.

  • @Trux what you are getting in the $i variable ?

  • You need to upload an initial image for this, $i = imagecreatefromjpeg('imagem_initial.jpg');

Browser other questions tagged

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