Image Transformation of E-mail Text - PHP and Javascript

Asked

Viewed 296 times

1

On my site (made in PHP and Javascript), I have a routine of sending vouchers (is a hotel client’s site) pro e-mail guest.

I wonder if it is possible to turn all the content of the email that is sent to the guest into an image (JPG, PNG, BMP or any other format).

  • In case it would be for him not to change the content of the voucher? Try to send the voucher in PDF using the dompdf. I believe it is the most practical and usual method. Hug

  • I think your question is not very clear. What kind of content?

  • The content is text and some images. And it has some links too. I imagine that the ideal in this case would be to put the same content of the email as a PDF attached, not to lose the links too.

1 answer

2

Yes, it is possible to use the library GD or Image Magick for image manipulation in PHP.

First you need a base image (with customer logo, background art, footer...). Ai using GD or Image Magick you enter custom text for each user (link, or code). Ai 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 be the code in Base 64 of the image or you can generate a physical file of that image and host in your site and just reference it in e-mail or you can generate the image on demand, that is, when the user opens the email, he calls a.php file on his website that is responsible for writing on an image or name of it and the voucher.

I’ll show you an example, with the last case I spoke about. You will send the HTML email with an image tag and the image path will be a file . php 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 customers put the following code:

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

Note that both the voucher and the client name is in the queryString of the file that will be loaded as an image (but it is actually a PHP file). Ai your file geraVoucher.php will be the following:

<?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);
?>

Browser other questions tagged

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