generate php image without saving to disk and send attachment

Asked

Viewed 188 times

1

So, I have a script that generates a boleto in image format (using imagegif()).

I wonder if there is a way to send this attached boleto (use phpmailer) without having to save the image to disk. Reason? Avoid staying using disk.

I mean, is there any magic for me to do that?

1 answer

3


It is possible using the function AddStringAttachment.

In your case it would look something like this:

$mail = new PHPMailer();
// ....
ob_clean(); // limpa qualquer coisa no buffer de saída
ob_start();
imagegif($img); // gera a imagem (assumindo que já exista uma imagem em $img)
$gif = ob_get_clean(); // $gif agora contém a imagem gerada

$mail->AddStringAttachment($gif, 'boleto.gif'); // 'boleto.gif' é o nome do anexo que vai aparecer no e-mail
  • The problem of this is that it needs to generate the file. I don’t know if it is so important not to generate the file but it is a requirement that it put.

  • @bigown This will not generate any file. imagegif() will print the image in the output buffer and the ob_get_clean() will pick up directly from there. Nothing goes to the file system.

  • imagecreatefromstring() can also be used..

  • So he accepts one stream any instead of a file? Did not know this.

  • @bigown That’s right.

  • For now it worked. I’ll see if my manager accepts this way.

  • @daniel-Omine if you pass the second argument in imagegif it creates a file. In the case of imagecreatefromstring I could use the $mail->addStringAttachment(imagecreatefromstring($img)); ? OBS: this way was not, only with the @bigown

  • Good guys, thank you. That’s right!

  • see the documentation to see the appropriate use for your Brow case.

Show 4 more comments

Browser other questions tagged

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