Transformation of text from email into image - PHP and Javascript

1

On my website (made in PHP and Javascript), I have a voucher submission routine (it is a hotel client's website) for guest email.

I would like to know if it is possible for me to transform all the content of the email that is sent to the guest into an image (JPG, PNG, BMP or any other format).

    
asked by anonymous 28.05.2015 / 16:50

1 answer

2

Yes, you can use the library GD or Image Magick for image manipulation in PHP.

First you need a base image (with the client logo, background art, footer ...). Already using GD or Image Magick you insert the personalized text for each user (link, or code). Then you can include the image as an attachment in the body of the email, you can send the email with the image and the source of the img tag is the code in Base 64 of the image or you can generate a physical file of that image and host it in your site and just refer it in the email or you can generate the image on demand, that is, when the user opens the email, it calls a .php file on your website which is responsible for writing in an image or name of it and the voucher.

I'll show you an example, with the last case I said. You will send the HTML email with an image tag and the image path will be an .php file Let's use the GD library for this example.

Create a PHP file with the following name, for example: geraVoucher.php In the emails of the clients put the following code:

<img src="http://www.seusite.com.br/geraVoucher.php?nomeCliente=Guilherme&voucher=MAIO2015DESCONTO"alt="Seu voucher é MAIO2015DESCONTO" />

Notice that both the voucher and the client name are in the queryString of the file that will be uploaded as an image (but in fact it is a PHP file). Then your geraVoucher.php file will look like this:

<?php
    header('Content-Type: image/png'); //define o cabeçalho como Imagem / PNG assim o navegador vai reconhecer como imagem e mostrar no corpo do e-mail do usuário.
    $nomeCliente = $_GET["nomeCliente"]; //obtém a queryString com o nome do cliente
    $voucher = $_GET["voucher"]; //obtém a queryString com o código VOUCHER para o cliente
    $imagem = imagecreatefrompng("base.png"); //base.png é a moldura da imagem.
    $preto = imagecolorallocate($imagem, 0, 0, 0); //define a cor "preto"
    $vermelho = imagecolorallocate($imagem, 255, 0, 0); //define a cor "vermelho"
    $fonteCaminho = "/fontes/arial.ttf"; //local onde se encontra o arquivo da fonte TTF (true type font)

    imagettftext($imagem, 25, 0, 15, 50, $preto, $fonteCaminho, "Olá $nomeCliente, seu voucher para desconto é:"); //escreve a primeira linha de texto, com a cor preta, nas coordenadas X->15 e Y->50, com a fonte tamanho 25 e 0 de inclinação.
    imagettftext($imagem, 30, 0, 15, 80, $vermelho, $fonteCaminho, $voucher); //escreve o VOUCHER em uma segunda linha na cor vermelho, nas coordenadas X->15 e Y->80 com fonte tamanho 3,0 e 0 de inclinação.

    /* Porque a segunda linha está nas coordenadas 15x80 ?
       Porque a primeira linha, você escreveu nas coordenadas 15x50 com uma fonte 25, logo vai acabar no pixel 75 (50 + 25) e ai adicionamos 5 pixels de margem. */
    imagepng($imagem);
    imagedestroy($imagem);
?>
    
28.05.2015 / 18:29