3

I have the following code that adds the first name and last name onto a predefined image.

function create_image($fname, $lname)
{
 $image = imagecreatefromjpeg('sample.jpg');
 $color = imagecolorallocate($image, 255, 255, 255);
 $font_path = '../fonts/GSMT.TTF';
 $font_size = 18;
 $first_name = $name;
 $last_name = $lname;
 $name = $fname." ". $lname;

// Print Text On Image
imagettftext($image, $font_size, 0, 100, 160, $color, $font_path, $name);

header('Content-type: image/jpeg');
imagejpeg($image);
imagedestroy($image);
}

I use the following email script;

function mailer($email_adrress, $registration_id)
{
$mail = new PHPMailer;

$mail->isSMTP(); // set mailer to use SMTP
$mail->Host = 'mail.server.com'; // set mail server
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->Username = 'email@address.com'; // SMTP username
$mail->Password = 'password'; // SMTP password
$mail->Port = 587; // TCP port to connect to
$mail->IsHTML(true);      
$mail->From = 'johndoe@gmail.com';
$mail->FromName = 'John Doe'; // from name     
$mail->addAddress($email_adrress); // recipient                            

$mail->Subject = 'emailer';
$mail->Body    = "Congratulations you have been Successfully registered.   

if(!$mail->send()) 
{
    die("error mail not sent);
} 
}

When the create_image function is called the image gets created successfully. But my question is instead of printing the image onto the browser how do i mail it. Should i temporarily save the image till the mail is sent and then delete it, or is there another approach?

miken32
  • 42,008
  • 16
  • 111
  • 154
nad
  • 1,068
  • 6
  • 16
  • You could [embed with base64 the images](http://stackoverflow.com/questions/1207190/embedding-base64-images). The majority of the browsers can interpret that. Also, the user doesn't have to click `view images` in their email/webmail client. – machineaddict Mar 04 '15 at 12:46

2 Answers2

1

I think you should use output buffering and encode the image to base64 and use it in src tag

ob_start();
// output jpeg (or any other chosen) format & quality
imagejpeg($image);
$contents = ob_get_contents();
ob_end_clean();

Now you need base64 content

$base64 = base64_encode($contents);

now you can use it in your message HTML contents

<img src="data:image/jpeg;base64,$base64">
Robert
  • 19,800
  • 5
  • 55
  • 85
0

Yes, you need to save it, send it and then delete it, as you said. It will be impossible to use php directly to show it up. However you might be able to use iframe. But the first solution I think is the best.

Leandro Papasidero
  • 3,728
  • 1
  • 18
  • 33